webmaster1 Posted January 11, 2009 Share Posted January 11, 2009 Hi All, I'm using a basic chunk of code to send an email with an attached PDF file. The problem is email message isn't being sent. <?php $fileatt = "cat_scan.pdf"; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = "cat_scan.pdf"; // Filename that will be used for the file as the attachment $email_from = "[email protected]"; // Who the email is from $email_subject = "Test subject"; // The Subject of the email $email_txt = "This is a test email."; // Message that the email has in it $email_to = "[email protected]"; // Who the email is too $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $ok = @mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "<font face=verdana size=2>The file was successfully sent!</font>"; } else { die("Sorry but the email could not be sent. Please go back and try again!"); } ?> Can anybody spot whats causing the problem? I can't see where $email_txt is being called on again. Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/ Share on other sites More sharing options...
gevans Posted January 11, 2009 Share Posted January 11, 2009 First of all cahnge this; $ok = @mail($email_to, $email_subject, $email_message, $headers); to this $ok = mail($email_to, $email_subject, $email_message, $headers); and see what error you get back Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734757 Share on other sites More sharing options...
webmaster1 Posted January 11, 2009 Author Share Posted January 11, 2009 I've removed the @ symbol though it hasn't made a difference. Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734761 Share on other sites More sharing options...
gevans Posted January 11, 2009 Share Posted January 11, 2009 So you just get Sorry but the email could not be sent. Please go back and try again! on the screen? Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734762 Share on other sites More sharing options...
webmaster1 Posted January 11, 2009 Author Share Posted January 11, 2009 Nope. It sends fine. "The file was succesfully sent" Everything sends except for the desired message of the email. I can't tell whether it comes from $email_txt or $email_message but I can confirm that neither are sending. Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734766 Share on other sites More sharing options...
gevans Posted January 11, 2009 Share Posted January 11, 2009 Ok change this $ok = @mail($email_to, $email_subject, $email_message, $headers); to this; <?php echo '1: '.$email_to.'<br />'; echo '2: '.$email_subject.'<br />'; echo '3: '.$email_message.'<br />'; echo '4: '.$headers.'<br />'; exit(); $ok = @mail($email_to, $email_subject, $email_message, $headers); ?> And post what's printed on the screen Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734769 Share on other sites More sharing options...
webmaster1 Posted January 11, 2009 Author Share Posted January 11, 2009 Its longwinded: 1: [email protected] 2: Test subject 3: This is a multi-part message in MIME format. --==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x Content-Type:text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x Content-Type: application/octet-stream; name="cat_scan.pdf" Content-Transfer-Encoding: base64 JVBERi0xLjQNJeLjz9MNCjQgMCBvYmogPDwvTGluZWFyaXplZCAxL0wgMTM4MjcyL08gNi9FIDc5 MTE0L04gMS9UIDEzODE1MS9IIFsgNjM2IDE1Nl0+Pg1lbmRvYmoNICAgICAgICAgICAgICAgICAg IA14cmVmDTQgMTcNMDAwMDAwMDAxNiAwMDAwMCBuDQowMDAwMDAwNzkyIDAwMDAwIG4NCjAwMDAw MDA4NTEgMDAwMDAgbg0KMDAwMDAwMTM2MCAwMDAwMCBuDQowMDAwMDAxNjgyIDAwMDAwIG4NCjAw MDAwMDI3NDIgMDAwMDAgbg0KMDAwMDAwMzcxMiAwMDAwMCBuDQowMDAwMDA0NjQ0IDAwMDAwIG4N [i'VE EDITED OUT A COUPLE OF HUNDRED LINES HERE BECAUSE IT WAS EXCEEDING THE CHARACHTER LIMIT TO POST] NScwMCcpL1RpdGxlKGNhdF9zY2FuKT4+DWVuZG9iag14cmVmDTAgNA0wMDAwMDAwMDAwIDY1NTM1 IGYNCjAwMDAwNzkxMTQgMDAwMDAgbg0KMDAwMDA3OTE2NCAwMDAwMCBuDQowMDAwMTM3OTczIDAw MDAwIG4NCnRyYWlsZXINPDwvU2l6ZSA0Pj4Nc3RhcnR4cmVmDTExNg0lJUVPRg0= --==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x-- 4: From: [email protected] MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x" Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734772 Share on other sites More sharing options...
gevans Posted January 11, 2009 Share Posted January 11, 2009 try this <?php $fileatt = "cat_scan.pdf"; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = "cat_scan.pdf"; // Filename that will be used for the file as the attachment $email_from = "[email protected]"; // Who the email is from $email_subject = "Test subject"; // The Subject of the email $email_txt = "This is a test email."; // Message that the email has in it $email_to = "[email protected]"; // Who the email is too $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n" . $email_txt; $ok = @mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "<font face=verdana size=2>The file was successfully sent!</font>"; } else { die("Sorry but the email could not be sent. Please go back and try again!"); } ?> Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734774 Share on other sites More sharing options...
webmaster1 Posted January 11, 2009 Author Share Posted January 11, 2009 Same result. Whats did you try? Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734777 Share on other sites More sharing options...
gevans Posted January 11, 2009 Share Posted January 11, 2009 I tried adding the message to the $email_message variable. I'm sorry this isn't one of my strong points, but check out this tutorial, maybe it can help you tutorial Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734778 Share on other sites More sharing options...
webmaster1 Posted January 11, 2009 Author Share Posted January 11, 2009 No worries Gevans. I'll try this and a few other scripts. I'll leave this open in case anyone can spot whats wrong with the code I submitted in the first post. Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734784 Share on other sites More sharing options...
webmaster1 Posted January 11, 2009 Author Share Posted January 11, 2009 That worked perfectly with very little modification required. Thanks G. <?php //define the receiver of the email $to = '[email protected]'; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: [email protected]\r\nReply-To: [email protected]"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; //read the atachment file contents into a string, //encode it with MIME base64, //and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> Link to comment https://forums.phpfreaks.com/topic/140398-solved-pdf-email-attachment-working-fine-but-message-doesnt-send/#findComment-734799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.