Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 创建Swift\u邮件类实例_Php_Oop_Swiftmailer - Fatal编程技术网

Php 创建Swift\u邮件类实例

Php 创建Swift\u邮件类实例,php,oop,swiftmailer,Php,Oop,Swiftmailer,我做了以下测试,效果很好 //Create the Transport $mailer = Swift_Mailer::newInstance($transport); // create the message $message = Swift_Message::newInstance('Hello World') ->setFrom(array(MAIL_FROM => MAIL_FROM_NAME)) ->setTo(array('MyEmail@email.co

我做了以下测试,效果很好

//Create the Transport
$mailer = Swift_Mailer::newInstance($transport);
// create the message
$message = Swift_Message::newInstance('Hello World')
  ->setFrom(array(MAIL_FROM => MAIL_FROM_NAME))
  ->setTo(array('MyEmail@email.com')
  ->setBody('Here is the message itself');
//Send the message
$result = $mailer->send($message);
现在我想将其包含在类实例中(例如,对象中的函数使用swift发送电子邮件)。我知道我必须重构,但我没有抓住要点。虽然这是错误的,但我想我是想说一些类似的话

$mailer=$this->Swift\u mailer::newInstance($transport)

但我不知道如何重构。你能帮忙吗? 谢谢
贾尔斯

我看不出这个案例与你的第一个例子有什么不同。首先在代码中包含swift邮件程序类,然后实例化swift对象。很简单。我不清楚这是怎么做到的-你能举例说明吗?检查下面的答案。这是你的例子。
inlcude 'swiftmailer.class.php;

class YourClass {
    function __construct() {}

    function yourFunction() {

        $mailer = Swift_Mailer::newInstance($transport);
        // create the message
        $message = Swift_Message::newInstance('Hello World')
        ->setFrom(array(MAIL_FROM => MAIL_FROM_NAME))
        ->setTo(array('MyEmail@email.com')
        ->setBody('Here is the message itself');
        //Send the message
        $result = $mailer->send($message);
    }

}