Login   Register  
PHP Classes
elePHPant
Icontem

Send a email with joint file

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      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
Messages:2
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  
Picture of Laurent GAY
Laurent GAY
2012-01-14 18:30:58
Hi,

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="smpt.phpclasses.org"; /* change with your own server */
$smtp->host_port=25;
$random_hash = md5(date('r', time()));

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

$extendbody.='--'.$random_hash."\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";
$extendbody.=chunk_split(base64_encode($contentFile));

$extendbody.='--'.$random_hash.'--'."\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  
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.

http://www.phpclasses.org/mimemessage