2013-07-01 55 views
0

試圖弄清楚如何在Restler中使用SwiftMailer。我想我可能會錯誤地包含它。根據SwiftMailer documentation,我所需要做的只是通過require_once包含一個文件,它們的自動加載器完成了所有的魔術,但是我一直在收到類未找到的錯誤。 Supposedly Restler與其他自動加載器相得益彰。在Restler類中使用SwiftMailer

我已經在我的restler文件中的各種不同的地方嘗試了以下代碼(將index_php放置在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 

回答

0

從Restler 3 RC4(目前在V3分公司提供),您可以使用作曲家的自動加載啓動。

使用以下composer.json並安裝Restler和SwifftMailer

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

閱讀的作曲家http://getcomposer.org/

+0

如果你是一個共享的主機提供商運行呢? –

+1

您可以在開發計算機上運行作曲家並上傳所有生成的文件。大多數時候這就是我們正在做的事情 – Luracast

2

我最近也有類似的慾望,包括其他類中斯威夫特梅勒。解決方案是在包裝類外部包含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); 
    }  
} 
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("[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); 
    } 
}