Search code examples
phpphpmailer

Putting a variable to call out email body


So using PHPMailer is it possible to embed a variable in the body message which would then grab the contents of the email into body?

So basically

like this:

$mail->Body = writeBody();

in here:

//Begin of send email
$mail = new PHPMailer;

//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.server.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "[email protected]";
$mail->Password = "super_secret_password";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;

$mail->From = $emailAddress;

$mail->addAddress("[email protected]", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = writeBody();

if(!$mail->send())
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    echo "Message has been sent successfully";
}

Which then would call up the contents like this into the body:

function writeBody()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>THANK YOU</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0 " />
  <meta name="format-detection" content="telephone=no" />
  <!--[if !mso]><!-->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
  <!--<![endif]-->
  <style type="text/css">
    body {
    -webkit-text-size-adjust: 100% !important;
    -ms-text-size-adjust: 100% !important;
    -webkit-font-smoothing: antialiased !important;
    }
    img {
    border: 0 !important;
    outline: none !important;
    }
    p {
    Margin: 0px !important;
    Padding: 0px !important;
    }
    table {
    border-collapse: collapse;
    mso-table-lspace: 0px;
    mso-table-rspace: 0px;
    }
    td, a, span {
    border-collapse: collapse;
    mso-line-height-rule: exactly;
    }
    .ExternalClass * {
    line-height: 100%;
    }
    span.MsoHyperlink {
    mso-style-priority:99;
    color:inherit;}
    span.MsoHyperlinkFollowed {
    mso-style-priority:99;
    color:inherit;}
    </style>
    <style media="only screen and (min-width:481px) and (max-width:599px)" type="text/css">
    @media only screen and (min-width:481px) and (max-width:599px) {
    table[class=em_main_table] {
    width: 100% !important;
    }
    table[class=em_wrapper] {
    width: 100% !important;
    }
    td[class=em_hide], br[class=em_hide] {
    display: none !important;
    }
    img[class=em_full_img] {
    width: 100% !important;
    height: auto !important;
    }
    td[class=em_align_cent] {
    text-align: center !important;
    }
    td[class=em_pad_top]{
    padding-top:20px !important;
    }
    td[class=em_aside]{
    padding-left:10px !important;
    padding-right:10px !important;
    }
    td[class=em_height]{
    height: 20px !important;
    }
    td[class=em_space]{
    width:10px !important;  
    }
    td[class=em_width55] {
    width:80px !important;
  text-align:center !important;
    }
    td[class=em_width75] {
    width:100px !important;
    }
    td[class=em_font]{
    font-size:14px !important;  
    }
    td[class=em_font2] {
    text-align:center !important;
    }
    td[class=em_align_cent1] {
    text-align: center !important;
    padding-bottom: 10px !important;
    }
    }
    </style>
    <style media="only screen and (max-width:480px)" type="text/css">
    @media only screen and (max-width:480px) {
    table[class=em_main_table] {
    width: 100% !important;
    }
    table[class=em_wrapper] {
    width: 100% !important;
    }
    td[class=em_hide], br[class=em_hide], span[class=em_hide] {
    display: none !important;
    }
    img[class=em_full_img] {
    width: 100% !important;
    height: auto !important;
    }
    td[class=em_align_cent] {
    text-align: center !important;
    }
    td[class=em_pad_top]{
    padding-top:20px !important;
    }
    td[class=em_height]{
    height: 20px !important;
    }
    td[class=em_aside]{
    padding-left:10px !important;
    padding-right:10px !important;
    } 
    td[class=em_font]{
    font-size:14px !important;
    line-height:28px !important;
    }
    td[class=em_font1]{
    font-size:14px !important;
    line-height:18px !important;
    }
    td[class=em_font2]{
    font-size:14px !important;
    line-height:18px !important;
    text-align:center !important;
    }
    td[class=em_space]{
    width:10px !important;  
    }
    span[class=em_br]{
    display:block !important;
    }
    td[class=em_width55] {
    width:55px !important;
    font-size:15px !important;
    line-height:19px !important;
  text-align:center !important;
    }
    td[class=em_width75] {
    width:75px !important;
    font-size:15px !important;
    line-height:19px !important;
    }
    td[class=em_align_cent1] {
    text-align: center !important;
    padding-bottom: 10px !important;
    }
    }
  </style>
</head>
<body style="margin:0px; padding:0px;" bgcolor="#ffffff">
  <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
  <!-- === PRE HEADER SECTION=== -->  
    <tr>
      <td align="center" valign="top"  bgcolor="#30373b">
        <table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
          <tr>
            <td style="line-height:0px; font-size:0px;" width="600" class="em_hide" bgcolor="#30373b"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif" height="1"  width="600" style="max-height:1px; min-height:1px; display:block; width:600px; min-width:600px;" border="0" alt="" /></td>
          </tr>

The goal here is to break up the various items of the email into their own files for it to be easier to manage and edit.

Is this possible? if so, am i doing it correctly?


Solution

  • Yes this is definitely possible, however since the function writeBody() is defined in another file this won't work. What you can do is create a file with the email body, for example email.html and then read in those contents with php and store this in the body.

    Like this, of course if email.html is in another directory you have to put the right path before the filename:

        //Begin of send email
    $mail = new PHPMailer;
    
    //Enable SMTP debugging.
    $mail->SMTPDebug = 3;
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();
    //Set SMTP host name
    $mail->Host = "smtp.server.com";
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth = true;
    //Provide username and password
    $mail->Username = "[email protected]";
    $mail->Password = "super_secret_password";
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure = "tls";
    //Set TCP port to connect to
    $mail->Port = 587;
    
    $mail->From = $emailAddress;
    
    $mail->addAddress("[email protected]", "Recepient Name");
    
    $mail->isHTML(true);
    
    $mail->Subject = "Subject Text";
    $mail->Body = file_get_contents('email.html');
    
    if(!$mail->send())
    {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
        echo "Message has been sent successfully";
    }
    

    And your email.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>THANK YOU</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0 " />
      <meta name="format-detection" content="telephone=no" />
      <!--[if !mso]><!-->
      <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
      <!--<![endif]-->
      <style type="text/css">
        body {
        -webkit-text-size-adjust: 100% !important;
        -ms-text-size-adjust: 100% !important;
        -webkit-font-smoothing: antialiased !important;
        }
        img {
        border: 0 !important;
        outline: none !important;
        }
        p {
        Margin: 0px !important;
        Padding: 0px !important;
        }
        table {
        border-collapse: collapse;
        mso-table-lspace: 0px;
        mso-table-rspace: 0px;
        }
        td, a, span {
        border-collapse: collapse;
        mso-line-height-rule: exactly;
        }
        .ExternalClass * {
        line-height: 100%;
        }
        span.MsoHyperlink {
        mso-style-priority:99;
        color:inherit;}
        span.MsoHyperlinkFollowed {
        mso-style-priority:99;
        color:inherit;}
        </style>
        <style media="only screen and (min-width:481px) and (max-width:599px)" type="text/css">
        @media only screen and (min-width:481px) and (max-width:599px) {
        table[class=em_main_table] {
        width: 100% !important;
        }
        table[class=em_wrapper] {
        width: 100% !important;
        }
        td[class=em_hide], br[class=em_hide] {
        display: none !important;
        }
        img[class=em_full_img] {
        width: 100% !important;
        height: auto !important;
        }
        td[class=em_align_cent] {
        text-align: center !important;
        }
        td[class=em_pad_top]{
        padding-top:20px !important;
        }
        td[class=em_aside]{
        padding-left:10px !important;
        padding-right:10px !important;
        }
        td[class=em_height]{
        height: 20px !important;
        }
        td[class=em_space]{
        width:10px !important;  
        }
        td[class=em_width55] {
        width:80px !important;
      text-align:center !important;
        }
        td[class=em_width75] {
        width:100px !important;
        }
        td[class=em_font]{
        font-size:14px !important;  
        }
        td[class=em_font2] {
        text-align:center !important;
        }
        td[class=em_align_cent1] {
        text-align: center !important;
        padding-bottom: 10px !important;
        }
        }
        </style>
        <style media="only screen and (max-width:480px)" type="text/css">
        @media only screen and (max-width:480px) {
        table[class=em_main_table] {
        width: 100% !important;
        }
        table[class=em_wrapper] {
        width: 100% !important;
        }
        td[class=em_hide], br[class=em_hide], span[class=em_hide] {
        display: none !important;
        }
        img[class=em_full_img] {
        width: 100% !important;
        height: auto !important;
        }
        td[class=em_align_cent] {
        text-align: center !important;
        }
        td[class=em_pad_top]{
        padding-top:20px !important;
        }
        td[class=em_height]{
        height: 20px !important;
        }
        td[class=em_aside]{
        padding-left:10px !important;
        padding-right:10px !important;
        } 
        td[class=em_font]{
        font-size:14px !important;
        line-height:28px !important;
        }
        td[class=em_font1]{
        font-size:14px !important;
        line-height:18px !important;
        }
        td[class=em_font2]{
        font-size:14px !important;
        line-height:18px !important;
        text-align:center !important;
        }
        td[class=em_space]{
        width:10px !important;  
        }
        span[class=em_br]{
        display:block !important;
        }
        td[class=em_width55] {
        width:55px !important;
        font-size:15px !important;
        line-height:19px !important;
      text-align:center !important;
        }
        td[class=em_width75] {
        width:75px !important;
        font-size:15px !important;
        line-height:19px !important;
        }
        td[class=em_align_cent1] {
        text-align: center !important;
        padding-bottom: 10px !important;
        }
        }
      </style>
    </head>
    <body style="margin:0px; padding:0px;" bgcolor="#ffffff">
      <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
      <!-- === PRE HEADER SECTION=== -->  
        <tr>
          <td align="center" valign="top"  bgcolor="#30373b">
            <table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
              <tr>
                <td style="line-height:0px; font-size:0px;" width="600" class="em_hide" bgcolor="#30373b"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif" height="1"  width="600" style="max-height:1px; min-height:1px; display:block; width:600px; min-width:600px;" border="0" alt="" /></td>
              </tr>
    

    Also remember to close your email.html with the correct closing tags, as this can cause some issues.

    The html file can also be a PHP file, it will work a bit differently then, then you have to include the email.php file which then contains a function to which you pass the parameters you want to use:

    include 'email.php';        
    //Begin of send email
    $mail = new PHPMailer;
    // same code as before, but use:
    // $mail->Body = writeBody();
    

    And then wrap the html in a function like this:

        function writeBody($param1, $param2) {
          return "email template here";
    }