无法让Swiftmailer在PHP网站上工作
问题描述:
我是PHP和Swiftmailer的新手。 我想要做的是在网络服务器上设置一个PHP站点,并使用SwiftMailer发送电子邮件。无法让Swiftmailer在PHP网站上工作
我得到的代码做我的本地XAMPP服务器上的工作,但会产生错误信息:
“致命错误:类‘Swift_Attachment’中/未找到[地址到我的PHP文件]”
当从在线网络服务器执行时,可以使用。
这里是我的代码:
<?php
// Get this directory, to include other files from
$dir = dirname(__FILE__);
// Get the contents of the pdf into a variable for later
ob_start();
require_once($dir.'/pdf_selbst_lir.php');
$pdf_html = ob_get_contents();
ob_end_clean();
// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
// Get the content of the HTML email into a variable for later
ob_start();
require_once($dir.'/Templates/html.php');
$html_message = ob_get_contents();
ob_end_clean();
// Swiftmailer
require_once($dir.'/swiftmailer-5.0.1/lib/swift_required.php');
// Create the attachment with your data
$attachment = Swift_Attachment::newInstance($pdf_content, 'pdfname.pdf', 'application/pdf');
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mymailserver', 587, 'tls')
->setUsername('username')
->setPassword('password')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
->setFrom(array('senderaddress' => 'Sender Name'))
->setSubject('subject')
->addBcc('somebccaddress')
->setBody($html_message, 'text/html')
->attach($attachment);
try {
$message->addTo('somerecipient');
}
catch (Swift_RfcComplianceException $e)
{
// Address was invalid
echo "Email address not correct.";
}
// Send the message
if ($mailer->send($message))
$success = true;
else
$error = true;
?>
需要注意的是,当我注释掉所有附件相关的东西,错误信息切换到
“致命错误:类‘Swift_SmtpTransport’没有找到/ [address to my php file]“并指向”$ transport“行。
所以它似乎整体SwiftMailer不工作,这不是与附件相关的。
帮助将不胜感激!
答
结案。
原来,代码工作正常。我所需要做的就是升级Swiftmailer(现在运行在5.4.2-DEV上)。对不起,感谢scaisEdge的帮助!
答
我don'see - > setTo(your_mail_dest)和 - >发送()
尝试用一个简单的发送这样
$message = Swift_Message::newInstance()
->setFrom(array('senderaddress' => 'Sender Name'))
->setTo(**** your_mail_dest ****)
->setSubject('subject')
->addBcc('somebccaddress')
->setBody($html_message, 'text/html')
->attach($attachment)
->send();
+0
好吧,刚刚尝试过 - 同样的故事。 – fraggykrueger
我投票结束这个问题作为题外话,因为它属于serverfault.com –