1

I need to know if PHPMailer was unable to send an email. But even sending to a fake email address returns true:

$phpmailer = new PHPMailer( true );
$phpmailer->setFrom( "myemail@myemailladdy.com", "myemail@myemailladdy.com" );

//This is definitely not reachable
$phpmailer->addAddress( "fake@shdsabdasdiuahsdiuhaiduhasidsjdfake.com", "IJustPressedRandomKeys" ); 

$phpmailer->Subject = "fake";
$phpmailer->Body = "fake";
echo "Is Mail: " . $phpmailer->IsMail();

//This prints "1"
echo "Was Sent: " . $phpmailer->send();

Why is this returning 1/true?

(When the email is valid, I do recieve the emails, so PHPMailer is setup correctly)

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • 3
    When it's a fake, you should be getting an email back as "Unknown recipient" as a bounce back. PHPMailer has done its job when sending out the mail in turn returning TRUE. It doesn't care who it's sent out to, just as long as the email "seems" well-formed, which it is. – Funk Forty Niner Sep 23 '14 at 00:15
  • @Fred-ii- Notice, the **domain* name in the email address is fake. So PHPMailer wouldn't even get a valid `helo` or `ehlo`. – Don Rhummy Sep 23 '14 at 00:16
  • John read my thoughts. See his answer. My fingers started cramping up. – Funk Forty Niner Sep 23 '14 at 00:18

2 Answers2

5

PHPMailer does not know whether an email address is real or not. The mail server won't know until it sends the email and gets a rejection response. But the handoff between the server and PHP has already been terminated by that point.

There is no real way to verify an email address exists without sending an email to it and getting either a response or having the user enter a unique token into a web form. The closest you can get is verifying MX records or other DNS information that verifies a domain exists, etc. But that will not be perfect and will have false positives as well as letting fake emails through if the domain is valid.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Just like I said lol you took it further by reading my thoughts. I can't type that fast ;) – Funk Forty Niner Sep 23 '14 at 00:17
  • @JohnConde when you say between the server and PHP, do you mean between the `mail` program and PHP? Because sending via PHP's mail function does not require an email server, it sends via SMTP. Are you certain PHPMailer does not open a socket to send the SMTP commands? Or that "mail()` does not report that status back to PHPMailer? – Don Rhummy Sep 23 '14 at 00:20
  • 1
    The mail server and PHP. The mail server will not get an instant response about the status of the email. Only that it is sent successfully. The rejections comes outside of this sequence. – John Conde Sep 23 '14 at 00:22
-1

I wos thinking about it for a while... and i think i have a nice solution.

If there will be some kind big trouble:

if(!$mail->Send()) {
    echo $mail->ErrorInfo; // this is important for you
    // other functions...
}

or if will be success?

else {
    $smtp_msg = 'ALL OK'; // sets the message you want to see
    if ($mail->ErrorInfo != '') { // check if there wos any other error
        $smtp_msg = $mail->ErrorInfo; // if yes - show it to me
    }
    // else is optional but no need couse if there wos no error we already set $smtp_msg = 'ALL OK';
    return $smtp_msg;
}

Or even better you could try to use codes of exrrors to show youre own messages...

Or... use try/catch like here: Error handling with PHPMailer

This could be helpfull!

Community
  • 1
  • 1
TomLi
  • 123
  • 12
  • 1
    please read the answer. your code will not catch the problem because PHP does not stay for the reply from the SMTP server – Don Rhummy Mar 12 '15 at 15:49
  • You wanted to inform user if the mail wos sent right? This code allows you to send the email and gets a rejection response witch you want to show to user... Ok i agree it wont tell you before sending an email - if is ok or not... but after trying to send it the responce is helpfull. – TomLi Mar 13 '15 at 09:52
  • please read the answer. as @JohnConde points out, there is no way for PHPMailer to inform the code if the email was sent to the recipient. It can only inform you if it went to the mail command correctly – Don Rhummy Mar 13 '15 at 15:50
  • You are wrong. You say there is no way for PHPMailer to inform the code if the email wos send to recipent... John Conde said "The mail server won't know until it sends the email" ... "Until it sends". My solution is using feedback from PHPMailer "after it sends email" – TomLi Mar 18 '15 at 07:48
  • Like Don Rummy says, my thoughts are there as well. $mail will only get an ErrorInfo if there's an actual typing error in the address. testing@gmail,com (notice the comma) for example. ErrorInfo would say something about that. But thisisfakecreepo@gmail.com would return true on ->Send() – anoraq Nov 25 '15 at 09:53