PHP Classes

Send a email with joint file

Recommend this page to a friend!

      SMTP E-mail sending class  >  All threads  >  Send a email with joint file  >  (Un) Subscribe thread alerts  
Subject:Send a email with joint file
Summary:An example to send a email + joint file with this class
Author:Laurent GAY
Date:2012-01-14 18:30:57
Update:2012-01-15 01:50:17

  1. Send a email with joint file   Reply   Report abuse  
Picture of Laurent GAY Laurent GAY - 2012-01-14 18:30:58

I am use those class in my project an it run perfectly.
I propose a little example to add an joint file to an email:

function sendEMail($from,$to,$subject,
$body /* Test body of your email */,
$fileName /*file name propose in email*/,
$contentFile /*string content of file to send*/) {

$smtp=new smtp_class;
$smtp->host_name=""; /* change with your own server */
$random_hash = md5(date('r', time()));

$Conttype='Content-Type: multipart/mixed; boundary="'.$random_hash.'"';
$extendbody.="Content-Type: text/plain;charset=iso-8859-1\r\n\n";

$finfo = new finfo(FILEINFO_MIME_TYPE);
$extendbody.='Content-Type: '.$finfo->buffer($contentFile).'; name="'.$fileName.'"'."\r\n";
$extendbody.="Content-Transfer-Encoding: base64\r\n";
$extendbody.='Content-Disposition: attachment; filename="'.$fileName.'"'."\r\n\n";


$ret=$smtp->SendMessage($from,explode(',',$to),array("From: $from","To: $to","Subject: $Subject",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z"),$Conttype),$extendbody);

if (!$ret)
throw new Exception($smtp->error);

  2. Re: Send a email with joint file   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-01-15 01:50:17 - In reply to message 1 from Laurent GAY
The SMTP class does not compose message bodies, it just sends the message to a SMTP server.

For composing messages with attached files, you need to use a message composing class like the MIME message.

It comes with a sub-class named smtp_message_class that can use the SMTP class for delivery.

Take a look at the test_smtp_message.php and test_attachment_message.php example scripts.