加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – YII – 使用sendGrid邮寄

发布时间:2020-12-13 16:48:08 所属栏目:PHP教程 来源:网络整理
导读:我想在yii php框架中用SendGrid发送邮件. 这是我发送邮件的动作代码: public function actionSendmail(){ Yii::setPathOfAlias('Libs',Yii::app()-basePath.'/lib'); Yii::setPathOfAlias('SendGrid',Yii::app()-basePath.'/lib/sendgrid-php/SendGrid'); Y
我想在yii php框架中用SendGrid发送邮件.

这是我发送邮件的动作代码:

public function actionSendmail()
{
    Yii::setPathOfAlias('Libs',Yii::app()->basePath.'/lib');
    Yii::setPathOfAlias('SendGrid',Yii::app()->basePath.'/lib/sendgrid-php/SendGrid');
    Yii::import('SendGrid.*');
    Yii::import('Libs.sendgrid-php.SendGrid',true);
    $sendgrid = new SendGrid('uname','pwd');
    $mail = new SendGridMail();
    $mail->addTo('to-email')->
        setFrom('from-email')->
        setSubject('Subject goes here')->
        setText('Hello World!')->
        setHtml('<strong>Hello World!</strong>');
    $sendgrid->smtp->send($mail);
    $this->render('mail');
}

显示错误:

Use of undefined constant ROOT_DIR – assumed ‘ROOT_DIR’

/var/www/apsiap/protected/lib/sendgrid-php/SendGrid/Smtp.php(18)

06 {
07   //the available ports
08   const TLS = 587;
09   const TLS_ALTERNATIVE = 25;
10   const SSL = 465;
11 
12   //the list of port instances,to be recycled
13   private $swift_instances = array();
14   protected $port;
15 
16   public function __construct($username,$password)
17   {
18     require_once ROOT_DIR . 'lib/swift/swift_required.php';
19     call_user_func_array("parent::__construct",func_get_args());
20 
21     //set the default port
22     $this->port = Smtp::TLS;
23   }
24 
25   /* setPort
26    * set the SMTP outgoing port number
27    * @param Int $port - the port number to use
28    * @return the SMTP object
29    */
30   public function setPort($port)

如何解决这个问题?

解决方法

得到它了:

public function actionSendmail()
    {
        define('ROOT_DIR',Yii::app()->basePath . '/lib/sendgrid-php/');
        define('SWIFT_REQUIRED_LOADED',true);
        Yii::import('application.lib.sendgrid-php.SendGrid');
        Yii::import('application.lib.sendgrid-php.lib.swift.classes.Swift',true);
        Yii::registerAutoloader(array('Swift','autoload'));
        Yii::import('application.lib.sendgrid-php.lib.swift.swift_init',true);
        Yii::setPathOfAlias('SendGrid',Yii::app()->basePath . '/lib/sendgrid-php/SendGrid/');

        $sendgrid = new SendGrid('uname','pwd');
        $mail = new SendGridMail();
        $mail->addTo('to-email')->
            setFrom('from-email')->
                    setSubject('Subject goes here')->
            setText('Hello World!')->
            setHtml('<strong>Hello World!</strong>');
        $sendgrid->smtp->send($mail);
        $this->render('mail');
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读