I am using PHPMailer in order to send a mail as follows
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->isHTML();
$mail->setFrom('from#server.com', 'server');
$mail->addReplyTo('user#myfavoritemailprovider.com', 'user');
$mail->addAddress('admin#server.com');
$mail->Subject = $subject;
$mail->AltBody = getPlainMail($data);
$mail->Body = getHTMLMail($data);
try {
$mail->send();
echo "SUCCESS";
} catch(phpmailerException $e) {
echo "FAILURE: ", $e->errorMessage();
}
This works perfectly fine to send mails using MAMP on my good old Mac OS X, but when I run this code on the Linux server where it should end up, something strange happens to the mails. Spaces appearing randomly in the content and styles not being applied randomly turned out to be a consequence of blank lines between every two lines in the body of the received mail. For a simple example this could look like
<!DOCTYPE html><html><head><title>Test</title></head><bo=
dy><main><section style=3D"color:red;"><p style=3D"color=
:black;">test</p></section></main></body></html>
whereas it should be something like
<!DOCTYPE html><html><head><title>Test</title></head><bo=
dy><main><section style=3D"color:red;"><p style=3D"color=
:black;">test</p></section></main></body></html>
in order to be correct. I tried to figure out where these blank lines come from, but I could not find anything. The strangest thing might even be that when I replace $mail->send(); echo "SUCCESS"; by
$mail->preSend();
echo htmlspecialchars($mail->getSentMIMEMessage());
I get the expected result.
Would anyone have an idea where these blank lines are coming from? Any hints are appreciated
PHPMailer 5.2 has a problem with inconsistent line break formats. Have a try with the 6.0 branch (not yet released) which avoids the problem, or try setting $mail->LE = "\r\n"; and ensuring all your content uses the same line break format.
Related
Before you classify this post as a duplicate, please note that I've been working on this issue for days and I've reviewed all of the other posts.
Previous solution: Use the latest version of PHPMailer.
I've tried PHPMailer 6.2.0, 6.1.8 , 6.1.7 and older versions
I've tried using the composer and i've also tried using PHPmailer
without the composer.
Sometimes PHPMailer works and sometimes I get the same error. There's no rhyme or reason. I can run the following script and it works with no error, run it 5 seconds later then it doesn't work, run it again 5 seconds later and it works. What the heck?!
require_once("PHPMailer-master/PHPMailerAutoload.php");
also tried:
require "PHPMailer3/PHPMailer-6.2.0/src/PHPMailer.php";
require "PHPMailer3/PHPMailer-6.2.0/src/SMTP.php";
require "PHPMailer3/PHPMailer-6.2.0/src/Exception.php";
use PHPMailer\PHPMailer\PHPMailer;
Lets continue:
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "10.10.10.38";
$mail->Port = 25;
$mail->SMTPDebug = false;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SetFrom ("me#x.com", "me");
$mail->AddReplyTo("me#x.com", "me");
$mail->Subject = "I need help fixing you";
$mail->AltBody = "Yes It Worked!";
$body ="Yes It Worked!";
$mail->MsgHTML($body);
$mail->AddAddress("x#x.com", "x");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Another Previous solution: check the from address
At first i thought this was the solution because my from address was a variable but that's not it! I'm using a static from address.
Another Previous solution: the from address is using single quotes but everything else is using double quotes.
Tried that
I've also tried:
$mail->From = "me#x.com";
$mail->FromName = "me#x.com";
What else is there to try?
By the way, i'm running PHP 7.3.21 and i'm using an internal SMTP relay server. Please dont suggest using a different email server. I have to get it working with the current server.
Thanks,
The code you're using looks like it's been copied from a very old example, though I don't think that's the issue here.
require_once("PHPMailer-master/PHPMailerAutoload.php");
that file is obsolete and has been omitted from supported PHPMailer versions since 2017.
I can run the following script and it works with no error, run it 5 seconds later then it doesn't work, run it again 5 seconds later and it works.
This tells you something important – your script (and PHPMailer) is not the source of the problem. The most likely explanation is that your mail server, or a firewall between you and the mail server, has some kind of throttling or rate limiting that's preventing you sending all the time, resulting in inconsistent results.
You're also doing this:
$mail->SMTPDebug = false;
Since you're trying to debug an SMTP issue, don't you think that this might possibly be relevant? Especially since it's one of the first things referred to in the troubleshooting guide I'm sure you've seen already, and used and described in literally hundreds of answers on here. Your very first step should be to set $mail->SMTPDebug = 3; and read what the output says, and post it as part of your question.
I'm using PhpMailer to send e-mails. It's working fine.
But in some cases I don't want to wait for the send function to return, specially because it can take a while sometimes. I just want to send and finish the funcition right away.
Is it possible to achieve this?
This is a sample code I'm using (nothing different from the basics).
try {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
// ...other options...
$mail->send();
return true;
} catch (Exception $e) {
return $e;
}
You don't need to use ajax or configure your own queueing system to do this. Just use a local mail server - which implicitly has a built-in queueing system that you don't need to configure - and you can submit messages to it in a fraction of a second. There is some performance advice on the PHPMailer wiki.
Like jeroen mentioned, you should use queues to achieve this. If you ever configured the cron to work with PHP, this have the similar principle except that it doesn't repeat the same task by schedule but executes any task you provide one-by-one in a queue. You can even give priorities to these tasks. I suggest you to start with beanstalkd.
This is driving me up a wall!
I'm trying to use phpmailer to send e-mail. Here is my code:
print "about to do the require!";
require_once("$s[phppath]/phpmailer.php");
print "require has been successful! creating a PHPMailer object!";
$mail = new PHPMailer();
print "yay!";
However, the program never gets to "yay", it dies at line 4 ($mail = new PHPMailer();). Most maddening is that no error message is displayed, even though php is running with display_errors = On. To test this, I threw in some obvious errors, like skipping semi-colons at the end of lines, and I immediately get an error message about this.
What the heck is going on here?
Edited
I modified the code like this:
require_once("$s[phppath]/phpmailer.php");
try {
$mail = new PHPMailer(true);
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Still no dice.
I am using PHPMailer also. Now in 2018 Version 6.05.
I was also in trouble with creation of $mail = new PHPMailer; I always saw the message that the class could not be created. But I was sure the file with the class was included.
What I found out, was that there is a namespace used within the class PHPMailer.
So the new object does not exist as "PHPMailer" but as class within the namespace PHPMailer\PHPMailer ! (not to be confused with folders of your installation).
What finally worked for me was to write the following line:
$mail = new PHPMailer\PHPMailer\PHPMailer;
// You need to add the namespace part after new and before class PHPMailer
Ok, there were two parts to this question:
1) Why no error messages? I feel very, very stupid about this one, because though I had display_errors turned on, my own script actually disabled error_reporting. Thank you Pathik Ghandi for pointing out this very simple fix. I can be quite special at times.
2) Why was PHPMailer not working? Because apparently PHPMailer now requires TWO files instead of one: class.phpmailer.php is not enough. You now also need PHPMailerAutoload.php. Hope this helps someone down the line!
Try echoing out $s[phppath] to make sure you are getting the right path. Redownload and reupload the phpMailer lib. Try using require instead of require_once. Try using include instead of require. Go into the phpmailer.php file and add in an echo or something to make sure you are actually requiring that file.
To be sure; Are you running some kind of framework? ie Wordpress? If so you may need to set a flag to show PHP error messages. WP: define("WP_DEBUG", true);
Try to call restore_error_handler() function above require.
I am using PHPMailer to send email using a contact form on my web page. I am using the code
$msg = $mail->Send();
echo $msg;
but nothing is printed out.. Obviously I have all my email/domain settings above these lines. Funny thing is that the email is sending correctly, but I get no response (true OR false) from the send method, thus I cannot run an if statement to redirect to another page according to whether the email has been sent or not..
Replace your $mail->Send() call with:
if(!$mail->Send()) {
echo $mail->ErrorInfo;
}
The most likely reason you're getting no output when you echo $msg is because the result is false. When you echo false it will display nothing. Use var_dump instead of echo.
How do I disable the error messages from the PHPMailer class? I'm displaying my own error messages and I don't want users to see errors such as "SMTP Error: Could not connect to SMTP host."
Thanks
I know this thread is old and already answered but I stumbled here because I had the same problem but ending up solving it differently so I thought I'd share. NOTE: I'm using PHPMailer v5.1.
When you instantiate the PHPMailer class, it takes one optional argument, $exceptions. That tells PHPMailer if it should throw exceptions if it encounters any. It defaults to false which means it does not throw any exceptions, just echoes its messages out. However, if you call it like
$mail = new PHPMailer(true);
you will tell it to throw exceptions. You can then catch those exceptions and deal with them however you choose. To me, that is much cleaner and more elegant than messing with the source code or disabling the error reporting.
This is the way PHPMailer wants you to do it; does not involve editing the original class file.
$mail->SMTPDebug = false;
$mail->do_debug = 0;
This is not probably the best solution, but it works.
In your phpmailer library folder open "class.phpmailer.php",
find
public function Send()
inside it comment the line
echo $e->getMessage()."\n";
There are two ways that work pretty much the same as of May 2020
$mail->SMTPDebug = 0;
OR
$mail->SMTPDebug = false;
There's a better solution (I think :) ), it's an improved version of the solution sugested by Riccardo
In the file "class.phpmailer.php", within the function
public function Send()
find the line
echo $e->getMessage()."\n";
and replace it for this
if ($SMTPDebug)
echo $e->getMessage()."\n";
In this way, it will only show the exceptions messages (or error messages) if you are running in debug mode..
Hope it helps!!
regards!!
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
Here's another solution:
ob_start(); //start output buffering to capture all output and prevent errors from being displayed
$delivery = $mail->Send();
ob_end_clean(); //erase the buffer and stop output buffering
I needed to suppress errors from the JUtility::sendMail function in Joomla, which uses PHPMailer behind the scenes, so the above answers didn't work for me but output buffering did the trick.
If the errors are coming from the call to $mail->Send(); then you can do something like this:
$oldErrorSetting = error_reporting(0); // temporarily disable warnings
$delivery = $mail->Send();
error_reporting($oldErrorSetting); // restore error setting
This will suppress PHP errors that come from the Send call.
Then, to get PHPMailer's errors and do something nice with them, you can use the $delivery return value and ErrorInfo:
if (!$delivery) {
$errorDetails = $mail->ErrorInfo;
print "<p>$errorDetails</p>\n";
}
I'm having a problem with the error: Can not modify header information - headers already sent by (output started at /phpMailer/class.phpmailer.php) because my return json needs header ('Content-Type: application / json') ; And was giving error because the class class.phpmailer.php printed on the screen the error Invalid address: which generates the error of the header so to solve it was simple, it only includes in the class class.phpmailer.php in the following line the debug check and it worked Perfectly follows
This line
Echo $ this-> Lang ('invalid_address'). ':'. $ Address;
Change to:
If ($ this-> SMTPDebug) {
Echo $ this-> Lang ('invalid_address'). ':'. $ Address;
}
Delete below code if included
$mail->SMTPDebug = SMTP::DEBUG_SERVER;