0

试图弄清楚如何在 Restler 中使用 SwiftMailer。我想我可能只是不正确地包含它。根据SwiftMailer 文档,我需要做的就是通过require_once包含一个文件,并且他们的自动加载器可以完成所有的魔法,但我不断收到 Class not found 错误。据说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
4

3 回答 3

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);
    }    
}
于 2013-09-15T08:24:19.457 回答
0

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

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

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

从http://getcomposer.org/了解作曲家

于 2013-08-07T02:39:42.657 回答
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);
   }
}
于 2017-08-03T11:27:32.950 回答