PHP - 使用 SwiftMailer 通过 gmail SMTP 发送邮件时出现问题

标签 php email smtp swiftmailer

我正在处理一个表单:提交表单后,我需要使用 gmail 帐户发送一封邮件。我已经设置了 SwiftMailer,似乎一切都设置正确,但没有任何反应。 我已将表单的“操作”设置为此 php 文件:

    <?php
    require_once '/assets/swift-mailer/lib/swift_required.php';

    // variables
    $visitor_email = $_POST['email'];
    $visitor_phone = $_POST['phone'];
    $visitor_page = $_POST['site'];


    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
      ->setUsername('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19c90989db1969c90989ddf929e9c" rel="noreferrer noopener nofollow">[email protected]</a>')
      ->setPassword('mypassword');

    $mailer = Swift_Mailer::newInstance($transport);

    $message = Swift_Message::newInstance('New submission')
      ->setFrom('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e4e8e0e5c9eee4e8e0e5a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>')
      ->setTo('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d4054425945485f404c44416d4942404c4443034e4240" rel="noreferrer noopener nofollow">[email protected]</a>')
      ->setBody("New submission from this site: \n
               $visitor_page \n
               e-mail address: $visitor_email \n
               phone number: $visitor_phone");

    $result = $mailer->send($message);
    ?>

我已经仔细检查了 PHP 的版本,在服务器中没有问题。 php_openssl 已启用。 服务器运行正常。 html 文件中的表单没问题,它关联了“method = post”。这些库已正确放置在服务器文件夹中。我真的不明白这是怎么回事。

最佳答案

也许有很多原因没有发送邮件...您想检查apache error_log和浏览器网络吗..或者您可以在脚本中设置error_reporting(E_ALL)。也许是google smtp问题或其他问题。首先你必须澄清你的问题,然后你就可以做任何事情。如果你上传你的错误,那么任何人都可以帮助你。

你可以尝试这个,但我不确定它对你是否有用

   $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls');

而不是

    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl");

关于PHP - 使用 SwiftMailer 通过 gmail SMTP 发送邮件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27051714/

相关文章:

php - wordpress wp_list_pages 帮助

Java 从 jar 文件加载图像

html - MAIL 模板的内联 CSS

zend-framework - 如何使用带有 tls 和自签名证书的 Zend_Mail_Transport_Smtp?

php - 在 php 中获取最近的日期

php - 从旧的 mysql 文件夹中获取旧的 MySQL 数据并导入到新数据库中

php - 在 laravel 5.1 中制作访客跟踪器

email - 通过 EMAIL 在 Jenkins 中触发构建

email - 在回复/转发中总是返回哪些 header ?

node.js - 错误命令太多 : outbound emails with Haraka