【问题标题】:What is causing a Fatal Error when sending email from a contact form via PHP and Swiftmailer?通过 PHP 和 Swiftmailer 从联系表单发送电子邮件时导致致命错误的原因是什么?
【发布时间】:2015-10-18 18:09:51
【问题描述】:

我是 Swiftmailer 的新手,正在尝试通过本地主机 (XAMPP) 上的 HTML 联系表单发送邮件。按下“提交”按钮后,错误显示为:

致命错误:在中找不到类“Swift_SmtpTransport” /Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php 上线 23

第 23、24 和 25 行是这个部分:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
    ->setUsername('foobar@googlemail.com')
    ->setPassword('GENERATED PASSWORD');

我期待邮件此时发送,并向我显示“已发送邮件”消息,以及 PHP 文件中的回显行,但是当我看到 "Mail Sent" 确认时,我也收到了错误,我没有收到电子邮件。

我查阅了文档here,我在'sendmessage.php' 中的代码似乎是正确的:

<?php
error_reporting(E_ALL);

echo getcwd();
echo function_exists('proc_open') ? "Yep, that will work" : "Sorry, that won't work";

require_once(dirname(__FILE__)."/swift.php");
echo 'Mail sent <br />';

// Grab the post data
$name = filter_var($_POST['Name'], FILTER_SANITIZE_STRING);
$usermail = filter_var($_POST['Email'], FILTER_SANITIZE_EMAIL);
$content = filter_var($_POST['Message'], FILTER_SANITIZE_STRING);

// Construct the body of the email
$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;

// Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
    ->setUsername('foobar@googlemail.com')
    ->setPassword('GENERATED PASSWORD');
echo 'line 26 <br />';

// Create the mailer using the created transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('This is the subject')
    ->setFrom(array('foobar@googlemail.com' => 'Feedback Received From User'))
    ->setTo(array('barfoo@gmail.com', 'foobar@googlemail.com' => 'Lead Recipients'))
    ->setSubject('Here is your Feedback subject')
    ->setBody($data, 'text/html');
echo 'line 34 <br />';

// Send the message
$result = $mailer -> send($message);
echo $result;
echo 'line 39<br />';
?>

proc_open 返回"Yep, that will work",但如上所述,我的收件箱中没有收到电子邮件。

我已尝试将相关线路上的端口更改为 25 和 486,但没有成功。

在 Google 搜索后,我发现 this question 似乎有同样的问题,但接受的答案并没有解决我的问题。

请注意,我收到了一个关于联系表单错误的previous question,这个问题紧随其后。

【问题讨论】:

    标签: php smtp swiftmailer


    【解决方案1】:

    SwiftMailer 似乎没有正确或完全加载。我不能保证它会有所帮助,但请尝试以下方法:

    在您的源代码(html 或 htdocs)目录中执行 GIT 克隆(请参阅 https://github.com/swiftmailer/swiftmailer/blob/5.x/doc/installing.rst)。

    这应该会为您提供一个名为“swiftmailer”的目录,并且您从中加载 swiftmailer 的脚本应该位于同一目录中。然后将您的“require_once”行更改为以下内容:

    require_once("swiftmailer/lib/swift_required.php");
    

    我只是粗略浏览了一下 swiftmailer 的源代码,但如果我没记错的话,它应该会自动为您完成其余的工作,包括加载 Swift_SmtpTransport 类。

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 2011-12-09
      • 2019-01-30
      • 2011-06-18
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 2022-01-25
      • 2019-09-04
      相关资源
      最近更新 更多