PHP mail()函数&PHMailer疑惑

PHP mail() function & PHMailer doubts

我对 PHP 中的邮件选项几乎没有疑问。

  1. 无论我读到什么,如果没有 SMTP,我都无法从本地主机发送邮件。正确吗?

  2. 为了使用 PHP mail() 函数从本地主机发送邮件,我需要对这两个文件进行少量更改。一个是 'php.ini',另一个是 'sendmail.ini'。更改设置 smtp_server、端口、用户名和密码。这个选项非常适合我,我已经用它发送了邮件。只是想确认我是否按照正确的方式从本地主机发送邮件?

  3. 如果我只需要发送简单的邮件,mail() 函数和 PHPMailer 之间有什么区别吗?

  4. 更改两个 ini 文件(php.ini & sendmail.ini)后,我使用的是页面末尾“https://github.com/PHPMailer/PHPMailer”中给出的示例,但该示例不起作用。我在 PHPMailer 示例中使用的 SMTP 与我在 'sendmail.ini' 中使用的相同,但仍然无法正常工作。但是如果我在 PHPMailer 示例中注释掉 SMTP 部分,即

    /*$mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'user@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; //Enable TLS encryption,sslalso accepted $mail->Port = 587; */

然后它工作得很好。我不知道为什么我必须注释掉 SMTP 部分?

请帮我解开疑惑。 提前致谢

Whatever I have read, I can not send mail from localhost without SMTP. Is it correct ?

您需要有邮件服务器(如linux sendmail)才能发送邮件。您可以使用服务器的 built-in 邮件 post 服务器或连接到 SMTP。 PHP 没有 built-in 邮件服务器/"postman"

For sending the mail from localhost using the PHP mail() function, I need to make few changes in these two files. one is 'php.ini' & another one is 'sendmail.ini'. changes like setting smtp_server, port, username & password. This option is working for me perfectly & I have sent mail using this. Just want to confirm that am I following the correct way for sending the mail from localhost ?

只要您正确设置 hostname/domain/dns 记录就可以了,否则您的邮件将进入 junk/spam 邮箱。阅读有关 SPF 记录的更多信息。

If I just have to send simple mails, is there any much difference between mail() function & PHPMailer ?

PHPMailer 默认使用 mail()。 PHPMailer interface/layer 对于 mail() 来说既好用又简单,因此您不必自己编写复杂的邮件 headers。如果是 STMP phpmailer 使用 sockets

After changing the two ini files(php.ini & sendmail.ini), I am using the example given on 'https://github.com/PHPMailer/PHPMailer' at the end of the page, but that example is not working. I am using the same SMTP in the PHPMailer example which I have used in 'sendmail.ini', but still that is not working. But If I comment out the SMTP part in PHPMailer example, which is

看看 echo 'Mailer Error: ' . $mail->ErrorInfo; 说了什么

所以你之前写的是正确的,如果你想从你的本地机器发送电子邮件,你需要一个 MTA(邮件传输代理)。

如果您使用 Linux 或 Windows,您可以安装一个 Postfix 有很好的工具可以制作一个本地邮件服务器,它可以捕获您所有的本地邮件。配置本地 Postfix 以捕获所有传入邮件有点复杂。

https://www.hmailserver.com/

PHP 中的默认 mail 函数使用 sendmail 向全世界发送电子邮件。当你有像 PHPMailer 或我喜欢的 SwiftMailer 这样的库时。然后你有很多功能,比如通过 SMTP 发送。

通常 sendmail 将您的电子邮件重定向到现有的 MTA。 Sendmail 是早期的遗留物,大多数程序都与 sendmail 兼容。

还有你最后的问题。如果您注释掉 $mail->isSMTP(); 行,那么您将通过本地中继 (sendmail) 发送邮件,所以您可能还没有安装 openssl?并且您无法通过 TLS 连接到您的 smtp 服务器。您应该检查端口 587 是否打开且可用。

要获得更好的错误信息,您可以使用 $mail->ErrorInfo; 查看发生了什么。