PHP - sending email with attachment does not show message content -
trying create script can send email attachments. works except when don't add file in email can still see attachment 0b , no name.
if(isset($_post["my_send"])){ $email_to = $_post['my_email_to']; //required $email_from = "myemail@example.co.uk"; // required $subject = $_post['my_subject']; // not required $comments = $_post['write_my_email']; // required $email_message .= stripcslashes("$comments"); $attachment = chunk_split(base64_encode(file_get_contents($_files['file']['tmp_name']))); $filename = $_files['file']['name']; // create email headers $headers = 'from: '.$email_from."\r\n". $headers .= "mime-version: 1.0\r\n"; $headers .= "content-type: multipart/mixed; boundary=\"_1_$boundary\"\r\n"; 'reply-to: '.$email_from."\r\n" . 'x-mailer: php/' . phpversion(); $message="this multi-part message in mime format. --_1_$boundary content-type: multipart/alternative; boundary=\"_2_$boundary\" --_2_$boundary content-type: text/plain; charset=\"iso-8859-1\" content-transfer-encoding: 7bit $email_message --_2_$boundary-- --_1_$boundary content-type: application/octet-stream; name=\"$filename\" content-transfer-encoding: base64 content-disposition: attachment $attachment --_1_$boundary--"; mail($email_to, $subject, $message, $headers); echo "<h5>thanks, email sent successfully!</h5>"; }
what doing wrong? advice?
just exclude when there no attachment:
--_1_$boundary content-type: application/octet-stream; name=\"$filename\" content-transfer-encoding: base64 content-disposition: attachment $attachment --_1_$boundary--
for example, appending $message
when filename not empty:
$message = "this multi-part message in mime format. --_1_$boundary content-type: multipart/alternative; boundary=\"_2_$boundary\" --_2_$boundary content-type: text/plain; charset=\"iso-8859-1\" content-transfer-encoding: 7bit $email_message"; if (!empty($filename)) { $message .= " --_1_$boundary content-type: application/octet-stream; name=\"$filename\" content-transfer-encoding: base64 content-disposition: attachment $attachment --_1_$boundary--"; }
Comments
Post a Comment