繁体   English   中英

在Restler类中使用SwiftMailer

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM