Php mailer and html includes with php variables

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

Hello I am trying to send html emails using php mailer class. The problem is i would like to incllude php variables in my email while using includes as to keep things organized. Heres my php mailer....,You can put variables in the html email and then do a string_replace so the contents appear in the email instead of the variables:,Important: Since your include is processing within a function, the scope of execution of the PHP template file (/emails/event.php) is in that function's scope (no variables immediately available besides super globals, I'm uncertain how I can get this to work.. I set $varsToMakeLocal = array('$toMail' => $toEmail, '$toName' => $toName); and I try to use your $mail->Body = $this->get_include_contents($htmlfile, $varsToMakeLocal); but the mail is intact and the variables don't get replaced. – Jorrick Sleijster Nov 9 '16 at 21:49

Yes, very easily with include and a short helper function:

function get_include_contents($filename, $variablesToMakeLocal) {
   extract($variablesToMakeLocal);
   if (is_file($filename)) {
      ob_start();
      include $filename;
      return ob_get_clean();
   }
   return false;
}

$mail - > IsHTML(true); // set email format to HTML
$mail - > Subject = "You have an event today";
$mail - > Body = get_include_contents('../emails/event.php', $data); // HTML -> PHP!
$mail - > Send(); // send message
load more v
88%

<?php
$to = $_POST['email'];
$subject = "Email Subject";

$message = 'Dear '.$_POST['name'].',<br>';
$message .= "We welcome you to be part of family<br><br>";
$message .= "Regards,<br>";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <enquiry@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>
load more v
72%

It is also possible to send HTML email with mail(). , (Windows only) When PHP is talking to a SMTP server directly, if a full stop is found on the start of a line, it is removed. To counter-act this, replace these occurrences with a double dot. , Using mail() to send a simple email: ,imap_mail() - Send an email message

load more v
65%

Here's a class that extends the phpmailer class and sets the defaults for the particular site: PHP include file: mail.inc.php

require("class.phpmailer.php");

$mail = new phpmailer();

$mail->From     = "list@example.com";
$mail->FromName = "List manager";
$mail->Host     = "smtp1.example.com;smtp2.example.com";
$mail->Mailer   = "smtp";

@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("my_company");
$query = "SELECT full_name, email, photo FROM employee WHERE id=$id";
$result = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result))
{
    // HTML body
    $body  = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
    $body .= "<i>Your</i> personal photograph to this message.<p>";
    $body .= "Sincerely, <br>";
    $body .= "phpmailer List manager";

    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "Hello " . $row["full_name"] . ", \n\n";
    $text_body .= "Your personal photograph to this message.\n\n";
    $text_body .= "Sincerely, \n";
    $text_body .= "phpmailer List manager";

    $mail->Body    = $body;
    $mail->AltBody = $text_body;
    $mail->addAddress($row["email"], $row["full_name"]);
    $mail->addStringAttachment($row["photo"], "YourPhoto.jpg");

    if(!$mail->send())
        echo "There has been a mail error sending to " . $row["email"] . "<br>";

    // Clear all addresses and attachments for next loop
    $mail->clearAddresses();
    $mail->clearAttachments();
}
load more v
75%

http://www.xeweb.net/2009/12/31/sending-emails-the-right-way-using-phpmailer-and-email-templates/,I'm using PHPMailer to send email. My ics content is: For some reason I don't see the expected RSVP buttons in any email clients. Both Gmail and Mac Mail simply...,I have previously asked some question. But my project requirement is to have the html template with % variable name % so that anyone could make changes in the html file without touching the code part.,Im trying to embed an image within my message body but it ends up as an attachment i have set everything else, including the addresses, the mail gets send out,...

index.php

    < ? php
    include('class.phpmailer.php'); // Retrieve the email template required 
    $message = file_get_contents('mail_templates/sample_mail.html');
    $message = str_replace('%testusername%', $username, $message);
    $message = str_replace('%testpassword%', $password, $message);
    $mail = new PHPMailer();
    $mail - > IsSMTP(); // This is the SMTP mail server 

    $mail - > SMTPSecure = 'tls';
    $mail - > Host = "smtp.gmail.com";
    $mail - > Port = 587;
    $mail - > SMTPAuth = true;
    $mail - > Username = '[email protected]';
    $mail - > Password = 'mypassword';
    $mail - > SetFrom('[email protected]', 'Pricol Technologies');
    $mail - > AddAddress('[email protected]');
    $mail - > Subject = 'Your account information';
    $mail - > MsgHTML($message);
    $mail - > IsHTML(true);
    $mail - > CharSet = "utf-8";
    //$mail->AltBody(strip_tags($message)); 
    if (!$mail - > Send()) {
       echo "Mailer Error: ".$mail - > ErrorInfo;
    } ?
    >
load more v
40%

Sending Email with PHP Mailer and Google Gmail SMTP server.,We added some PHP variables within our HTML page to echo out either the error messages or the value that was entered in the form. Note that we haven’t created these variables yet inside our PHP script so it will produce some errors if you refresh your browser., We added some PHP variables within our HTML page to echo out either the error messages or the value that was entered in the form. Note that we haven’t created these variables yet inside our PHP script so it will produce some errors if you refresh your browser. ,We created a basic form with the POST method which ultimately sends the request back to this page.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>Document</title>
</head>

<body>
   <h1>Hello World</h1>
</body>

</html>
load more v

Other "undefined-undefined" queries related to "Php mailer and html includes with php variables"