Function that sends emails with attachment? - php

Is there a PHP class/script (opensource/free) that allows me to send emails with attached files?
I want something as simple as
mail_with_attachment($to, $subject, $_FILES["file"]);
Is there such thing available?

There are:
http://framework.zend.com/manual/en/zend.mail.attachments.html
http://www.php.net/manual/en/function.mail.php#105661
http://sourceforge.net/projects/phpmailer/
http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en#sec_13
http://xpertmailer.sourceforge.net/documentation/
http://swiftmailer.org/docs/messages.html
Choose one.

Something like this may get the job done:
<?php
function multi_attach_mail($to, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
$subject = date("d.M H:i")." F=".count($files);
$message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
?>
I got that from the comments section of the mail function's page on php.net. You can go there to see more examples of similar functions.

I use SwiftMailer library to do this: http://swiftmailer.org/
Here is quick example: http://swiftmailer.org/docs/messages.html

Related

Empty attachment using PHP mailer

I am trying to send a mail with a attachment using PHP. When I send the mail I am recieving it with a empty attachment (the attachment file size = 0 bytes). The filepath that I am using is correct.
Does someone know what is wrong with my script. Why am I recieving an empty attachment?
Here is my script:
$to = $destination;
$from = 'test#test.test';
$fromName = 'Test';
$subject = $subject;
$file = "./image-based-pdf-sample.pdf";
$htmlContent = $content;
$headers = "From: $fromName"." <".$from.">";
$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}\"";
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\r\n" . $htmlContent . "\r\n";
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file,"rb");
$data = #fread($fp,filesize($file));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\r\n" . $data . "\r\n";
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$mail = #mail($to, $subject, $message, $headers, $returnpath);
echo $mail?"<h1>Success</h1>":"<h1>Error</h1>";

Multiple or malformed newlines found in additional_header using mail() in php

trying to send mail using native mail() but its showing error in the header part
"Message: mail(): Multiple or malformed newlines found in
additional_header".
It is showing an error in the last line of the code which is:
$mail = mail($email, $subject, $message, $headers, $returnpath);
code:
$email = $id;
$subject = $mydetails_name.' had sent Contact Details';
$message = $html;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: CloudBigTechnology' . "\r\n";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
if(!empty($file_element_name['name'])) {
$filesCount = count($explodedString);
for($x=0;$x<count($explodedString);$x++) {
$file_namex = basename($file_element_name['name'][$x]);
$file_sizex = filesize($file_element_name['tmp_name'][$x]);
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file_element_name['tmp_name'][$x], "rb");
$datam = #fread($fp, $file_sizex);
#fclose($fp);
$datam = chunk_split(base64_encode($datam));
$message .= "Content-Type: application/octet-stream; name=\"".$file_namex."\"\n" .
"Content-Description: ".$file_namex."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".$file_namex."\"; size=".$file_sizex.";\n" .
"Content-Transfer-Encoding: base64\n\n" . $datam . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $email;
$mail = mail($email, $subject, $message, $headers, $returnpath);
How to fix it?

attachment issue using php mail() function

I'm using this code for sending mixed content(text with html + attachment):
function multi_attach_mail($to, $subject, $message1, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
$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}\"";
$message1 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message1 . "\n\n";
$message1 .= "--{$mime_boundary}\n";
$fp = #fopen($files,"rb");
$data = #fread($fp,filesize($files));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message1 .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"\n" .
"Content-Description: ".basename($files)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files)."\"; size=".filesize($files).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message1 .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
$mail = mail($to, $subject, $message1, $headers, $returnpath);
if($mail){ return TRUE; } else { return FALSE; }
}
It sends email successfully, attaches file, but attachment content isn't available and once downloaded it says that file can be damaged. I guess something is wrong with my headers but can't understand exactly what is the issue.
I'm using wordpress and this function is inside theme folder's functions.php file.

Sending multiple Attachment files in php mail function

I have got some piece of code to send multiple attachments through mail function in php.
The mail with attachment will be sent if there is only one attachment.
But mail sending fails if add two or more files in the array.
This is my code
// array with filenames to be sent as attachment
//$files = array("upload/Tulip.jpg");
$files = array("upload/Tulip.jpg","upload/Tulips.jpg","upload/Tulips1.jpg");
// email fields: to, from, subject, and so on
$to = "mail#mail.com";
$from = "mail#mail.com";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
The mail will be sent if $files array have only one file..
Please someone help me in solving this.
Please Remember , the above code works fine if we have only one image path in $files array.
Thanks in advance

how to remove directory name from the attachment name in php

i have a small issue. i had upload a screen shot of a email that has an attachment sent by using php.but in the attachment name there is the directory name as "upload./" i want to remove that part. how can i do that?
Thank you.
$message = $mes;
echo $message;
//echo $message;
$subject = $coursetitle;
$headers .= 'Cc: gayani#amdt.lk' . "\r\n";
ini_set("SMTP", "mail.amdt.lk");
ini_set("sendmail_from", "info#amdt.lk");
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$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" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($dir.$files[$x],"rb");
$data = fread($file,filesize($dir.$files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$dir.$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$dir.$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
//echo $message;
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
You're doing it yourself...
"Content-Disposition: attachment;\n" . " filename=\"$dir.$files[$x]\"\n"
^^^^
that's the filename as it should appear in the client browser... it has nothing to do with where the file is on your server, or what its name is on your server. That's purely for the remote user's information.
In the greater scheme of things... don't build your own mime messages, or use mail(). Mime is painful to get right, when there's perfectly good libraries like PHPMailer and Swiftmailer that do all of that for you with far less code.
Use basename() function:
$file="upload./file.jpg";
$basename=basename($file);
$dirname=dirname($file);
$string = str_replace("upload./", "", $string);
Thats all :)

Categories