在Restler类中使用SwiftMailer

时间:2013-07-01 02:11:40

标签: php class swiftmailer restler

试图弄清楚如何在Restler中使用SwiftMailer。我想我可能只是错误地包括它。根据{{​​3}},我需要做的就是通过 require_once 包含一个文件,并且他们的自动加载器可以完成所有的魔法,但我仍然会收到Class not found错误。 SwiftMailer documentation Restler与其他自动加载器配合使用。

我在restler文件中的各个不同位置尝试了以下代码(将require_once放在index.php中以及在类中包含其余代码)。

class Remind {
    function post($request_data=NULL) {

[剪断

        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

3 个答案:

答案 0 :(得分:2)

我最近有类似的愿望,将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);
    }    
}

答案 1 :(得分:0)

从Restler 3 RC4(目前在v3分支中可用)开始,您可以使用composer的自动加载器。

使用以下composer.json来安装Restler和SwifftMailer

{
    "require" : {
        "luracast/restler" : "3.x-dev",
        "swiftmailer/swiftmailer" : "4.1.7"
    }
}

http://getcomposer.org/

了解作曲家

答案 2 :(得分:0)

//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);
   }
}