views:

2511

answers:

3

Hi,

How do I send mail via PHP with attachment of HTML file? -> Content of HTML file (code) is in string in DB. Is there some easy way or free script to do this? I don't want to store the file localy, I need to read it out of DB and send it straightaway as attachment (not included in body).

A: 

You should be able to follow these instructions on sending email attachments. You will simply need to adjust your code to read a string from the database instead of reading the contents of a file.

Noah Goodrich
+3  A: 

If you have a hard time getting the headers right, you can always use something like PHP Mailer instead of reinventing the wheel.

Lucas Oman
+3  A: 

I like pear.

<?
include('Mail.php');
include('Mail/mime.php');
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './files/example.zip';
$crlf = "rn";
$hdrs = array(
              'From'    => '[email protected]',
              'To'      => '[email protected]',
              'Subject' => 'Test mime message'
              );
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file,'application/octet-stream');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail', $params);
$mail->send('[email protected]', $hdrs, $body); 
?>
Georg Zimmer
+1 for the example
cletus