3 Replies - 1638 Views - Last Post: 23 August 2012 - 10:05 PM Rate Topic: -----

#1 Peter0011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-July 10

problem Sending email with attachment in php.

Posted 23 August 2012 - 07:51 PM

When I send the mail it has an attachment but the file is empty.
Thanks.
<?php

function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
$allowedExts = array("jpg", "jpeg", "png","doc","docx","pdf");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ($_FILES["file"]["size"]/ 1024 < 1024 && in_array($extension, $allowedExts))
  {
  if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed

                //check if the email address is invalid
                $mailcheck = spamcheck($_REQUEST['email']);
                if ($mailcheck==FALSE)
                  {
                  echo "Invalid input";
                  }
                else
                  {//send email
                  $email = $_REQUEST['email'] ;
                  $subject = $_REQUEST['firstName']." ".$_REQUEST['lastName']." applying for the position of ". $_REQUEST['position'];

                  $fileName = $_FILES["file"]['name'];
                  $temp_name= $_FILES["file"]["tmp_name"];
                  $fileType = $_FILES["file"]["type"] ;
                  $message="Sample message";

              //attaching message
                  $file=$temp_name;
                  $content=  chunk_split(base64_encode(file_get_contents($file)));
                  $uid=  md5(uniqid(time()));

                 //header
                  $header ="From: ".$email."\r\n";
                  $header .="Reply-To: {$email}\r\n";
                  $header .="Mime-Version: 1.0\r\n";

                  $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                  $header .="This is a multi-part message in MIME format.\r\n";

                  $header .="--".$uid."\r\n";
                  $header .="Content-type:text/plain; charset=iso-8859-1\r\n";
                  $header .=$message."\r\n\r\n";

                  //file attachment

                  $header .="--{$uid}\r\n";
                  $header .="Content-Transfer-Encoding: base64\r\n";
                  $header .="Content-Type: {$fileType}; name=\"{$fileName}\"\r\n";
                  $header .="Content-Disposition: attachment; filename=\"{$fileName}\"";
                  $header .= $content."\r\n\r\n";
                  $header .= "--".$uid."--";
               echo $header;

                  mail("peter.balinto@goshenlandcapital.com", "Subject: $subject",
                  "",$header );
                  echo "Thank you for your interest joining GoshenLand Capital";
                  }
                }
              else
                {//if "email" is not filled out, display the form
                echo "Error in Sending Email";
                }
  }
else
  {
  echo "Invalid file";
  }




?>




Is This A Good Question/Topic? 0
  • +

Replies To: problem Sending email with attachment in php.

#2 miguel879  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 40
  • Joined: 20-July 12

Re: problem Sending email with attachment in php.

Posted 23 August 2012 - 09:04 PM

The script that I have mentioned below will definitely help you.

<?php 
function mail_attachment(, , , , ,, , ) { 
     = .; 
     = filesize(); 
     = fopen(, "r"); 
     = fread(, ); 
    fclose(); 
     = chunk_split(base64_encode()); 
     = md5(uniqid(time())); 
     = basename(); 
     = "From: ".." <"..">rn"; 
     .= "Reply-To: ".."rn"; 
     .= "MIME-Version: 1.0rn"; 
     .= "Content-Type: multipart/mixed; boundary=""..""rnrn"; 
     .= "This is a multi-part message in MIME format.rn"; 
     .= "--".."rn"; 
     .= "Content-type:text/plain; charset=iso-8859-1rn"; 
     .= "Content-Transfer-Encoding: 7bitrnrn"; 
     .= ."rnrn"; 
     .= "--".."rn"; 
     .= "Content-Type: application/octet-stream; name=""..""rn"; // use diff. tyoes here 
     .= "Content-Transfer-Encoding: base64rn"; 
     .= "Content-Disposition: attachment; filename=""..""rnrn"; 
     .= ."rnrn"; 
     .= "--".."--"; 
    if (mail(, , "", )) { 
        echo = "mail send ... OK"; // or use booleans here 
    } else { 
        echo = "mail send ... ERROR!"; 
    } 
}




hope this help you men...

miguel
Was This Post Helpful? 1
  • +
  • -

#3 Peter0011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-July 10

Re: problem Sending email with attachment in php.

Posted 23 August 2012 - 09:33 PM

yes i tried already that code and it only send ah file name part2.

//test file
    $file = "sdp.jpg";
    
    echo $file;
    
    $file_size = filesize($file);
    echo $file_size;
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $subject="sample";
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: peterbalintojr@gmail.com <peterbalintojr@gmail.com>\r\n";
    $header .= "Reply-To: peterbalintojr@gmail.com\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail("peter.balinto@goshenlandcapital.com",$subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }



View Postmiguel879, on 23 August 2012 - 09:04 PM, said:

The script that I have mentioned below will definitely help you.

<?php 
function mail_attachment(, , , , ,, , ) { 
     = .; 
     = filesize(); 
     = fopen(, "r"); 
     = fread(, ); 
    fclose(); 
     = chunk_split(base64_encode()); 
     = md5(uniqid(time())); 
     = basename(); 
     = "From: ".." <"..">rn"; 
     .= "Reply-To: ".."rn"; 
     .= "MIME-Version: 1.0rn"; 
     .= "Content-Type: multipart/mixed; boundary=""..""rnrn"; 
     .= "This is a multi-part message in MIME format.rn"; 
     .= "--".."rn"; 
     .= "Content-type:text/plain; charset=iso-8859-1rn"; 
     .= "Content-Transfer-Encoding: 7bitrnrn"; 
     .= ."rnrn"; 
     .= "--".."rn"; 
     .= "Content-Type: application/octet-stream; name=""..""rn"; // use diff. tyoes here 
     .= "Content-Transfer-Encoding: base64rn"; 
     .= "Content-Disposition: attachment; filename=""..""rnrn"; 
     .= ."rnrn"; 
     .= "--".."--"; 
    if (mail(, , "", )) { 
        echo = "mail send ... OK"; // or use booleans here 
    } else { 
        echo = "mail send ... ERROR!"; 
    } 
}




hope this help you men...

miguel

Was This Post Helpful? 0
  • +
  • -

#4 Peter0011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-July 10

Re: problem Sending email with attachment in php.

Posted 23 August 2012 - 10:05 PM

It's now working tnx for the help..

View Postmiguel879, on 23 August 2012 - 09:04 PM, said:

The script that I have mentioned below will definitely help you.

<?php 
function mail_attachment(, , , , ,, , ) { 
     = .; 
     = filesize(); 
     = fopen(, "r"); 
     = fread(, ); 
    fclose(); 
     = chunk_split(base64_encode()); 
     = md5(uniqid(time())); 
     = basename(); 
     = "From: ".." <"..">rn"; 
     .= "Reply-To: ".."rn"; 
     .= "MIME-Version: 1.0rn"; 
     .= "Content-Type: multipart/mixed; boundary=""..""rnrn"; 
     .= "This is a multi-part message in MIME format.rn"; 
     .= "--".."rn"; 
     .= "Content-type:text/plain; charset=iso-8859-1rn"; 
     .= "Content-Transfer-Encoding: 7bitrnrn"; 
     .= ."rnrn"; 
     .= "--".."rn"; 
     .= "Content-Type: application/octet-stream; name=""..""rn"; // use diff. tyoes here 
     .= "Content-Transfer-Encoding: base64rn"; 
     .= "Content-Disposition: attachment; filename=""..""rnrn"; 
     .= ."rnrn"; 
     .= "--".."--"; 
    if (mail(, , "", )) { 
        echo = "mail send ... OK"; // or use booleans here 
    } else { 
        echo = "mail send ... ERROR!"; 
    } 
}




hope this help you men...

miguel

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1