首页 > 解决方案 > 我可以在没有 Gmail 的情况下使用本地主机通过 PHP 邮件程序发送电子邮件吗?

问题描述

我可以在不使用 Gmail 的情况下使用本地主机通过 PHP 邮件程序发送电子邮件吗?或使用我的本地主机的任何其他电子邮件服务,如 Hotmail、Outlook?

标签: phpmailer

解决方案


是的,这实际上是使用 PHPMailer 发送的最佳方式。你需要两件事:

  1. 允许出站 SMTP 的托管服务提供商。大量提供商默认阻止 SMTP,但您可能会发现他们会根据请求删除阻止 (scaleway.com),提供替代服务 (AWS),这也是可以接受的。
  2. 本地安装的邮件服务器。如果你在 Linux 上,像 postfix 这样的东西是一个不错的选择。

然后它的工作方式是这样的:

  1. localhost像往常一样编写您的 PHPMailer 脚本,但通过 SMTP(而不是通过mail())传递
  2. 将您的邮件服务器配置为充当“完整”邮件服务器,尝试直接传递邮件,或充当“智能主机”以通过其他服务(例如 gmail 或 mailgun)进行中继。

The main advantages of this approach are:

  1. Perceived performance: your message submissions will appear to be instant, so it's great for things like contact forms
  2. Reliability: if your message can't be delivered immediately, the local mail server will automatically take care of queuing and redelivery attempts, in a way that is far more efficient than you would ever achieve if you handled that within your own application.

As with many things, with great power comes great responsibility, and you will have to deal with bounce handling, blacklisting, inbound filtering (or just block it), etc. It's probably not worth doing all this for a simple contact form, but if you have an application that generates a reasonable amount of email traffic (signups, password resets, notifications, etc), it's a great way to go.


推荐阅读