Showing posts with label mail in php. Show all posts
Showing posts with label mail in php. Show all posts

Tuesday, October 27, 2015

PHP mail with attachment - Multiple or malformed newlines found in additional_header

Original (now broken code) below:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {

    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));

    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";

    if (mail($mailto, $subject, "", $header)) {
        echo "Mail Sent Successfully to " . $mailto ."<br/>"; // or use booleans here
    } else {
        echo "Mail NOT Sent to " .$mailto ."<br/>";
    }

See the changes made to get this working with our webservers (highlighted in blue):

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
(THIS ENTIRE LINE HAS BEEN CHANGED)
    $nmessage = "--".$uid."\r\n";
    $nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $nmessage .= $message."\r\n\r\n";
    $nmessage .= "--".$uid."\r\n";
    $nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
    $nmessage .= "Content-Transfer-Encoding: base64\r\n";
    $nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $nmessage .= $content."\r\n\r\n";
    $nmessage .= "--".$uid."--";
error_reporting(E_ALL);
    if (mail($mailto, $subject, $nmessage, $header)) {
        echo "Mail Sent Successfully to " . $mailto ."<br/>"; // or use booleans here
    } else {
        echo "Mail NOT Sent to " .$mailto ."<br/>";
    }

}

I hope this helps someone . Thanks to mindofdes to share the code. :)

Tuesday, May 21, 2013

Send mail with multiple attachments in php

Following these two steps to send multiple attachments in mail.

Step 1: 

<html>  
<head>  
<title>How to send mail in PHP with multiple attachments</title>  
</head>  
<body>    
<form name="sendmail" action="sendmail.php" method="post" enctype="multipart/form-data">  
    <label>E-mail ID:</label>  
    <input name="email" type="text" /><br />  
  
    <label>Subject:</label>  
    <input name="subject" type="text" /><br />  
  
    <label>Body:</label><br />  
    <textarea name="body" rows="4"></textarea><br />  
  
    <label>Attachments:</label><br />  
    <input name="file1" type="file" /><br />  
    <input name="file2" type="file" /><br />  
    <input name="file3" type="file" /><br />  
  
    <input name="submit" type="submit" value="Send Mail" />  
</form>    
</body>  
</html> 

Step 2:  (sendmail.php)

<?php  
$from = "YOUR FROM EMAIL";  
$to = $_POST['email'];  
$subject =$_POST['subject'];  
$message = $_POST['body'];  
  
// Temporary paths of selected files  
$file1 = $_FILES['file1']['tmp_name'];  
$file2 = $_FILES['file2']['tmp_name'];  
$file3 = $_FILES['file3']['tmp_name'];  
  
// File names of selected files  
$filename1 = $_FILES['file1']['name'];  
$filename2 = $_FILES['file2']['name'];  
$filename3 = $_FILES['file3']['name'];  
  
// array of filenames to be as attachments  
$files = array($file1, $file2, $file3);  
$filenames = array($filename1, $filename2, $filename3);  
  
// include the from email in the headers  
$headers = "From: $from";  
  
// boundary  
$time = md5(time());  
$boundary = "==Multipart_Boundary_x{$time}x";  
  
// headers used for send attachment with email  
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";  
  
// multipart boundary  
$message = "--{$boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  
$message .= "--{$boundary}\n";  
  
// attach the attachments to the message  
for($x = 0; $x < count($files); $x++) {  
    $file = fopen($files[$x],"r");  
    $content = fread($file,filesize($files[$x]));  
    fclose($file);  
    $content = chunk_split(base64_encode($content));  
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";  
    $message .= "--{$boundary}\n";  
}  
  
// sending mail  
$sendmail = mail($to, $subject, $message, $headers);  
  
// verify if mail is sent or not  
if ($sendmail) {  
    echo "Mail Sent successfully!";  
} else {  
    echo "Error occurred!!!";  
}  
?> 

Now you learn how to send multiple attachment send in mail using php in simply two steps. special thanks to codegambler.com . Cheers!!!!!