[英]Using SwiftMailer inside Restler class
試圖弄清楚如何在Restler中使用SwiftMailer。 我想我可能只是錯誤地包括它。 根據SwiftMailer文檔 ,我需要做的就是通過require_once包含一個文件,並且他們的自動加載器可以完成所有的魔法,但是我一直在發現Class not found錯誤。 據說 Restler和其他自動加載器一起玩得很好。
我已經在我的restler文件中的各種不同的地方嘗試了以下代碼(將require_once放在index.php中以及在類中包含其余代碼)。
class Remind {
function post($request_data=NULL) {
[ snip ]
require_once('../../Swift/lib/swift_required.php');
$transport = Swift_MailTransport::newInstance(); // Create the transport; use php mail
$mailer = Swift_Mailer::newInstance($transport); // Create the Mailer using your created Transport
$message = Swift_Message::newInstance() // Create the message
->setPriority($priority) // Give the message a priority, 1 through 5, 1 being the highest.
->setSubject($subject) // Give the message a subject
->setFrom(array($from_email => $from_name)) // Set the From address with an associative array
->setTo(array($to_email => $to_name)) // Set the To addresses with an associative array
->setReadReceiptTo(SYS_EMAIL) // Send read receipts to Support
->setBody('Here is the message itself') // Give it a body
->addPart('<q>Here is the message itself</q>', 'text/html') // And optionally an alternative body
;
$result = $mailer->send($message); // Send the message
}
}
錯誤:
Fatal error: Class 'Swift_MailTransport' not found in /home/[snip]/public_html/[snip].php on line 63
我最近有類似的願望,將Swift Mailer包含在另一個類中。 解決方案是在包裝類之外包含swift_required.php,然后創建一個由Swift_Mailer擴展的類。 在其中,您可以引用Swift_Message類:
require 'path/to/swift_mailer/library/lib/swift_required.php';
class Remind extends Swift_Mailer {
function post($request_data=NULL) {
$transport = Swift_SmtpTransport::newInstance()
->setHost('host')
->setPort('port')
->setUsername('username')
->setPassword('password')
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setPriority($priority)
->setSubject($subject)
->setFrom(array($from_email => $from_name))
->setTo(array($to_email => $to_name))
->setReadReceiptTo(SYS_EMAIL)
->setBody('Here is the message itself')
->addPart('<q>Here is the message itself</q>', 'text/html')
;
$result = $mailer->send($message);
}
}
從Restler 3 RC4(目前在v3分支中可用)開始,您可以使用composer的自動加載器。
使用以下composer.json來安裝Restler和SwifftMailer
{
"require" : {
"luracast/restler" : "3.x-dev",
"swiftmailer/swiftmailer" : "4.1.7"
}
}
從http://getcomposer.org/了解作曲家
//the autoloader is a the swiftmailer downloaded with composer
require_once 'vendor/autoload.php';
class Remind {
private $transport;
private $message;
function post{
// Create the Transport
$this->transport = new Swift_SmtpTransport('host', port, 'ssl');
$this->transport->setUsername('username');
$this->transport->setPassword('password');
/*
You could alternatively use a different transport such as Sendmail:
// Sendmail
$transport = new Swift_SendmailTransport('/usr/sbin/sendmail -bs');
*/
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($this->transport);
// Create a message
$this->message = (new Swift_Message('Wonderful Subject'));
$this->message->setFrom("test@test.com");
$this->message->setTo(array("test@test.com" => "test"));
$this->message->setBody('Here is the message itself');
$mailer->send($this->message, $failedRecipients);
echo "message sent";
// Show failed recipients
print_r($failedRecipients);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.