如何使用 PHP 在 heroku 上发送电子邮件?

Posted

技术标签:

【中文标题】如何使用 PHP 在 heroku 上发送电子邮件?【英文标题】:How do I send email on heroku using PHP? 【发布时间】:2013-09-13 23:24:37 【问题描述】:

我已经创建了 php 应用程序并使用 heroku 进行了部署。在应用程序中,我有联系表格,可以将邮件发送到我的 gmail 帐户。为了实现这一点,我编写了以下 PHP 代码

<?php
   $to = "to@gmail.com";
   $subject = "This is subject";
   $message = "This is simple text message.";
   $header = "From:from@yantranet.com \r\n";
   $retval = mail ($to,$subject,$message,$header);
   if( $retval == true )
      echo "Message sent successfully...";
   else
      echo "Message could not be sent...";
   
?>

我的 php 脚本正在执行。但它没有发送邮件,而是显示Message could not be sent。任何人都可以帮助使用 PHP 实现联系表单吗?

【问题讨论】:

【参考方案1】:

Heroku 不允许发送电子邮件,您必须使用外部 SMTP 服务器。

“要从部署到 Heroku 的应用程序发送电子邮件,请使用外部 SMTP 服务。” https://devcenter.heroku.com/articles/smtp

这里描述了如何发送电子邮件抛出 SMTP:Sending email with PHP from an SMTP server

【讨论】:

【参考方案2】:

在没有附加组件的情况下,通过 Heroku 上的 smtp 发送电子邮件并不容易。

我建议您查看Sendgrid Heroku Add-on Sendgrid 是一个灵活的 Web 和 SMTP API,加上一个简单的 SMTP 中继设置,让您可以决定哪种集成方法适合您的环境。

检查一下,它应该是问题的解决方案,因为它解决了我的问题。

更新

对于初学者,试试 sendgrid,它使用起来很简单。但请注意,Sendgrid 只是可以使用的众多选项之一。您可以使用您选择的任何 SMTP 服务器进行发送,到目前为止,您拥有所需的凭据,例如用户名、密码、主机、端口、驱动程序和/或加密。

PHPMailer 提供了您需要的功能。因此,只需将 PHPMailer 或任何类似软件包与您的应用程序集成,并提供您的 SMTP 服务器提供的所需凭据。

【讨论】:

以上是关于如何使用 PHP 在 heroku 上发送电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 密码重置电子邮件未在 Heroku 上使用 gmail 发送

Ruby on Rails 中的 Sendgrid / 电子邮件发送问题(托管在 Heroku 上)

邮件未在 rails 上使用 devise、heroku 和邮戳发送

带有 Gmail 服务的 Nodemailer 无法在 heroku 上运行

如何将数据从 Heroku 上托管的 Node.js 应用程序发送到托管在完全独立(Cpanel)服务器上的 PHP 文件?

在 Heroku 上使用 Postmark API 的 Rails 电子邮件——由对等方重置连接