无法让 Swiftmailer 在 PHP 站点上工作
Cannot get Swiftmailer to work on a PHP site
我是 PHP 和 Swiftmailer 的新手。
我想要做的是在网络服务器上设置一个 PHP 站点并使用 SwiftMailer 发送电子邮件。
我得到的代码在我的本地 XAMPP 服务器上确实有效,但会产生错误消息:
"Fatal error: Class 'Swift_Attachment' not found in /[address to my php file]"
从联机网络服务器执行时。
这是我的代码:
<?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;
?>
请注意,当我注释掉所有与附件相关的内容时,错误消息切换为
"Fatal error: Class 'Swift_SmtpTransport' not found in /[address to my php file]" 并指向“$transport”行。
所以看起来整个 SwiftMailer 都没有工作,这与附件无关。
不胜感激!
我没看到 ->setTo( your_mail_dest) 和 ->send()
试试像这样简单的发送
$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();
案件结案。
事实证明,代码毕竟工作正常。我需要做的就是升级 Swiftmailer(现在 运行 在 5.4.2-DEV 上)。抱歉给您带来麻烦,感谢 scaisEdge 的帮助!
我是 PHP 和 Swiftmailer 的新手。 我想要做的是在网络服务器上设置一个 PHP 站点并使用 SwiftMailer 发送电子邮件。
我得到的代码在我的本地 XAMPP 服务器上确实有效,但会产生错误消息:
"Fatal error: Class 'Swift_Attachment' not found in /[address to my php file]"
从联机网络服务器执行时。
这是我的代码:
<?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;
?>
请注意,当我注释掉所有与附件相关的内容时,错误消息切换为
"Fatal error: Class 'Swift_SmtpTransport' not found in /[address to my php file]" 并指向“$transport”行。
所以看起来整个 SwiftMailer 都没有工作,这与附件无关。
不胜感激!
我没看到 ->setTo( your_mail_dest) 和 ->send()
试试像这样简单的发送
$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();
案件结案。
事实证明,代码毕竟工作正常。我需要做的就是升级 Swiftmailer(现在 运行 在 5.4.2-DEV 上)。抱歉给您带来麻烦,感谢 scaisEdge 的帮助!