如何使用 PHP 从 IMAP 帐户发送电子邮件?

发布于 2024-10-09 03:54:51 字数 1305 浏览 3 评论 0原文

我在通过 PHP/IMAP 发送电子邮件时遇到问题 - 我不知道是否是因为:

  • 我没有正确理解 IMAP,或者
  • 我的服务器有问题

我的应用程序打开与电子邮件帐户的 IMAP 连接以进行阅读收件箱中的消息。它成功地做到了这一点。我遇到的问题是我想从该帐户发送邮件并将它们显示在发件箱/已发送文件夹中。

据我所知,PHP imap_mail() 函数不会以任何方式挂钩到我当前打开的 IMAP 流。

我的代码执行时没有抛出错误。但是,电子邮件永远不会到达收件人,也永远不会显示在我的已发送文件夹中。

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()");
    }
}

我猜我需要手动将邮件复制到 IMAP 帐户中...或者是否有其他解决方案来解决此问题?

另外,如果“发件人”地址中的域与运行该脚本的服务器的域不同,这有什么关系吗?我无法解释为什么该消息从未发送。

I'm having an issue sending email via PHP/IMAP - and I don't know if it's because:

  • I don't correctly understand IMAP, or
  • there's an issue with my server

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.

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.

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以往的大感动 2024-10-16 03:54:51

imap_mail 只是 sendmail 二进制文件的包装器,就像 mail 函数一样。您最好通过正常的 SMTP 方式发送邮件(查看 PHPMailerSwift Mailer),然后让您的代码使用 imap_append

不确定是什么导致发送失败,但您应该检查代码中 imap_send 的布尔返回值。除此之外,您还需要检查 SMTP 服务器日志以获取任何线索。

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 PHPMailer or Swift Mailer) and then have your code place the message into the Sent folder with imap_append.

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.

你穿错了嫁妆 2024-10-16 03:54:51

已经有一段时间了,但为了其他人的记录,您可以通过 imap_last_error() 获取 IMAP 错误。这将为您提供最后一个问题的错误字符串。

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.

月光色 2024-10-16 03:54:51

检查 imap_mail 调用的返回值。 imap_mail 失败时不会抛出异常,而是返回 false

另外,您必须自己将邮件复制到发送文件夹,imap_mail 不会为您执行此操作。

Check the return value of your call to imap_mail. imap_mail does not throw na exception on failure, it returns false.

Also, you will have to copy the message to the sent-folder yourself, imap_mail does not do that for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文