I am using PHPmailer to send email.
I have also used HTML = True content type
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($To , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $Subject;
$mail->Body = $Body;
Once the email is sent i see the actual HTML code instead of the contents please check below
**Not sure what is the issue **
Calling the isHTML() method after the instance Body property (I mean $mail->Body) has been set solved the problem for me:
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->IsHTML(true); // <=== call IsHTML() after $mail->Body has been set.
There is msgHTML() method, which also call IsHTML().
The name IsHTML is confusing...
/**
* Create a message from an HTML string.
* Automatically makes modifications for inline images and backgrounds
* and creates a plain-text version by converting the HTML.
* Overwrites any existing values in $this->Body and $this->AltBody
* #access public
* #param string $message HTML message string
* #param string $basedir baseline directory for path
* #param bool $advanced Whether to use the advanced HTML to text converter
* #return string $message
*/
public function msgHTML($message, $basedir = '', $advanced = false)
or if you have still problems you can use this
$mail->Body = html_entity_decode($Body);
In version 5.2.7 I use this to send plain text:
$mail->set('Body', $body);
do like this-paste your html code inside your separate html file using GET method.
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->addAttachment= $_GET['addattachment']; $mail->AltBody
=$_GET['AltBody']; $mail->Subject = $_GET['subject']; $mail->Body = $_GET['body'];
just you need to pass true as an argument to IsHTML() function.
all you need to do is just add $mail->IsHTML(true); to the code it works fine..
Related
I have a problem sending multiple email addresses functions. If I send a single email address, I can work. If I send multiple emails to the receiver, they cannot receive my message. I am using the PHPMailer function to do the email function with XAMPP. I am using the PHP array function to put the receiver's address in the array and use the while function to loop the receiver address to send it.
Below is my coding:
$address = array('st9overfindsolution#gmail','st7overfindsolution#gmail');
require 'class/class.phpmailer.php';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
// $mail->SMTPDebug = 1;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = '1233aqqq';
$mail->SMTPSecure = 'ssl';
$mail->From = $_POST["email"];
$mail->FromName = $_POST["name"];
$mail->AddCC($_POST["email"], $_POST["name"]);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
while(list ($key, $val) = each($address)){
$mail->AddAddress($val);
}
What I've tried?
1.I put all receiver email addresses in the array $address = array('st9overfindsolution#gmail','st7overfindsolution#gmail'); and use below while function code, but cannot work.
2.If send single email address without using while function, just can work, like below coding:
Hope someone can guide me on how to solve this problem. Thanks.
i have created new function named "sendmyemail" and inserted the code inside this function, so you can sent multiple emails by calling this function with email address.
<?php
/**
* This example shows sending a message using a local sendmail binary.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
function sendmyemail($whotoEmail){
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($whotoEmail);
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to
embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent to '.$whotoEmail.'!';
}
}
sendmyemail('abcduser#gmail.com');
sendmyemail('efguser#gmail.com');
sendmyemail('hijkuser#gmail.com');
sendmyemail('lmnouser#gmail.com');
I am using PHPmailer to send email.
I have also used HTML = True content type
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($To , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $Subject;
$mail->Body = $Body;
Once the email is sent i see the actual HTML code instead of the contents please check below
**Not sure what is the issue **
Calling the isHTML() method after the instance Body property (I mean $mail->Body) has been set solved the problem for me:
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->IsHTML(true); // <=== call IsHTML() after $mail->Body has been set.
There is msgHTML() method, which also call IsHTML().
The name IsHTML is confusing...
/**
* Create a message from an HTML string.
* Automatically makes modifications for inline images and backgrounds
* and creates a plain-text version by converting the HTML.
* Overwrites any existing values in $this->Body and $this->AltBody
* #access public
* #param string $message HTML message string
* #param string $basedir baseline directory for path
* #param bool $advanced Whether to use the advanced HTML to text converter
* #return string $message
*/
public function msgHTML($message, $basedir = '', $advanced = false)
or if you have still problems you can use this
$mail->Body = html_entity_decode($Body);
In version 5.2.7 I use this to send plain text:
$mail->set('Body', $body);
do like this-paste your html code inside your separate html file using GET method.
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->addAttachment= $_GET['addattachment']; $mail->AltBody
=$_GET['AltBody']; $mail->Subject = $_GET['subject']; $mail->Body = $_GET['body'];
just you need to pass true as an argument to IsHTML() function.
all you need to do is just add $mail->IsHTML(true); to the code it works fine..
I'm using phpmailer in my app, and i want it to be used by multiple users, but without changing the smtp server. here's what i want to do.
$mail = new PHPMailer(true);
$mail->SMTPDebug = 1;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Username = "mygmailtestaccount#gmail.com";
$mail->Password = "pwd0";
$mail->SetFrom("myoutlooktestaccount#hotmail.com","User name test");
$to = "receiveroutlooktestaccount#hotmail.co.jp";
$mail->AddAddress($to);
$mail->Subject = "Testing PHPMailer Message";
$mail->AltBody = "Email viewer! please do no spam me !!! \n";
$mail->WordWrap = 80;
$mail->MsgHTML($body);
$mail->IsHTML(true);
if (!$mail->Send()) {
echo "im in error zone";
echo "Mailer Error: " . $mail->ErrorInfo;
}
The problem is that i get the email with the sender as gmail "mygmailtestaccount#gmail.com", but i want my app to be only a "jump", to send emails from it regardless of my SMTP provider.
You need a lowercase s for setFrom:
/**
* Set the From and FromName properties.
* #param string $address
* #param string $name
* #param bool $auto Whether to also set the Sender address, defaults to true
* #throws phpmailerException
* #return bool
*/
public function setFrom($address, $name = '', $auto = true)
Taken from: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
I have the following php code for sending email with phpmailer
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->SetFrom($from, $sender_name);
$mail->Subject = $subject;
$mail->AddAddress($to, "test");
$mail->Body = $body; //$body is an html content
$mail->Send();
This will send an email.I need to send plain text alternative with this,for an email client does not have HTML support.
Is possible?
Is possible to do with php mail() function?
PHPMailer has a property AltBody, so you could add some plain text like this:
$mail->AltBody = strip_tags($body);
See the documentation.
I am looking for a way to save my template file which I use as a certificate snf sttsch it to my my.
Below is the code to replace fields in my template with one I have queried.
require("class.phpmailer.php");
//placeholders array
$pholders = array('replace_name', 'replace_number', 'replace_title');
//Certificate replace values array
$failedValues = array($FullName, $TestNo, $Title);
$mail_body = file_get_contents("failed.html");
$mail_body = str_replace($pholders,$failedValues,$mail_body);
//mailer
$mail = new phpmailer;
$mail->IsSMTP(); // set mailer to use SMTP
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Results";
$mail->Body = $mail_body;
$mail->AddAttachment("cpd.html");
I have omitted code for the smtp setting. The file cpd.html has to be generated and attached. I am looking for a way on how to generate it and save it for attaching it to the mail.