Send Mail With Attachment Using PHP

PHP script to send mail with attachment.

Apart from default php mail function chunk_split, base64_encode and file_get_contents also used in this script.

<?php
$htmlbody = " Your Mail Contant Here.... You can use html tags here...";
$to = "name@domain.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: name@domain.com\r\nReply-To: name@domain.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents('logo.png')));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"logo.png\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed"; ?>

Click here to send send text mail, HTML mail using PHP mail function.


Download Code Total Downloads: 13565
Comments
20 February
2014
Shahbaz Ahmed Bhatti
i need some help can anybody help me please
18 February
2014
sundar
Great coding. Nice explanation. Awesome working.
Thanks for sharing this information. http://www.dreamdestinations.in/
31 December
2013
Luiz Eduardo
It was very useful for me. Good work!
comments powered by Disqus