Jump to content

Sending HTML email with phpMailer


jsw1988eku

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/253093-sending-html-email-with-phpmailer/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.