Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a email was sent successfully

Tags:

php

email

All, I have the standard mail code to send an email in PHP.

$to = $resultset['email_address'];
$subject = "New client inquiry from Website.com";
$message = $email_message;
$from = $your_email;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers,"-f $from");

The variables are created earlier in my code. However what I want to check is to make sure that the mail function was executed successfully. With this code, how can I determine if sent the email and if it didn't echo out "failure"?

like image 776
user1048676 Avatar asked Dec 17 '22 03:12

user1048676


1 Answers

Simply said: This is not possible with the PHP mail() command.

the return value from mail() just indicates, whether the mail was successfully handed over to the MTA, but not if it was sent. If e.g. your MTA is postfix, and the postfix service is stopped, mail() will happily return true, as queueing the mail to postfix worked. It will however never be sent, if postfix is not manually started (or even correctly configured).

If you really want to make sure, the mail has been sent, you need to talk to a MTA via sockets. There are frameworks for that.

like image 65
Eugen Rieck Avatar answered Jan 06 '23 15:01

Eugen Rieck