php - 检测是否发送了电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14191446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php - detect if email is sent
提问by Kiel
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 语句中,但这里的代码不起作用。
if( @$send = mail($emailRecipient, $subject, $message, $headers)){
//do something
}else{
//do something
}
回答by Daya
if(@mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
回答by Brenton Alker
Firstly, I'd suggest using a third party mail library (SwiftMailer, PHPMailer, Zend_Mail...) for sending email instead of the built in mailfunction. Composing mail is more complicated than most people realize, especially if you want to do multipart and/or HTML formatted email.
首先,我建议使用第三方邮件库(SwiftMailer、PHPMailer、Zend_Mail...)来发送电子邮件而不是内置mail函数。撰写邮件比大多数人意识到的要复杂,尤其是当您想要制作多部分和/或 HTML 格式的电子邮件时。
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.
您唯一可以(并且确实应该)做的事情就是确保您的系统以理智的方式处理退回的电子邮件。例如。如果电子邮件地址不断退回,请考虑取消订阅该地址。
回答by jmkeyes
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.
调用 to 的结果mail()仅表明 PHP 能够将电子邮件发送到 MTA,但不会表明电子邮件是否已实际发送或收件人是否实际收到。
If you want more reliability, you have to use something otherthan 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.
如果你想要更高的可靠性,你必须使用一些其他的比mail()。虽然我从未使用过它,但 PHPMailer 或其他 SMTP 库可能会给你从 MTA 返回的信息,它会告诉你更多关于排队传递的信息,但不会轮询退回邮件(可能会或可能不会传递到发件人的收件箱)您无法判断收件人是否收到了电子邮件。
回答by Milo? ?akonovi?
Only for sake of completenessit should be mentioned that there is a way to send an email via PHPand 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.
只是为了完整起见,应该提到有一种方法可以PHP通过接收 MTA发送电子邮件并知道它是否真的被接收(同样,这并不意味着它被传递给用户或在 SMTP 握手后静默丢弃) 或不。
One could theoretically write an SMTP client purely in PHP, for example using built-in Stream functionslike stream_socket_clientin order to talk to receiving MTA via direct raw tcp connections formed in SMTP protocol requests and responses.
理论上可以完全用 编写一个 SMTP 客户端PHP,例如使用像stream_socket_client这样的内置Stream 函数,以便通过在 SMTP 协议请求和响应中形成的直接原始 tcp 连接与接收 MTA 对话。
General logic of issuing SMTP commands would be like:
发出 SMTP 命令的一般逻辑如下:
<?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 PHPlanguage has to be performed.
当然,这显然是不完整的(有经验的开发人员会注意到我既没有听也没有解析我得到的 SMTP 响应)。为了使用这种方法,必须在PHP语言中认真地重新发明轮子。
How would this be used to detect if email is sent?
这将如何用于检测是否发送了电子邮件?
Well, you would have at least 3 types of information:
那么,您至少会拥有 3 种类型的信息:
- whether receiving host is alive and gets email messages at all
- whether you'r not bounced during handshake (SMTP 5xx responses class)
- whether you got SMTP
250 2.0.0 Okafter submitting message text
- 接收主机是否还活着并接收电子邮件
- 您是否在握手期间没有被退回(SMTP 5xx 响应类)
250 2.0.0 Ok提交消息文本后是否收到 SMTP
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.
同样,这仅用于教育目的,除非您准备好与开发符合标准的 SMTP 客户端的项目相结合,否则不要在家里这样做。
回答by Rushil Pachchigar
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:
我想在@Daya 的回答中添加更多信息,我想在他的回答中做一些修改,如下所示:
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:
如果电子邮件将成功发送,则不是问题,但如果邮件功能有错误,那么开发人员将如何获取错误,为此他/她可以使用error_get_last()获取最后一个错误的功能,修改如下:
if(!$mail) {
print_r( 'Mailer error: ' . error_get_last());
} else {
echo 'Message has been sent.';
}
回答by user1600211
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";
回答by Praveen PL
$send = mail($emailRecipient, $subject, $message, $headers);
$send = mail($emailRecipient, $subject, $message, $headers);
Check the returned value of $sent like If($send['error_code'] == 0) { echo "successful"; } Else { Echo "error"; }
检查 $sent 的返回值,如 If($send['error_code'] == 0) { echo "successful"; } Else { 回声“错误”;}

