How to attach HTML file to email using content taken from DB in PHP?

How to send mail through PHP with an HTML file attachment? → The content of the HTML file (code) is located in a row in the database. Is there an easy way or free script for this? I do not want to store the file locally, I need to read it from the database and send it immediately as an attachment (not included in the body).

+2
source share
3 answers

If you find it difficult to get the headers correctly, you can always use something like PHP Mailer instead of reinventing the wheel.

+3
source

I like the 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' => ' someone@domain.pl ', 'To' => ' someone@domain.pl ', '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(' mail@domain.pl ', $hdrs, $body); ?> 
+3
source

You can follow these instructions when sending email attachments. You just need to tweak your code to read a row from the database, and not read the contents of the file.

0
source

All Articles