Adeus Posted May 6, 2008 Share Posted May 6, 2008 Hi, I'm trying to get a form to send an email which includes an attachment AND a message in the body. I can get one or the other, but for some reason I can not figure out how to send both. Here is my attempt: <?php if (isset($_POST['submit'])) { $to = $_POST['email']; $from = $_POST['uemail']; $subject = $_POST['pdfname']; $message = $_POST['message']; $pdf = $_POST['pdf']; //PDF/name.pdf $filename = array(0=>array('file'=>$pdf,'mimetype'=>'application/pdf','filename'=>$pdfname)); function mail_attached($to, $from, $subject, $message, $filename, $headers = '') { $unique_sep = md5(uniqid(time())); $headers .= "From: $from\n"; $headers .= "MIME-Version: 1.0\nContent-Type:" ." multipart/mixed;boundary=\"$unique_sep\";\n"; $headers .= "charset=\"iso-8859-1\"\nContent-Transfer-Encoding:" ."7bit\n\n"; $headers .= "--$unique_sep\n"; $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $headers .= "Content-Transfer-Encoding: 7bit\n\n"; $headers .= $message."\n\n"; $headers .= "--$unique_sep--"; if(is_array($filename)) { foreach($filename as $val) { if(file_exists($val['file'])) { $headers .= "--$unique_sep\n"; $headers .= "Content-Type: {$val['mimetype']}; "."name=\"{$val['filename']}\"\n"; $headers .= "Content-Transfer-Encoding: "."base64\n"; $headers .= "Content-Disposition: attachment\n\n"; $filedata = implode(file($val['file']), ''); $headers .= chunk_split(base64_encode($filedata)); } else { echo "Error: File doesn't exist."; } } } else { echo "Error: Invalid parameter passed."; } $headers .= "--$unique_sep--\n"; } mail($to, $subject, $message, $headers); echo "Sent."; } ?> This sends the message, but not the attachment. I was somewhat following the guide at http://www.theukwebdesigncompany.com/articles/php-file-attachments.php. All of the variables are passing correctly. Any help is much appreciated! Link to comment https://forums.phpfreaks.com/topic/104324-php-mail-mime-attachment-and-text/ Share on other sites More sharing options...
Adeus Posted May 6, 2008 Author Share Posted May 6, 2008 D'oh! Solved it... I used a different reference. This works, from http://www.weberdev.com/get_example-4595.html : <?php if (isset($_POST['submit'])) { $to = $_POST['email']; $from = $_POST['uemail']; $subject = $_POST['pdfname']; $message = $_POST['message']; $pdf = $_POST['pdf']; $fileatttype = "application/pdf"; $fileattname = $_POST['pdfname']; $fp = fopen($pdf, "rb"); $file = fread($fp, filesize($pdf)); $file = chunk_split(base64_encode($file)); $num = md5(time()); //Normal headers $headers = "From: ".$from."\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed; "; $headers .= "boundary=".$num."\r\n"; $headers .= "--$num\r\n"; // This two steps to help avoid spam $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; // With message $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 8bit\r\n"; $headers .= "".$message."\n"; $headers .= "--".$num."\n"; // Attachment headers $headers .= "Content-Type:application/pdf"; $headers .= "name=\"".$fileattname."\"r\n"; $headers .= "Content-Transfer-Encoding: base64\r\n"; $headers .= "Content-Disposition: attachment; "; $headers .= "filename=\"".$fileattname."\"\r\n\n"; $headers .= "".$file."\r\n"; $headers .= "--".$num."--"; // SEND MAIL mail($to, $subject, $message, $headers); fclose($fp); } ?> Link to comment https://forums.phpfreaks.com/topic/104324-php-mail-mime-attachment-and-text/#findComment-534140 Share on other sites More sharing options...
Adeus Posted May 7, 2008 Author Share Posted May 7, 2008 Looks like I spoke too soon. I tested this script by sending it to my Gmail account, and it worked perfectly. When I try sending it to a different account (which is set up in Outlook), it sends the attachment but not the message in the body. Any ideas? Link to comment https://forums.phpfreaks.com/topic/104324-php-mail-mime-attachment-and-text/#findComment-535381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.