【问题标题】:YII - mailing with sendGridYII - 使用 sendGrid 发送邮件
【发布时间】:2012-11-03 06:05:03
【问题描述】:

我正在尝试在 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 SendGrid\Mail();
    $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');
}

显示错误:

使用未定义的常量 ROOT_DIR - 假定为“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)

如何解决这个问题?

【问题讨论】:

    标签: php email yii sendgrid


    【解决方案1】:

    知道了:

        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 SendGrid\Mail();
            $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');
        }
    

    【讨论】:

      【解决方案2】:

      您是否有机会查看以下链接?主要问题似乎是您导入 SendGrid 库的方式。

      【讨论】:

      【解决方案3】:

      你不应该这样使用SendGrid,你必须配置一个类或下载一个已有的扩展并设置为一个邮件组件,然后你才能轻松发送电子邮件。

      不要重新发明轮子:)

      【讨论】:

      • 在建议解决问题的替代方法时,请使用链接和/或代码丰富您的答案。另外,做一些研究,看看你的建议有多可行。
      猜你喜欢
      • 2016-09-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多