Reputation: 709
Trying to figure out how to use SwiftMailer inside Restler. I think I may just be including it incorrectly. Per the SwiftMailer documentation, all I need to do is include a single file via require_once and their autoloader does all the magic, but I keep getting Class not found errors. Supposedly Restler plays nice with other autoloaders.
I've tried the following code in a variety of different places in my restler files (placed the require_once in the index.php as well as inside the class with the rest of the code).
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
}
}
Error:
Fatal error: Class 'Swift_MailTransport' not found in /home/[snip]/public_html/[snip].php on line 63
Upvotes: 0
Views: 1431
Reputation: 81
//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("[email protected]");
$this->message->setTo(array("[email protected]" => "test"));
$this->message->setBody('Here is the message itself');
$mailer->send($this->message, $failedRecipients);
echo "message sent";
// Show failed recipients
print_r($failedRecipients);
}
}
Upvotes: 0
Reputation: 101
I recently had a similar desire, to include Swift Mailer inside another class. The solution was to include the swift_required.php outside the wrapper class, then create a class extended by Swift_Mailer. In it you can reference the Swift_Message class:
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);
}
}
Upvotes: 2
Reputation: 993
Starting from Restler 3 RC4 (currently available in v3 branch) you can use composer's autoloader.
Use the following composer.json to and install both Restler and SwifftMailer
{
"require" : {
"luracast/restler" : "3.x-dev",
"swiftmailer/swiftmailer" : "4.1.7"
}
}
Read about composer from http://getcomposer.org/
Upvotes: 0