Web Design Forum: How to send HTML emails with multiple attachments in PHP - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to send HTML emails with multiple attachments in PHP It's easy Rate Topic: -----

#1 User is offline   Barth 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 30
  • Joined: 26-July 10
  • Reputation: 7
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 03 August 2011 - 06:49 PM

As the current guides on the web seem to overcomplicate how to do this, I thought I’d try to make a simpler guide, using a single function in php. Please note that this script doesn't include any validation, all this does is send the email.


This is an example of how to use the function


Starting off with the basic details, pretty much self-explanatory

$RecipientEmail = "joebloggs@example.com"; 

$RecipientName = "Joe Bloggs";

$SenderEmail = "fredbloggs@example.com"; 

$SenderName = "Fred Bloggs";

$subject = "lorem ipsum"; 



You can specify multiple emails with cc and bcc, the emails must be separated with commas

$cc = "alice@example.com, bob@example.com"; 

$bcc = "johndoe@example.com";



Set the type as blank for the message to be HTML (recommended), or set it as plain for plain text

// For HTML
$type = "";
// For plain text
$type = "plain";



Your message can contain UTF-8 characters, so this will allow you to send emails in multiple languages
(Just using "lorem ipsum" in this example)

$message = "<b>Sed urna odio, imperdiet a semper ut, fermentum ut tellus.</b> 
Quisque tempor venenatis quam, ut porttitor lacus pretium sit amet.";



In addition you can change the priority of the email

// For normal leave it blank
$priority = "";

// For high use "high"
$priority = "high";

// For low use "low"
$priority = "low";




The attachments

Attachments are specified in an array, in the format location:type:name

The reason for having both location and name in the attachments is simple. It is better to have the name mountains.jpg instead of IMG_0001.JPG appear when the recipient opens the email.

Colons have to be used to separate each section, and the location cannot be external (i.e. another website)


You can do it like this

$attachments = array("attachments/01.jpg:image/jpeg:one.jpg", 
                     "attachments/01.jpg:image/jpeg:two.jpg", 
                     "attachments/03.jpg:image/jpeg:three.jpg");



Or like this (Both work exactly the same)

$attachments[0] = "attachments/01.jpg:image/jpeg:one.jpg";
$attachments[1] = "attachments/02.jpg:image/jpeg:two.jpg";
$attachments[2] = "attachments/03.jpg:image/jpeg:three.jpg";
// And so on...



If you don't need attachments, just leave it blank

$attachments = "";



Introducing the function


Here is a clear layout, just showing the variables from above being passed into the function

$sent = Email($RecipientEmail, 
              $RecipientName, 
              $SenderEmail, 
              $SenderName, 
              $cc, 
              $bcc, 
              $subject, 
              $message, 
              $attachments, 
              $priority, 
              $type);

echo $sent ? "It worked!!" : "It didn't work"; 
// ^ This is used to test if it worked


The full function

I’ve put it into a block of full code so it’s easier to copy, I’ve included comments to try and explain how it works. I recommend that you put this somewhere separate and name it something like “email.function.php”, and include it in your main code.

e.g.

include('email.function.php');


Without further ado the full function…


// This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License

function Email($remail, $rname, $semail, $sname, $cc, $bcc, $subject, $message, $attachments, $priority, $type)  {

// Checks if carbon copy & blind carbon copy exist
if($cc != null){$cc="CC: ".$cc."\r\n";}else{$cc="";}
if($bcc != null){$bcc="BCC: ".$bcc."\r\n";}else{$bcc="";}

// Checks the importance of the email
if($priority == "high"){$priority = "X-Priority: 1\r\nX-MSMail-Priority: High\r\nImportance: High\r\n";}
elseif($priority == "low"){$priority = "X-Priority: 3\r\nX-MSMail-Priority: Low\r\nImportance: Low\r\n";}
else{$priority = "";}

// Checks if it is plain text or HTML
if($type == "plain"){$type="text/plain";}else{$type="text/html";}

// The boundary is set up to separate the segments of the MIME email
$boundary = md5(@date("Y-m-d-g:ia"));

// The header includes most of the message details, such as from, cc, bcc, priority etc. 
$header = "From: ".$sname." <".$semail.">\r\nMIME-Version: 1.0\r\nX-Mailer: PHP\r\nReply-To: ".$sname." <".$semail.">\r\nReturn-Path: ".$sname." <".$semail.">\r\n".$cc.$bcc.$priority."Content-Type: multipart/mixed; boundary = ".$boundary."\r\n\r\n";    
  
// The full message takes the message and turns it into base 64, this basically makes it readable at the recipients end
$fullmessage .= "--".$boundary."\r\nContent-Type: ".$type."; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\n".chunk_split(base64_encode($message));

// A loop is set up for the attachments to be included.
if($attachments != null) {
  foreach ($attachments as $attachment)  {
    $attachment = explode(":", $attachment);
    $fullmessage .= "--".$boundary."\r\nContent-Type: ".$attachment[1]."; name=\"".$attachment[2]."\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment\r\n\r\n".chunk_split(base64_encode(file_get_contents($attachment[0])));
  }
}

// And finally the end boundary to set the end of the message
$fullmessage .= "--".$boundary."--";

return mail($rname."<".$remail.">", $subject, $fullmessage, $header);
}


Thanks, if I’ve missed out anything please let me know, and I hope that this helps :)
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users