austin350s10 Posted March 14, 2012 Share Posted March 14, 2012 Does anyone know of a way to use PHP to create and email a HTML file? I was attempting to use the standard PHP mail function to pass a HTML string to the message variable, but mail clients have a pretty hard time rendering HTML/CSS correctly. My thought was, if I could take my HTML string and turn it into a file and attach it to a email message the user would be able to open the file in there default browser which would render without issues. Just for the record my HTML string also contains PHP variables, so it must be processes before being sent. So far my research suggests using the PEAR library for sending mail with attachments, but I am only finding literature on how to attach a file already existing on the server to an email message. I am having trouble finding a way to create a HTML file on the fly and attach it to an email message. Has anyone had a similar problem? Is there a different way to accomplish what I am attempting to do here? Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/ Share on other sites More sharing options...
Muddy_Funster Posted March 14, 2012 Share Posted March 14, 2012 I had something slightly simmilar, I was using XML attachments, but you could use html just as easy. My biggest problem was that I need to send from a mail account that requred SMTP authentication in a WAMP environment, so I had to use the PEAR Net_SMTP class or install some other third party lib's. I posted my final solution as a function at the end of this thread: http://www.phpfreaks.com/forums/index.php?topic=351896.msg1661586#msg1661586 Let me know if it helps Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/#findComment-1327259 Share on other sites More sharing options...
austin350s10 Posted March 14, 2012 Author Share Posted March 14, 2012 That is impressive, but I've never user PEAR before and I'm not totally sure if I need to connect and send using SMTP. The part that I am unsure of is exactly how to transform a string into a HTML file. Do you know how to do something like that? Is it even possible without getting to complicated? Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/#findComment-1327268 Share on other sites More sharing options...
dmikester1 Posted March 14, 2012 Share Posted March 14, 2012 It's actually very easy. Stole this code from tizag.com and modified it accordingly. http://www.tizag.com/phpT/filewrite.php $myFile = "testFile.html"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<html>\n"; $stringData .= "<head>\n"; $stringData .= "</head>\n"; $stringData .= "<body>\n"; $stringData .= "<h1>Hello World</h1>\n"; $stringData .= "</body>\n"; $stringData .= "</html>\n"; fwrite($fh, $stringData); fclose($fh); * code is untested, but should work. ** The \n characters are to put new lines in the code itself. So when you view the source on your page, it won't be all in one line. It would not affect the actual HTML output at all. Mike Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/#findComment-1327273 Share on other sites More sharing options...
Muddy_Funster Posted March 14, 2012 Share Posted March 14, 2012 this section of the code deals with the attachment: if($attachment != null && $fName != null){ //check if there is an attchment being passed into the function and that it has a name $message .= "--".$bound.$e; //add boundary to change to next content type $message .= "Content-type:application/octet-stream;"; //set content type to suitable type for attachment (octet-stream is for generic binary data other are available) $message .= "name=\"$fName.xml\"".$e; //set the file name $message .= "Content-disposition:attachment;"; //use attachment to infer data that is being passed as a file $message .= "filename=\"$fName.xml\";".$e; //set the filename as it will be recieved $message .= "Content-transfer-encoding:base64".$e.$e; // encode the file for transfer $message .= $attachment.$e.$e; // add attachment contents the $fName can be any string that you want for the file name ie $fName = "ClickToViewHTMLMessage.html"; and the $attachment is your actual HTML code: $attachment = <<<HTML_BLOCK <!DOCTYPE html> <html lang="en"> <head> <title></title> </head> </body> <p>Hi, this is my message to $recipientUser</p> <p>You are so <b>super</b> I just had to tell you that you have $number people trying to follow you on some random social networking site</p> </body> </html> HTML_BLOCK; so you could just pass the data into the function when you call it, no need to make files for attachments. Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/#findComment-1327275 Share on other sites More sharing options...
austin350s10 Posted March 14, 2012 Author Share Posted March 14, 2012 I like the last 2 ideas and it seems like either one should work. I will have to play around a bit. Question for Muddy_Funster: As I'm a total noob to PHP and haven't mastered a way to combat spam bots would it be a bad idea for me to use your script as it contains the login credentials for my mail server? Seems like it might be the silver platter hackers might be looking for. What do you think? Question for dmikester1: If I am reading correctly, your script would basically involve putting a dummy file on my server and overwriting it each time it is needed. Would PEAR still be needed to send a attachment in this fashion or would the standard mail function be able to accommodate? Thank you both for your ideas. This issue has been working at me for days! Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/#findComment-1327286 Share on other sites More sharing options...
Muddy_Funster Posted March 14, 2012 Share Posted March 14, 2012 you can pass the login details via a form input. Infact, you can pass all the information via form input if your really want to. I just had the defaults set because it was written for an intratnet page that was sending out over a no-reply address from a controled user page. All the variables that you see in the brackets of the function line will be overwritten if you pass in new ones when you call it: eg - $mailAtempt = sendSMTP("My Subject", "please open the attached file", "myMailUserName", "myMailPassword", "Recipient Name", "[email protected]", "Sender Name", "[email protected]", "myhtml.html", "<html><head><title></title></head><body>This Is My Message</body></html>") Or you can pass in your own $variables, as long as the order matches you can play it however you like. Quote Link to comment https://forums.phpfreaks.com/topic/258899-sending-a-html-file-as-an-email-attachment/#findComment-1327307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.