Im trying to use PHPMailer and been getting an error that the Message body empty or the screen is blank and does not print that the message has been sent. My folder structure is everything is in the same folder for testing in case anyone needs to know. I have also followed this post but did not seem to work. PHPMailer Mailer Error: Message body empty
<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
echo $body;
//$body = preg_replace('/[\]/','',$body);
//$body = "To view the message, please use an HTML compatible email viewer!";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "no-reply#yourdomain.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('no-reply#yourdomain.com', 'First Last');
//$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->IsHTML(true);
$mail ->MsgHTML($body);
$mail->Body = $body;
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
//$mail->IsHTML(true);
$address = "someemail#yourdomain.com";
$mail->AddAddress($address, "John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Contents.html:
<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<br>
<br>
This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a <div> tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost (codeworxtech#users.sourceforge.net)<br />
Author: Marcus Bointon (coolbru#users.sourceforge.net)<br />
</div>
</body>
I have put the echos in the code for troubleshooting to see if it is getting the data or not. I think this is honestly something to do with Gmail for the mail hosting. But its what they use. Thank you in advanced with any direction you can provide.
Chris
try this
<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
//error_reporting(E_ALL);
date_default_timezone_set('America/Toronto');
include "PHPMailer_5.2.4/class.phpmailer.php";
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$message='<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<br>
<br>
This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a <div> tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Andy Prevost (codeworxtech#users.sourceforge.net)<br />
Author: Marcus Bointon (coolbru#users.sourceforge.net)<br />
</div>
</body>';
$mail = new PHPMailer();
//$body = file_get_contents('contents.html');
echo $message;
//$body = preg_replace('/[\]/','',$body);
//$body = "To view the message, please use an HTML compatible email viewer!";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "no-reply#yourdomain.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('no-reply#yourdomain.com', 'First Last');
//$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->IsHTML(true);
$mail ->MsgHTML($message);
$mail->Body = $message;
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
//$mail->IsHTML(true);
$address = "someemail#yourdomain.com";
$mail->AddAddress($address, "John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Related
I have following piece of code. It returns message "send successfully " but we won't receive email at given gmail, Is this code right or have I missed something?
<?php
require("mailer/PHPMailerAutoload.php");
require("mailer/class.smtp.php");
require("mailer/class.phpmailer.php");
$name=$_POST['Your_Name'];
$email=$_POST['Your_Email'];
$message=$_POST['Your_Msg'];
echo $name ;
$mail = new PHPMailer();
// set mailer to use SMTP
//$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = "465";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mandarpatil0003#gmail.com"; // SMTP username
$mail->Password = "Con#Soumitra1"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("mandarpatil0003#gmail.com", "Mandar");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody ="Name : {$name}\n\nEmail : {$email}\n\nMessage : {$message}";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
else
{
echo "Thank you for contacting us. We will be in touch with you very soon.";
}
?>
Does this code have any problems or do I have to try a different approach?
<?php
include 'library.php'; // include the library file
include "classes/class.phpmailer.php"; // include the class name
include "classes/class.smtp.php";
if(isset($_POST["send"])){
$email = $_POST["email"];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; //Hostname of the mail server
$mail->Port = 465; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPDebug = 1;
//$mail->SMTPAuth = false; //Whether to use SMTP authentication
$mail->Username = "****#gmail.com"; //Username for SMTP authentication any valid email created in your domain
$mail->Password = "****"; //Password for SMTP authentication
$mail->AddReplyTo("****#gmail.com", "Reply name"); //reply-to address
$mail->SetFrom("****#gmail.com", "Your Name"); //From address of the mail
// put your while loop here like below,
$mail->Subject = "Your SMTP Mail"; //Subject od your mail
$mail->AddAddress($email, "Asif18"); //To address who will receive this email
$mail->MsgHTML("<b>Hi, your first SMTP mail has been received."); //Put your body of the message you can place html code here
//Attach a file here if any or comment this line,
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
}
else{
echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server by Asif18</title>
<meta name="keywords" content="send mails using smpt in php, php mailer for send emails in smtp, use gmail for smtp in php, gmail smtp server name"/>
<meta name="description" content="Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server"/>
<style>
.as_wrapper{
font-family:Arial;
color:#333;
font-size:14px;
}
.mytable{
padding:20px;
border:2px dashed #17A3F7;
width:100%;
}
</style>
<body>
<div class="as_wrapper">
<h1>Send mails using SMTP and PHP in PHP Mailer using our own server or gmail server</h1>
<form action="" method="post">
<table class="mytable">
<tr>
<td><input type="email" placeholder="Email" name="email" /></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Send via SMTP" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
This question already has answers here:
donĀ“t recognize variable of php
(2 answers)
Closed 7 years ago.
Hello i made this script:
index.php:
<table width="400" border="0" align="center" cellpadding="3" cellspacing="1"><tr><td><strong>Whitelist Request </strong></td></tr></table><table width="400" border="0" align="center" cellpadding="0" cellspacing="1"><tr><td><form name="form1" method="post" action="send_whitelist.php"><table width="100%" border="0" cellspacing="1" cellpadding="3"><tr><td width="16%">Username</td><td width="2%">:</td><td width="82%"><input name="username" type="text" id="username" size="50"></td></tr><tr><td>Paysafe</td><td>:</td><td><input name="paysafe" type="text" id="paysafe" size="50"></td></td><tr></tr><td>Name</td><td>:</td><td><input name="name" type="text" id="name" size="50"></td></tr><tr><td>Email</td><td>:</td><td><input name="customer_mail" type="text" id="customer_mail" size="50"></td></tr><tr><td> </td><td> </td><td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></form></td></tr></table>
Email Sender:
<?php
// Contact subject
$subject ="Minecraft Server Whitelist Request";
// Details
$message="Name: $name \n Username: $username \n Paysafe: $paysafe ";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='n************#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "I have received your whitelist request. Please wait.";
}
else {
echo "ERROR";
}
?>
The contents of the textboxes doesen't appear in the mail.
Any ideas?
I normally use PHPMailer to send emails with php. Here's and example to send an email via gmail using an html file as body and attaching an image:
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP 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'), dirname(__FILE__));
//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!";
}
TIPS:
1 - Check other PHPMailer examples.
2 - PHPMailer Troubleshooting.
3 - Set SMTPDebug = 2 or higher, to see what the remote SMTP server says.
4 - You may have to enable less secure apps on you gmail account.
Here is my code, for some reasons.. it is working perfectly fine in my localhost and it can send email with HTML content and is rendered perfectly. When I upload my codes on the production, the email seems to be in text format and all the HTML tags are shown. What to do with this kind of issue?
There are no errors and email is sent (based from the error info on the last part of the code), its just the rendering of the BODY Im having trouble with. Thanks!
<?php
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'email-smtp.us-west-2.amazonaws.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
// I tried PORT 25, 465 too
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
// Username to use for SMTP authentication - use full email address for gmail
$mail-> Username = "USERNAME";
// Password to use for SMTP authentication
$mail-> Password = "PASSWORD";
//Set who the message is to be sent from
$mail->setFrom('from#email.com', '');
//Set who the message is to be sent to
$mail->addAddress('example#email.com', '');
//Set the subject line
$mail->Subject = 'TEST Mail';
//this is just an example content
$mail->Body = '
<div style="background-color:#254a64;">
<div align="center" style="color:white; padding:20px 0px 10px 5px; font-size: 1.5em; font-family:Arial;">
SAMPLE HEADER
</div>
</div>
<div style="background-color: #e8c020; padding: 2px;"></div>
<br><br>';
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body ALT BODYYYY';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
This question in now closed. Thank you! You may check the answer from below.
Calling the isHTML() method after the $mail->Body has been set solved the problem for me:
$mail->Body = $Body;
$mail->IsHTML(true); // <=== call IsHTML() after $mail->Body has been set.
Issue now closed! Thanks :D
am working on a website for my client, i want the application to read the content of the html and send an email to the customer's email, but i want to display some variables which the customer entered in the html content and send it via email. This is my code.
this is the html code:
<p> {$cname} </p>
<p> Thank you for contacting our support team. A support ticket has now been opened for your request. You will be notified when a response is made by email. The details of your ticket are shown below. </p>
<p> Subject: {$sub} <br />
Priority: {$prio} <br />
Status: Open </p>
<p> Thanks as always for being our customer.<br />
<br />
Regards,</p></td>
this is my php code:
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$_SESSION['studentid'] = $_POST['sid'];
$_SESSION['cname'] = $_POST['namee'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['department'] = $_POST['department'];
$_SESSION['priority'] = $_POST['priority'];
$_SESSION['subjectd'] = $_POST['subject'];
$_SESSION['commentc'] = $_POST['message'];
$cname =$_SESSION['cname'];
$detailz = $_SESSION['commentc'];
$email = $_SESSION['email'];
$cname = $_SESSION['cname'];
$ip = getenv("REMOTE_ADDR");
$prio =$_SESSION['priority'];
$stid = $_SESSION['studentid'];
$dept = $_SESSION['department'];
$sub = $_SESSION['subjectd'];
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = preg_replace('/[\]/','',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $row_prs['shost']; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = $row_prs['shost']; // sets the SMTP server
$mail->Port = $row_prs['portx']; // set the SMTP port for the GMAIL server
$mail->Username = $row_prs['susername']; // SMTP account username
$mail->Password = $row_prs['spassword']; // SMTP account password
$mail->SetFrom($row_pr['from'], $row_pr['sname']);
//$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "[Ticket ID:" .$tno."]".$sub;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = $_SESSION['emid'];
$mail->AddAddress($address, "John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Try this,
<p> <?php echo $cname; ?> </p>
<p> Thank you for contacting our support team. A support ticket has now been opened for your request. You will be notified when a response is made by email. The details of your ticket are shown below. </p>
<p> Subject: <?php echo $sub; ?> <br />
Priority: <?php echo $prio; ?> <br />
Status: Open </p>
<p> Thanks as always for being our customer.<br />
<br />
Regards,</p></td>
You are store the data in session. So you can use those to show it back. Use this
<p> <?php echo $_SESSION['cname'] ?> </p>
<p> Thank you for contacting our support team. A support ticket has now been opened for your request. You will be notified when a response is made by email. The details of your ticket are shown below. </p>
<p> Subject: <?php echo $_SESSION[' subjectd'] ?> <br />
Priority: <?php echo $_SESSION['priority'] ?> <br />
Status: Open </p>
<p> Thanks as always for being our customer.<br />
<br />
Regards,</p>
Your data already sotred in session. So till session get distroyed you can use those variable as well.
some ref , but is just replace test '{$XXX}' , not are real php var
$replace_values = array(
'$cname' => $_SESSION['cname'],
);
$result = preg_replace_callback('!\{(\$\w+)\}!', function($ma) use ($replace_values) {
if(isset($ma[1]) && isset($replace_values[$ma[1]])){
return $replace_values[$ma[1]];
}
return '';
}, $body);
https://eval.in/409962
this is the code that worked, I just want to say thank you to you guys that answered my question but i just want a BIG THANK YOU to JOE LEE. I own you a bottle of beer. Respect
///to get the content of the html and replace it with the variables
$body = file_get_contents('contents.html');
$replace_values = array(
'$cname' => $_SESSION['cname'],
'$prio' => $_SESSION['priority'],
'$sub' => $_SESSION['subjectd'],
);
$result =preg_replace_callback('!\{(\$\w+)\}!', function($ma) use ($replace_values) {
if(isset($ma[1]) && isset($replace_values[$ma[1]])){
return $replace_values[$ma[1]];
}
return '';
}, $body);
////
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $row_prs['shost']; // SMTP server
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = $row_prs['shost']; // sets the SMTP server
$mail->Port = $row_prs['portx']; // set the SMTP port for the GMAIL server
$mail->Username = $row_prs['susername']; // SMTP account username
$mail->Password = $row_prs['spassword']; // SMTP account password
$mail->SetFrom($row_prs['from'], $row_prs['sname']);
//$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "[Ticket ID:" .$tno."]".$sub;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($result);
I tried using summernote
and PHP Mailer to sent the contents to my e-mail.
But the e-mail does not appear to be in HTML form eventhough I put :
$mail->IsHTML(true); //set email format to HTML
Below is my code for sending e-mail:
require '../PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP(); //set mailer to use SMTP
$mail->Host = "mail.xxxyy.com"; //specify SMTP mail server
$mail->Port = "2525"; //specify SMTP Port
$mail->SMTPAuth = true; //turn on SMTP authentication
$mail->Username = "xxxxx#xxxxx.com"; //Full SMTP username
$mail->Password = "xxxxx"; //SMTP password
$mail->From = "xxxxx#xxx.com";
$mail->FromName = "xxxxx";
$mail->AddAddress("xxxxxx", "Mei Yi");
$mail->WordWrap = 50; //optional, you can delete this line
$mail->IsHTML(true); //set email format to HTML
$mail->Subject = "RE:". $_POST[txtFeedbackSubject2];
$mail->Body = htmlspecialchars($_POST['content']);
if(!$mail->Send())
{
echo "Message could not be sent.
";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
The e-mail content appears to be like this:
<span style=\"font-weight: bold; font-style: italic; text-decoration: underline;\">Aadadssdasadsad.</span>
Please advice.
Thanks.
You're applying htmlspecialchars to the body, which will prevent the HTML from being rendered. Don't do that.
Apart from that, your message body will be whatever is in $_POST['content'], over which PHPMailer has no control.