我是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不工作,這不是與附件相關的。
幫助將不勝感激!
我投票結束這個問題作爲題外話,因爲它屬於serverfault.com –