Php test if mail sent

How to Test and Fix the php mail() Function

The php mail() function allows you to send emails directly from a php script.
This tutorial shows you how to test if php mail is working correctly.

Test php mail() on your Web Server

1. Create a php test file using a text editor and save it e.g. as test.php:

$sender = ‘ someone@somedomain.tld ‘;
$recipient = ‘ you@yourdomain.tld’ ;

$subject = «php mail test»;
$message = «php test message»;
$headers = ‘From:’ . $sender;

if (mail($recipient, $subject, $message, $headers))
echo «Message accepted»;
>
else
echo «Error: Message not accepted»;
>
?>

2. Change the $sender and $recipient in the code.

3. Upload the php file to your webserver.

4. Open the uploaded php file in your browser to execute the php script.

5. The output show either «Message accepted» or «Error: Message not accepted».

chevron_right Message accepted:

Open your mail client to see if the message was delivered to the specified email address.
(also check your spam folder!)

If the message was delivered:

If the message was not delivered:

  • Some provider don’t allow external recipients when using php mail. Change the recipient ($recipient) in the code to a local recipient. This means use an email address from the server’s domain, for example if your server domain is www.yourdomain.tld then the recipient’s email should be someone@yourdomain.tld.
  • Upload the modified php file and retry.
  • If it’s still not working: change the sender ($sender) to a local email (use the same email as used for recipient).
  • Upload the modified php file and retry.
  • Contact your provider if it still does not work.
    Tell your provider that the standard php «mail()» function returns TRUE, but not mail will be sent.
    It’s recommended to include the used php test script to show your provider, that the problem is not caused by the php script used.

chevron_right Error: Message not accepted

php mail might not be enabled:

  • Login to your webserver configuration and check the php settings.
    Some php installations require that you setup a default sender in the php configuration first.

Some provider don’t allow external recipients when using php mail:

  • Change the recipient ($recipient) in the code to a local recipient. This means use an email address from the server’s domain, for example if your server domain is www.yourdomain.tld then the recipient’s email should be someone@yourdomain.tld.
  • Upload the modified php file and retry.
  • If it’s still not working: change the sender ($sender) to a local email (use the same email as used for recipient).
  • Upload the modified php file and retry.

If you have checked the php settings, tried a local recipient and it’s still not working:

  • Contact your provider if you are unable to enable php mail().
    Tell your provider that the standard php «mail()» function returns FALSE.
    It’s recommended to include the used php test script to show your provider, that the problem is not caused by the php script used.

Disclaimer: The information on this page is provided «as is» without warranty of any kind. Further, Arclab Software OHG does not warrant, guarantee, or make any representations regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise. See: License Agreement

  • ©1997-2023 Arclab®. All other trademarks and brand names are the property of their respective owners.
  • info_outline
  • fingerprint Cookies & Privacy

Источник

php — detect if email is sent

From the mail docs: Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. — in other words, you cannot detect with PHP if the email was sent.

I believe the problem was that you were checking if $send managed to be set as the output of mail, which would always be true regardless of whether the mail sent successfully or not. What needed to be done was to either just put the mail() statement in the condition or set $send = mail(); before the if statement and just put $send in the condition.

7 Answers 7

if(@mail($emailRecipient, $subject, $message, $headers)) < echo "Mail Sent Successfully"; >else

This simply checks if the local mail server (or the SMTP configured in php.ini) accepted the outgoing email. The mail server will then try to send the email to the recipient’s mail server, but this is done after PHP’s mail() returns, as explained in other answers.

Firstly, I’d suggest using a third party mail library (SwiftMailer, PHPMailer, Zend_Mail. ) for sending email instead of the built in mail function. Composing mail is more complicated than most people realize, especially if you want to do multipart and/or HTML formatted email.

Secondly, beyond checking if the message was successfully delivered to the first (usually local) email service, it is pretty much impossible to determine if an email was sent. This is due to the way email inherently works and there is little than can be done about it.

The only thing you can (and really should) do, is make sure your system handles bounced emails in a sane way. eg. If an email address continuously bounces, consider unsubscribing the address.

Thanks brenton for the advice ill use this in the future as of now i just need to know if ive sent the email. wether the user receives it or not. thanks guys!

Here’s the truth: unfortunately you can’t reliably detect if an email was either sent or received: email is not a reliable form of communication.

The result of a call to mail() only indicates that PHP was able to send the email to an MTA, but that will not indicate whether or not an email was actually sent or that the recipient actually received it.

If you want more reliability, you have to use something other than mail() . Although I’ve never used it, PHPMailer or another SMTP library may give you the returned information from the MTA, which will tell you more about the queued delivery, but without polling for a bounce message (which may or may not be delivered to the sender’s inbox) you have no way of telling if a recipient received the email.

Only for sake of completeness it should be mentioned that there is a way to send an email via PHP and to know whether it is really accepted by receiving MTA (which, again, doesn’t mean it is delivered to user or discarded silently after SMTP handshake) or not.

One could theoretically write an SMTP client purely in PHP , for example using built-in Stream functions like stream_socket_client in order to talk to receiving MTA via direct raw tcp connections formed in SMTP protocol requests and responses.

General logic of issuing SMTP commands would be like:

Of course, this is blatantly incomplete (experienced devs would notice that I wasn’t neither listening nor parsing to SMTP responses I’d get). In order for this approach to be used serious re-inventing the wheel in PHP language has to be performed.

How would this be used to detect if email is sent?

Well, you would have at least 3 types of information:

  1. whether receiving host is alive and gets email messages at all
  2. whether you’r not bounced during handshake (SMTP 5xx responses class)
  3. whether you got SMTP 250 2.0.0 Ok after submitting message text

Again, this is only for educational purposes, don’t do this at home unless you’re prepared to marry the project of developing standard-compliant SMTP client.

Источник

how to test mail() using PHPUnit

is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly? I have to test a code which uses PHP function mail() . With custom mailer class i could always make a mock, but for mail() . ? Maybe there is some plugin which is capable to use IMAP and verify if mail is received? (and it should be OS-agnostic if it is possible. )

3 Answers 3

The solution here would be to wrap mail in a class that could be mocked and use that instead.

I don’t see the point in testing mail() itself, I’m sure it’s been thoroughly tested already.

@ts Artefacto’s point still stands: The best idea would be to set up a wrapper class that can mock mail() in the unit test (and, say, always return true unless the recipient E-Mail address is not a valid one.)

There is a project called MailCatcher that can help assure you your email has (a) been sent and (b) is constructed as intended (it has the content you put into it). Note this program does not verify that your email is receivable (ie: not marked as spam or rejected by a mail server for other reasons)

Mailcatcher is a local SMTP service and web interface to help you verify emails sent by your code. In addition, emails can be programmatically verified by using the API: eg: /messages, /messages/:id.json, /messages/:id.html etc. To use the API you’ll need something like Guzzle to make the HTTP calls. The project page is http://mailcatcher.me/

is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly?

If you want to check whether or not mail was sent successfully, you don’t need phpunit, you simply do:

mail(. ) or die('Could not send the email !!'); 

Note that this tells you whether or not mail was sent NOT whether or not it was received to whom the email was sent. So the better term here should be delivered.

Источник

How do I know if an email has been delivered using PHP?

I’m sending a bunch of emails out through a script in PHP, and I want to analyse the outcome of each so I know if it was delivered or not. Anyway of doing that through PHP?

3 Answers 3

You can check it’s been sent, but checking it’s been delivered is another thing entirely.

In fact, I can’t think of any way to confirm it’s been delivered without also checking it gets read, either by including a 1×1 .gif that is requested from your server and logged as having been accessed, or a link that the user must visit to see the full content of the message.

Neither are guaranteed (not all email clients will display images or use HTML) and not all ‘delivered’ messages will be read.

You can of course, monitor your reply-to address for any bounced mail which will let you say with certainty that a message hasn’t been delivered.

If a mail has been send can be detected easily, detecting if it has been delivered is a different case.

What you could do is implement a 1px image in the e-mail, which is hosted on your site. The image is ofcourse created by PHP, so you can give it an unique ID to detect if the image has been loaded. If you disable the cache on it, you could probably even detect how often the mail gets read.

 header('Content-Type: image/png'); $im = @imagecreate(1, 1); $background_color = imagecolorallocate($im, 255, 255, 255); // make it white imagepng($im,"image.png"); imagedestroy($im); 

Store this on a page called image.php (or something, up to you). Store also in a database which e-mails has been send and create a unique ID for it (md5 or sha1 hash would be nice)

$id = sha1([ID FROM DATABASE] + salt) 

store this also in the database on the row of the e-mail.

Then link to the image with the ID

Источник

Читайте также:  Python sqlite for linux
Оцените статью