PHP mail with attachment - additional file: part 1.4

I use the following code to send email with attachments:

$mime_boundary = "<<<--==+X[".md5(time())."]"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed;\r\n"; $headers .= " boundary=\"".$mime_boundary."\""; $message .= "This is a multi-part message in MIME format.\r\n\r\n"; $message .= "--".$mime_boundary."\r\n"; $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; $message .= "Content-Transfer-Encoding: 7bit\r\n"; $message .= "\r\n"; $message .= "$message_body\r\n"; $message .= "--".$mime_boundary."\r\n"; foreach($attachments as $filename => $data) { $message .= "Content-Type: application/octet-stream;\r\n"; $message .= " name=\"$filename\"\r\n"; $message .= "Content-Transfer-Encoding: quoted-printable\r\n"; $message .= "Content-Disposition: attachment;\r\n"; $message .= " filename=\"$filename\"\r\n"; $message .= "\r\n"; $message .= chunk_split(base64_encode($data)); $message .= "\r\n"; $message .= "--".$mime_boundary."\r\n"; } mail($email_address, $email_subject, $message, $headers); 

This works just fine, except that adding an extra file (called "Part 1.4").

Is there any way not to add this?

Cheers, Dan.

+4
source share
1 answer

IIRC, the last part separator should be --something unique-- , i.e. in your case

 $message .= "--".$mime_boundary."--\r\n"; 

But mime mail is a more or less solved problem (i.e. for an application developer, it is boring when done right and reeeeally annoying when done wrong ;-)). Do yourself a favor and use something like Swiftmailer or any other descend library / distribution class.

+5
source

Source: https://habr.com/ru/post/1300504/


All Articles