You are here: Home » PHP » Email with attachment

Email with attachment

38 Flares 38 Flares ×

To create mail with attachment, First you have to upload file.
and you have to define file path of your site or if you want to attach file from your site, you have only define file path.
Use below code and define Recipient, Sender, Subject, Message.
You have to define header to mail.

$subject="Your new mail";
$htmlbody .= '


  <p><b> '.$subject.' </b> </p>

  <table width="750" border="1">

  <tr><th width="300" >Applicant Name</th><td width="450">Test Name</td></tr>

  <tr><th >Applicant Email</th><td>email@test,com</td></tr>

  <tr><th >Project Name</th><td>My Project</td></tr>

  <tr><th >Phone Number</th><td>9874563210/td></tr>

  <tr><th >Message  </th><td>My Message</td></tr>



<p> Thank you!</p>



$textmessage = "Message";
$subject = "New project file";
$headers .= "From: ".$adminemail."\r\nReply-To: ".$adminemail."";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = chunk_split(base64_encode(file_get_contents('image.jpg')));
//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";

$message .= strip_tags($textmessage);
$message .= "\r\n\r\n--PHP-alt-$random_hash\r\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n";

$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
$message .= "--PHP-mixed-$random_hash\r\n"
."Content-Type: application/zip; name=\"".$img."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
 mail($to, $subject , $message, $headers );


About Abhishek Sinha

I am Abhishek and I love to code on PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *


Scroll To Top
38 Flares Twitter 15 Facebook 2 Google+ 2 StumbleUpon 17 Pin It Share 0 LinkedIn 2 38 Flares ×