Php send email with application

I cannot find a problem with this php function that I wrote to send an email with the application. I struggled with this for a long time.

function myMail($to, $subject, $mail_msg, $filename, $contentType){ $random_hash = md5(date('r', time())); $headers = "From: webmaster@example.com \r\nReply-To: ".$to; $headers .= "\r\nContent-Type: ".$contentType. "; boundary=\"PHP-mixed-".$random_hash."\""; $attachment = chunk_split(base64_encode(file_get_contents($filename))); ob_start(); echo " --PHP-mixed-$random_hash Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\" --PHP-alt-$random_hash Content-Type: text/plain; charset=\"utf-8\" Content-Transfer-Encoding: 7bit $mail_msg --PHP-alt-$random_hash --PHP-mixed-$random_hash-- Content-Type: text/plain; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash-- "; $message = ob_get_clean(); $mail_sent = @mail( $to, $subject, $message, $headers ); return $mail_sent ? "Mail sent" : "Mail failed"; } 

Edit The problem is that the mail message is mixed with the file and sent as an attachment.

+2
source share
4 answers

Artefacto made me look at the result with great attention, and I found a fix:

  function myMail ($ to, $ subject, $ mail_msg, $ filename, $ contentType, $ pathToFilename) {
     $ random_hash = md5 (date ('r', time ()));
     $ headers = "From: webmaster@mysite.com \ r \ nReply-To:". $ to;
     $ headers. = "\ r \ nContent-Type: multipart / mixed; boundary = \" PHP-mixed - ". $ random_hash." \ "";
     $ attachment = chunk_split (base64_encode (file_get_contents ($ pathToFilename)));
     ob_start ();
  echo "
 --PHP-mixed- $ random_hash
 Content-Type: multipart / alternative;  boundary = \ "PHP-alt- $ random_hash \"

 --PHP-alt- $ random_hash
 Content-Type: text / plain;  charset = \ "utf-8 \"
 Content-Transfer-Encoding: 7bit

 $ mail_msg

 --PHP-alt- $ random_hash--

 --PHP-mixed- $ random_hash
 Content-Type: $ contentType;  name = \ "$ filename \" 
 Content-Transfer-Encoding: base64 
 Content-Disposition: attachment 

 $ attachment
 --PHP-mixed- $ random_hash--
 ";
 $ message = ob_get_clean ();
 $ fh = fopen ('log.txt', 'w');
 fwrite ($ fh, $ message);
 $ mail_sent = @mail ($ to, $ subject, $ message, $ headers);
 return $ mail_sent?  "Mail sent": "Mail failed";
 }
+6
source

I just looked through a couple of my letters, and I noticed that the final attachment border ends with a “-”, but the opening border marker does not. In your code you have:

 --PHP-mixed-$random_hash-- Content-Type: text/plain; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash-- 

Perhaps this should be:

 --PHP-mixed-$random_hash Content-Type: text/plain; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash-- 

See an example here:

http://en.wikipedia.org/wiki/MIME#Multipart_messages

+10
source

If you do not learn the internal workings of MIME emails, the standard answer would be to use a mailing list library such as PHPMailer or Swiftmailer , which can handle attachments out of the box.

SwiftMailer examples for attaching files here .

+3
source

These are the headers I use, and they always worked like a charm.

 $base = basename($_FILES['upload']['name']); $file = fopen($randname_path,'rb'); $size = filesize($randname_path); $data = fread($file,$size); fclose($file); $data = chunk_split(base64_encode($data)); //boundary $div = "==Multipart_Boundary_x".md5(time())."x"; //headers $head = "From: $from\n". "MIME-Version: 1.0\n". "Content-Type: multipart/mixed;\n". " boundary=\"$div\""; //message $mess = "--$div\n". "Content-Type: text/plain; charset=\"iso-8859-1\"\n". "Content-Transfer-Encoding: 7bit\n\n". "$message\n\n". "--$div\n". "Content-Type: application/octet-stream; name=\"$base\"\n". "Content-Description: $base\n". "Content-Disposition: attachment;\n". " filename=\"$base\"; size=$size;\n". "Content-Transfer-Encoding: base64\n\n". "$data\n\n". "--$div\n"; $return = "-f$from"; 

http://asdlog.com/Create_form_to_send_email_with_attachment

+1
source

All Articles