您如何使用 PHP 从 IMAP 帐户发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4512921/
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
How do you send email from IMAP account with PHP?
提问by arthurakay
I'm having an issue sending email via PHP/IMAP - and I don't know if it's because:
我在通过 PHP/IMAP 发送电子邮件时遇到问题 - 我不知道是不是因为:
- I don't correctly understand IMAP, or
- there's an issue with my server
- 我没有正确理解 IMAP,或者
- 我的服务器有问题
My application opens an IMAP connection to an email account to read messages in the inbox. It does this successfully. The problem I have is that I want to send messages from this account and have them display in the outbox/sent folder.
我的应用程序打开与电子邮件帐户的 IMAP 连接以阅读收件箱中的邮件。它成功地做到了这一点。我的问题是我想从此帐户发送邮件并将它们显示在发件箱/已发送文件夹中。
As far as I can tell, the PHP imap_mail() function doesn't in any way hook into the IMAP stream I currently have open.
据我所知,PHP imap_mail() 函数不会以任何方式挂接到我当前打开的 IMAP 流中。
My code executes without throwing an error. However, the email never arrives to the recipient and never displays in my sent folder.
我的代码执行时不会抛出错误。但是,电子邮件永远不会到达收件人,也永远不会显示在我的已发送文件夹中。
private function createHeaders() {
return "MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=iso-8859-1" . "\r\n" .
"From: " . $this->accountEmail . "\r\n";
}
private function notifyAdminForCompleteSet($urlToCompleteSet) {
$message = "
<p>
In order to process the latest records, you must visit
<a href='$urlToCompleteSet'>the website</a> and manually export the set.
</p>
";
try {
imap_mail(
$this->adminEmail,
"Alert: Manual Export of Records Required",
wordwrap($message, 70),
$this->createHeaders()
);
echo(" ---> Admin notified via email!\n");
}
catch (Exception $e) {
throw new Exception("Error in notifyAdminForCompleteSet()");
}
}
I'm guessing I need to copy the message into the IMAP account manually... or is there a different solution to this problem?
我猜我需要手动将消息复制到 IMAP 帐户中……或者是否有针对此问题的不同解决方案?
Also, does it matter if the domain in the "from" address is different than that of the server on whicn this script is running? I can't explain why the message is never sent.
此外,如果“发件人”地址中的域与运行此脚本的服务器的域不同,这是否重要?我无法解释为什么永远不会发送消息。
回答by webbiedave
imap_mail
is just a wrapper to the sendmail binary like the mail
function. You're better of sending the mail through normal SMTP means (take a look at PHPMaileror Swift Mailer) and then have your code place the message into the Sent folder with imap_append.
imap_mail
只是像mail
函数一样的 sendmail 二进制文件的包装器。您最好通过普通的 SMTP 方式发送邮件(看看PHPMailer或Swift Mailer),然后让您的代码将邮件放入带有imap_append的 Sent 文件夹中。
Not sure what is making the send fail but you should check the boolean return value of imap_send
in your code. Beyond that, you'll want to examine your SMTP server logs for any clues.
不确定是什么导致发送失败,但您应该检查imap_send
代码中的布尔返回值。除此之外,您还需要检查 SMTP 服务器日志以获取任何线索。
回答by namezero
It's been a while, but for the records for others you can get the IMAP erorr via imap_last_error(). This will give you an error string of what the last issue was.
已经有一段时间了,但是对于其他人的记录,您可以通过 imap_last_error() 获取 IMAP 错误。这将为您提供上一个问题的错误字符串。
回答by Oswald
Check the return value of your call to imap_mail
. imap_mail
does not throw na exception on failure, it returns false
.
检查您对 的调用的返回值imap_mail
。imap_mail
失败时不会抛出 na 异常,它返回false
.
Also, you will have to copy the message to the sent-folder yourself, imap_mail
does not do that for you.
此外,您必须自己将消息复制到已发送文件夹中,而imap_mail
不会为您这样做。