Reputation: 493
Im building an automated newsletter, im kinda stuck with this problem. I need to know if the email was sent or not. Here is my code
@$send = mail($emailRecipient, $subject, $message, $headers);
i tried to add it to an if statement but it does not work here is the code.
if( @$send = mail($emailRecipient, $subject, $message, $headers)){
//do something
}else{
//do something
}
Upvotes: 14
Views: 96899
Reputation: 1321
I'd like to add a bit more information in @Daya's answer and I want to do bit modification in his answer and that is following:
If the email will send successfully then not an issue but if mail function has error then how could developer will get the error and for that he/she can use error_get_last()
function for getting last error and the modification is following:
if(!$mail) {
print_r( 'Mailer error: ' . error_get_last());
} else {
echo 'Message has been sent.';
}
Upvotes: 0
Reputation: 39
$send = mail($emailRecipient, $subject, $message, $headers);
Check the returned value of $sent like If($send['error_code'] == 0) { echo "successful"; } Else { Echo "error"; }
Upvotes: -4
Reputation: 3871
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:
<?php
$fp = stream_socket_client("tcp://fqdn-of-receiving-mta:25", $errno, $errstr, 90);
fwrite($fp, "EHLO LOCALHOST\r\n");
fwrite($fp, "MAIL FROM: [email protected]\r\n");
fwrite($fp, "RCPT TO: [email protected]\r\n");
fwrite($fp, "DATA\r\n");
fwrite(
$fp,
"Subject: your subject text\r\n" .
"First line of message body\r\n" .
"At the end of the message body one single period char should be placed before and after EOL character\r\n" .
"Like this: \r\n" .
".\r\n"
);
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.
Well, you would have at least 3 types of information:
250 2.0.0 Ok
after submitting message textAgain, 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.
Upvotes: 2
Reputation: 1
You can also try this
$send = mail($to,$subject,$msg);
if($send)
{
echo "Your Account is Successfully Created. You must Activate your account.";
}
else
echo "Failed to send";
Upvotes: 0
Reputation: 9072
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.
Upvotes: 13
Reputation: 3771
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.
Upvotes: 5
Reputation: 1170
if(@mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
Upvotes: 34