jsw1988eku Posted December 13, 2011 Share Posted December 13, 2011 I have been taking a crash course trying to learn php/html/javascript/jquery and am probably asking a fairly simple question. I installed phpmailer recently and have been able to send mail with attachments without a problem, but I am trying to make the body of the email HTML. Unfortunately each attempt the formatting is not preserved. I tried copying the same style that I use when I generate the same report to a web page, but it isnt translating to an html email. I have included the code that is relevant. Any glaring oversights? $htmlBody = '<html><head><title>Calls Yesterday</title></head>'; $htmlBody .='<style type="text/css"> body { font-family: Arial, Helvetica, sans-serif; font-size: 16px; } th { font-weight: bold; text-align: left; } td, th { padding: 15px 15px 15px 15px; width: 75px; vertical-align: top; text-align: left; } .outputData2 { margin-left: -10px; margin-right: -10px; width: 500px; } .outputData2 th { white-space: nowrap; text-align: left; padding-right: 10px; } .outputData2 td { white-space: nowrap; text-align: left; } a { text-decoration: none; color: #000; } strong { font-weight: bold; } </style> <br />'; $htmlBody .= '<div style="width: 640px; height: 500px; margin: 10px 0 0 0; padding: 10px; border: 1px solid #e3e3e3; overflow: auto" ><br />'; $htmlBody .= '<table cellpadding="0" cellspacing="0" align="center" class="outputdata2"><br />'; // $htmlBody .= '<table cellpadding="0" cellspacing="0" th="font-weight: bold; width: 25px; padding: 5px,5px,5px,5px; padding-right: 10px; text-align: left; white-space: nowrap;" td="padding: 5px,5px,5px,5px; width: 25px; text-align: left; vertical-align: top; white-space: nowrap;">'; $htmlBody .= '<th>Campaign</th><th>Number of Calls</th><th>Total Call Duration</th><th>Avg Call Duration</th><th>Call Status</th><br />'; // Setup mail class, recipients and body $mailer->AddAddress('[email protected]', 'PHP Novice'); $mailer->Subject = 'Calls on '.$currentDate.''; $mailer->Body = $htmlBody; $mailer->IsHTML(true); $mailer->AltBody = $textBody; $mailer->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/dashboardOld/campaignFiles/'.$prevClient."_".$currentDate.'.csv', ''.$prevClient."_".$currentDate.'.csv'); $mailer->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/dashboardOld/campaignFiles/'.$prevClient."_".$currentDate.'.json', ''.$prevClient."_".$currentDate.'.json'); $mailer->Send(); $mailer->ClearAddresses(); $mailer->ClearAttachments(); $mailer->IsHTML(false); I even resorted to include the style sheet in what i passed to phpmailer because the formatting wasn't working when i coded the html settings withing $htmlBody originally. Any help would be appreciated. Thanks, Jeff Quote Link to comment https://forums.phpfreaks.com/topic/253093-sending-html-email-with-phpmailer/ Share on other sites More sharing options...
Drummin Posted December 13, 2011 Share Posted December 13, 2011 You need to use inline styling for email. Quote Link to comment https://forums.phpfreaks.com/topic/253093-sending-html-email-with-phpmailer/#findComment-1297535 Share on other sites More sharing options...
jsw1988eku Posted December 13, 2011 Author Share Posted December 13, 2011 I tried inline w/CreateBody and GetMailMIME and neither utilized the proper formatting. Which function should I use w/inline. Thanks, Jeff Quote Link to comment https://forums.phpfreaks.com/topic/253093-sending-html-email-with-phpmailer/#findComment-1297580 Share on other sites More sharing options...
Drummin Posted December 13, 2011 Share Posted December 13, 2011 Well you can't use class="something". You need to use style="width:500px" etc. Also, even though it's bad html, nested tables work better then divs so for example you want a centered letter you'd have the outer table with centered cell and the "smaller" inner table inside. Looks like your example is missing some <tr><td> tags as well as </table> tags. Make sure html is correct before attempting styling. Quote Link to comment https://forums.phpfreaks.com/topic/253093-sending-html-email-with-phpmailer/#findComment-1297585 Share on other sites More sharing options...
jsw1988eku Posted December 14, 2011 Author Share Posted December 14, 2011 Thank you. I nested tables and was able to add the style necessary to give the proper formatting to the html email. Jeff Quote Link to comment https://forums.phpfreaks.com/topic/253093-sending-html-email-with-phpmailer/#findComment-1297722 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.