1

I am trying to use PHP Mailer to send dynamic emails. the issue i face is that the variable values are not going in the email, infact nothing is being sent in the email from the p tags where the variable is stored. the email however goes fine.Not sure what i am missing , Please see the code below :-

<html>
    <head>
      <style>

            .email-background {
            background: #eee;
            padding:10px;

            }
            .email-container {
             max-width:800px;
              background: #fff;
             margin:0 auto;
             padding:20px;
             color: #b3d1ff;
             }
             .pre-header {
        max-width: 800px;
        background: #66a3ff none repeat scroll 0% 0%;
        text-align: center;
        margin: 0px auto;
        padding: 20px;
        font-size: 20px;
        font-weight: 800;
        font-family: montserrat;
        color: #fff;


             }
             img {
             max-width:100%;
             }
             hr { 
            display: block;
            margin-top: 0.5em;
            margin-bottom: 0.5em;
            margin-left: auto;
            margin-right: auto;
            border-style: inset;
            border: 2px solid #eee;
        } 
      </style> 
    </head>
    <body>
    <div class="email-background">
      <div class="pre-header">
      Your Weekly.......
      </div>
      <div class="email-container">
          <p>Dear %$firstname%,</p>
          <h1>Test Email</h1>


         <tr>
           <td>
           test1 test2
           </td>
           <td>
           firstname
           Lastname

           <hr>
           </td>
         </tr>
       <div class="email-footer">

       </div>


       </div>



       </div>     

    </body>
</html>

    $mail = new PHPMailer(true);



$mail->IsMail();


$mail->setFrom('test.com');

$mail->addAddress($row3["email"]);               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');

$mail->isHTML(true);                     // Set email format to HTML

$mail->Subject = 'test email!!!';
$body= file_get_contents ('/Autoaction/phpmailer.html');

$body = str_replace('%$firstname%',$firstname,$body);   
$body = preg_replace('/\\\\/','', $body);        
$mail->msgHTML($body);
//$mail->Send();                    



if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
pranay
  • 61
  • 10
  • I can't find where the value of $firstname is assigned in your code. – Karthik N Feb 19 '16 at 06:20
  • yes there are 3 SQL queries above this code which fetch the value i can echo it fine if i need so value itself is not the issue. Only the Variable values are not going in the emails – pranay Feb 19 '16 at 06:22
  • echo your $body and check for the string you get after file_get_contents() – Karthik N Feb 19 '16 at 06:26
  • @karthik - i echo'ed the $body i get the same information i get in email , without the variable. How can i check the string after file_get_contents() ? – pranay Feb 19 '16 at 06:34
  • need help ,please... i have been trying this since a long time and unable to get this to work..... – pranay Feb 19 '16 at 06:56
  • before $mail->send(); echo $mail->body; exit; check the content – Karthik N Feb 19 '16 at 07:00
  • @Karthik- tried echo $mail->body; exit; before the $mail->send(); it doesn't display anything. However when i echo $body; it displays the email body but without the variable values – pranay Feb 19 '16 at 07:07
  • and also check your PHPMailer class file the function name is MsgHTML() or it is MsgHTML() – Karthik N Feb 19 '16 at 07:08
  • PHP is case sensitive for property names (but not function names - `msgHTML()` is correct) - you need to look at `$mail->Body`, not `$mail->body`. Also, you're enabling exceptions (passing true to the constructor), but you've got no try/catch to deal with them. – Synchro Feb 19 '16 at 07:38
  • I changed the code to use array's and it resolved :- $body = str_replace( array( '[firstname]','[lastname]'), array($firstname,$lastname,,),$body); $body = preg_replace('/\\\\/','', $body); – pranay Feb 22 '16 at 08:59

0 Answers0