I'm trying to get the basic example working for PHPMailer.
All I done was upload the whole PHPMailer_5.2.2 folder, configured the page below as per the code you see and I keep getting Mailer Error: Message body empty, but I can clearly see the contents.html file has html in it and isn't empty. This is the example file I'm using from the PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php
I tried using the settings I have in Outlook for Gmail that works, I know my username and password, the SMTP port is 587 and it's set to TLS, I tried replacing SSL with TLS in the code below, I still get same error.
I also tried the following code, which has been suggested:
changed this:
$mail->MsgHTML($body);
to this
$mail->body = $body;
I still got same error, is there something else I need to configure? It's my first time using PHPMailer, I can get the standard php mail working, but I want to try this because the page I'm going to be emailing has lots of html and I don't want to have to go through all the html and enter character escapes so someone recommending using this.
<?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');
$body = preg_replace('/[\]/','',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.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 = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxx#gmail.com"; // GMAIL username
$mail->Password = "xxx"; // GMAIL password
$mail->SetFrom('xxx#gmail.com', 'First Last');
$mail->AddReplyTo("xxx","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "xxx.net";
$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!";
}
?>
I had the same problem and I solved just changing:
This:
$mail->body = 'Hello, this is my message.';
Into this:
$mail->Body = 'Hello, this is my message.';
body into Body what a little mistake!
PHPMailer->MsgHTML() tries to do something clever with fixing non-absolute URLs, but for me too it just returns empty.
$mail->IsHTML(true);
$mail->Body=$body;
First I had to enable the ssl extension, (which I have obtained from another post on this site). To do that, open php.ini and enable php_openssl.dll by changing
;extension=php_openssl.dll
to
extension=php_openssl.dll
Now, enable tls and use port 587. Then comment out the preg_replace.
Finally, I was able to make it show up in my gmail "Sent" file, although I was expecting to see it in my "Inbox".
This site is awesome! Thanks.
When setting $mail->Body equal to variable the body is empty.
When I attach a string to the body everything works fine.
The solution is to strval() a variable containing Your HTML code.
$message;//Your HTML message code
$mail->Body = strval($message);
Related
I've got a custom PHP website that has a form which triggers an email to be sent and in this email it contains an authentication link to confirm/verify that they are the person submitting to the form.
The current PHPMailer code looks like:
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//$mail->isSMTP();
$mail->Host = 'localhost';
//$mail->SMTPAuth = false;
//$mail->Username = '';
//$mail->Password = '';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->XMailer = 'WebsiteName Mailer';
$mail->addCustomHeader('X-Mailer-Type', 'WebsiteName/Auto/Verify');
$mail->setFrom('auth#websitename.com', 'WebsiteName');
$mail->addAddress('test-xxxxxxx#srv1.mail-tester.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Form Authentication from WebsiteName';
$mail->AltBody = 'Example email body here in plain text';
$mail->Body = "<!DOCTYPE html><html lang='en'><body>Example html email here <br></body></html>";
$mail->send();
I've also tried changing the port to 587 and changing the SMTPSecure to ENCRYPTION_STARTTLS
Every email that is sent to a server with SpamAssassin gets -2.499 score because of the PHP_SCRIPT flag which has the description of Sent by PHP script
I'm trying to find a fix my PHP (PhpMailer) to get around this as my emails are legitimate and wondering what I can do to better make them be detected fairly.
The interesting thing is emails sent with WP SMTP Mail plugin via WordPress do not get flagged for this SpamAssassin rule even though WP SMTP Mail also uses PhpMailer
https://github.com/awesomemotive/WP-Mail-SMTP/blob/master/src/Providers/MailerAbstract.php#:~:text=public%20function%20process_phpmailer(%20%24phpmailer%20)%20%7B
Fatal error: Class 'PHPMailer' not found in C:\wamp\www\sendemail.php on line 13
This is line 13:
$mail = new PHPMailer();
I have already researched and they say you need to have these:
require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
But I already have those and they reside in the same folder as sendemail.php but still the same error.
<?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_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('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 = "email#email.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('email#email.com', 'name');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('email#sample.com', 'name');
//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!";
}
Here is my file layout:
I encountered the same issue, and managed to solve it by entering the following two lines at the very top of the PHP script used for sending the mail - not just in the files I required.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
?>
Let me know if it works for you as well.
Add all those extra require lines are not going to help; Just load the autoloader, like the original example code says. Try loading it from an absolute path:
require '/full/path/to/PHPMailerAutoload.php';
If that works, you need to check that your include_path setting in php.ini includes the directory you're loading from - for example that it contains . as one of the paths.
I'm pretty convinced this is an environment/config problem, not code, so try a completely minimal script, just this:
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
If you're doing new development, you should really be using composer anyway - it completely solves include problems and you'll never have to worry about where your libraries are again.
I'm trying to use phpmailer to work alongside a contact form I have made. I was originally using simple php coding for the form, but this would not work on the web company's server, so they asked me to use phpmailer instead. I have changed the coding a little bit to try and integrate it into the contact page that has the contact form.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->Username = "myemail#hotmail.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->addAddress('stacey_victoria#hotmail.com', 'Sender'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message from Website';
$mail->Body = $message; $name;
$mail->From = $email;
if($_POST){
$feedback = 'Thanks for your message';
}
?>
The contact page appears as normal, and once the submit button has been pressed, the echo-feedback message is shown. However, no email is sent through.
I edited the "Simple Example" found here: https://github.com/PHPMailer/PHPMailer and uploaded the php file to the server and this worked fine so I know the smtp information I have used is correct and working.
Is it even possible to put phpmailer coding above the html coding?
If so, what am I doing wrong?
You are not sending the email...
Use :
if($mail->Send())
I've been trying to get my PhpMailer to work for over a week now.
I'm on a Windows machines, so I set up a "droplet" server with DigitalOcean (because they seem to have good step-by-step instructions) and then set up fake email accounts with ZohoMail (because it's free and I just need to test this.)
I've got breaks all over the three PhpMailer files I need (PhpMailerAutoload.php, class.phpmailer.php, and class.smtp.php). What I figured out is that I'm getting hung up on this line:
class.smtp.php line 1060 (or about since I've got breaks in there)
inside function get_lines:
$str = #fgets($this->smtp_conn, 515);
I put in more breaks, as below:
//*********Debug Code here
$test = $this->smtp_conn;
echo "\nContents of test var: \n";
var_dump($test);
echo "about to set str var \n";
$str = #fgets($test, 515);
echo "Contents of str var \n";
var_dump($str);
echo "\n out of debug code";
//***********End of debug code
After two minutes (2 minutes and 5 seconds to be precise) $test var_dumps just fine and "about to set str var" prints just fine.
But that's it. No more code executes.
I don't know what #fgets is. I don't see any documentation in the PHP manual for that, so I'm not sure where to go from here.
FIRST IDEA:
I checked on my php versions:
- server is running 5.5.9
- my PC is running 5.6.12
I'm guessing those are close enough.
SECOND IDEA:
I found that in the past there are a "==" vs "=" issue with this line of code, but that's clearly not the issue here.
THIRD IDEA:
I also read that some people fixed this problem by making sure that "php_openssl.dll" is enabled in their php.ini file.
In mine I have a line that reads:
extension = php_openssl.dll
So I think that's OK.
FOURTH IDEA:
Is my configuration wrong?
I'm using the following:
SMTP Host: smtp.zoho.com (should I get the IP address instead?)
SMTP Port: 465
and I think I have encryption and authentication turned on. (My code is at the end if that helps.)
FIFTH IDEA:
Start over using gmail to see if that gives me any clues, unless someone has a better idea.
function send_SMTP_email($email, $name, $subject, $msg) {
//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_once '/inc/phpmailer/PHPMailerAutoload.php'; // will load class.phpmailer.php and class.smtp.php itself
require_once '/inc/config.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging, TO DO: Disable for production
// 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_HOST;
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = SMTP_PORT;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = SYSTEM_EMAIL;
//Password to use for SMTP authentication
$mail->Password = SYSTEM_PASSWORD;
//Set who the message is to be sent from
$mail->setFrom(SYSTEM_EMAIL, SYSTEM_NAME);
//Set an alternative reply-to address
$mail->addReplyTo(SYSTEM_EMAIL, SYSTEM_NAME);
//Set who the message is to be sent to
$mail->addAddress($email, $name);
//Set the subject line
$mail->Subject = $subject;
//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__));
//Send HTML OR Plain Text
$mail->isHTML(true);
//Set body
$mail->Body = "<p>" . $msg . "</p>";
//Include plain text if non-HTML email reader
$mail->AltBody = $msg;
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
$result = $mail->send();
//send the message, check for errors
//if (!$mail->send()) {
if (!$result) {
$return_val = "Mailer Error: " . $mail->ErrorInfo;
} else {
$return_val = "";
}
return $return_val;
}
I just had the same problem and finally found the problem. The default is set to TLS. Once I changed it to SSL with
$mail->SMTPSecure = 'ssl';
everything worked fine. Hopes this helps, if anyone else faces the same issue.
I created a form that uses phpMailer to email the results to the website owner. Of course, before I add the owner's email address I use mine to test that the form works. When I use my email the form sends perfectly, however, when I use the owner's address it throws the error "could not instantiate mail function" and won't send the form. The error only occurs with email addresses associated with the owner's domain. Any idea why this could be happening?
If I type this into the command line it works fine:
echo 'test' | mail -s 'test' me#example.com
edit: I wasn't initially using SMTP, but it's now configured as shown below. The error message is now "SMTP Error: The following recipients have failed xxx#somedomain.com" and the end result is still the same. It can e-mail to a number of gmail test addresses but has issue with the owner's email#hisdomain.com. Further, with SMTPDebug it's now producing the error "RCPT TO command failed: 550 No Such User Here" The owner's e-mail, however, works without issue when e-mailed through gmail, outlook, etc.
phpMailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = "error_log";
$mail->Host = "mail.mydomain.com";
$mail->SMTPAuth = true;
$mail->Username = "admin#mydomain.com";
$mail->Password = "XXXXXXXXXXXXXXX";
$mail->CharSet = 'UTF-8';
$mail->AddReplyTo($emailAddress);
$mail->SetFrom("admin#mydomain.com");
$mail->AddAddress($toAddress,$toName);
$mail->Subject = $emailSubject;
$mail->isHTML(true);
$mail->Body = $emailBody;
$mail->AltBody = strip_tags($emailBody);
// Attempt to send the e-mail
if (!$mail->send()) {
//error handling
}
There are couple of things you should try and check with this particular error message:
Make sure you can use regular php mail() function. Create a blank page and use the php mail() to send a test email. If that works, maybe its your SMTP that's having issues with the particular user domain. Setup gmail SMTP or a different SMTP to send emails:
$mail->IsSMTP();
$mail->Host = "smtp.domain.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Can you share your phpMailer source for us to view?
Set $mail->SMTPDebug = 2; so you can see what the server has to say, and read the troubleshooting guide.
You're using authentication without encryption, which is not a good combination and many servers won't allow that. Add this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You've based your code on an old example, so you're probably using an old version of PHPMailer too; get the latest from github.