【问题标题】:How do I send email on heroku using PHP?如何使用 PHP 在 heroku 上发送电子邮件?
【发布时间】: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 实现联系表单吗?

【问题讨论】:

    标签: php heroku


    【解决方案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 服务器提供的所需凭据。

      【讨论】:

        猜你喜欢
        • 2014-05-05
        • 2019-08-29
        • 2015-11-22
        • 2021-03-17
        • 1970-01-01
        • 2018-05-08
        • 2012-07-30
        • 1970-01-01
        相关资源
        最近更新 更多