Published on : 05.05.2010 Category : PHP Classes Viewed : 35 times.
You can send emails very easily with PHP by the help of mail() function, but how does one send attachment attached to that email?
You’ll still use mail() function, but with a little more twists in the header argument – yes, the content of the attached file is actually encoded into the header of the email, can you believe that. Big head.
Unfortunately, it’s a pain in the ass every time you have to get through all the chores needed to attach and encode the file into the email header. You have to know the size of the file, base64_encode() it, chunk_split() it and set all the configurations required in the header so that the email client can recognize that something’s in the header and can extract it out as an attachment.
Anyway, I’ve made this class AttachmentEmail that you can email an attachment with PHP more easily. All the chores are taken care of. It’s written in a hurry so the structure can sure be better. Other than this, it works like a charm. The Class:
Code:
class AttachmentEmail { private $from = 'yours@email.com'; private $from_name = 'Your Name'; private $reply_to = 'yours@email.com'; private $to = ''; private $subject = ''; private $message = ''; private $attachment = ''; private $attachment_filename = '';
public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') { $this -> to = $to; $this -> subject = $subject; $this -> message = $message; $this -> attachment = $attachment; $this -> attachment_filename = $attachment_filename; }
public function mail() { if (!empty($this -> attachment)) { $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ; $path = dirname($this -> attachment); $mailto = $this -> to; $from_mail = $this -> from; $from_name = $this -> from_name; $replyto = $this -> reply_to; $subject = $this -> subject; $message = $this -> 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.">rn"; $header .= "Reply-To: ".$replyto."rn"; $header .= "MIME-Version: 1.0rn"; $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn"; $header .= "This is a multi-part message in MIME format.rn"; $header .= "--".$uid."rn"; $header .= "Content-type:text/plain; charset=iso-8859-1rn"; $header .= "Content-Transfer-Encoding: 7bitrnrn"; $header .= $message."rnrn"; $header .= "--".$uid."rn"; $header .= "Content-Type: application/octet-stream; name="".$filename.""rn"; // use diff. tyoes here $header .= "Content-Transfer-Encoding: base64rn"; $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn"; $header .= $content."rnrn"; $header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) { return true; } else { return false; } } else { $header = "From: ".($this -> from_name)." <".($this -> from).">rn"; $header .= "Reply-To: ".($this -> reply_to)."rn";
if (mail($this -> to, $this -> subject, $this -> message, $header)) { return true; } else { return false; }
} } }
The Usage:
For example, there’s this file residing on your web server at the path: /home/racker/gift.jpg and you want it attached to an email you are sending to Marry:
Code:
$sendit = new AttachmentEmail('marry@example.com', 'Merry Christmas!', 'Hi', '/home/racker/gift.jpg'); $sendit -> mail();
The last argument ($attachment_filename) can be left empty if a filename is included in the second to last argument ($attachment). Otherwise you have to make clear what the file name is so the class can grab it and send it via attachment.
It returns true if the delivery is successful, otherwise false.
|