2013-02-19 85 views
1

我在使用swiftmailer时遇到了一些麻烦。它不断崩溃,但如果我拿出 - > sentTo它不会出错,但我也不能收到电子邮件。Swiftmailer错误输出

我很困惑,我做错了什么。

require_once 'lib/swift_required.php'; 

// Using smtp 
//$transport = Swift_SmtpTransport::newInstance('my_smtp_host.com', 25) 

//Using Gmail 
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") 
    ->setUsername('***') // or your gmail username 
    ->setPassword('***'); // or your gmail password 

// Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

// Create a message 
$message = Swift_Message::newInstance($subject) 
    ->setFrom(array($email => $name)) 
    ->setTo(array('[email protected]' => 'John Doe')) 
    ->setBody($content, 'text/html'); 

$message->attach(
Swift_Attachment::fromPath($_FILES['userfile']['name'])->setFilename($pic['tmp_name']) 
); 

// Send the message 
$result = $mailer->send($message); 

?> 

致命错误:

Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "...." using 2 possible authenticators' in ../lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 

Stack trace: 

0 ../lib/classes/Swift/Transport/Esmtp/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) 

1 ../lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() 

2 ../lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() 

3 upload.php(73): Swift_Mailer->send(Object(Swift_Message)) 

4 {main} thrown in lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184 
+0

“错误”是什么意思? – 2013-02-19 17:21:52

+0

对不起,我的俚语。 PHP有一个致命错误,并没有成功执行它应该完成的任务。 – Giovatto 2013-02-19 17:32:46

+0

我认为我的问题可能与附件有关。也许我必须在将它移动到服务器后拉它 – Giovatto 2013-02-19 17:36:10

回答

0

不得不改变邮件附件这样:

$message->attach(
    Swift_Attachment::fromPath($theDirectory)->setFilename($fileNameForEmail) 
    ); 

    // Send the message 
    $result = $mailer->send($message); 
} 
else{ 
     echo 'The file you have chosen is invalid. Please go back and try again.'; 
} 
+1

我同意你的'fromPath()'调用是无效的,但它不是你提供的错误信息的原因。 – 2013-02-20 08:21:06

0

此:

Failed to authenticate on SMTP server with username "...." using 2 possible authenticators

...含义正是它说。 Gmail不接受您的用户名和密码,或者是因为他们错了或者因为您未能提供某些必需的参数。

根据other questions at Stack Overflow和我自己的测试,主机名,端口和加密是正确的。所以可能的问题包括:

  1. 您提供了错误的用户名。
  2. 您提供了错误的密码。
  3. 如果您启用two-way authentication,则需要生成特定于应用程序的密码。 This video解释如何。

一旦你解决这个问题,你可能会出现以下情况例外:

Uncaught exception 'Swift_IoException' with message 'Unable to open file for reading []'

...这段代码触发:

,如果您正在阅读的原因来自$_FILES array的错误项目:fromPath()需要读取文件系统路径,但$_FILES['userfile']['name']包含客户端计算机上文件的原始名称。

+1

我的问题是我在下面发布的答案。与Google无关。它说我必须等2天才能标记我的答案正确。 – Giovatto 2013-02-19 18:05:59

+1

事实上,当我尝试切换到由于外部应用程序劫持而导致该问题时,我从我的其他帐户收到了来自Google的警告电子邮件。 – Giovatto 2013-02-19 18:06:47

+1

@Giovatto - 我实际上在我的电脑中安装了Swift Mailer,并在我的Gmail帐户中运行了您的代码。这*是*您对所发布的错误细节的解释。不过,你的代码还有一个错误,而且我刚刚编辑了我的答案来解释第二个错误 - 但在修复第一个错误消息之前,您不会得到第二个错误消息。 如果您对我很生气,想假装错误信息无关紧要,请继续并接受您的回答,但这无助于使Stack Overflow成为参考问答网站。 – 2013-02-20 08:29:45