【问题标题】:CakePHP-2.0: How can i send email from a gmail account using CakEmail and SMTP setting?CakePHP-2.0:如何使用 CakEmail 和 SMTP 设置从 gmail 帐户发送电子邮件?
【发布时间】:2011-12-16 10:48:50
【问题描述】:

我正在尝试使用 CakEmail 和 SMTP 设置从 gmail 帐户发送电子邮件。

如果有人一步一步告诉流程该怎么做,那就太好了。

我在 app/Config/email.php=>

中添加了以下内容
<?php
class EmailConfig {
    public $smtp = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => 'my@gmail.com',
        'password' => 'secret'
    );
}

现在我如何从“my@gmail.com”向任何电子邮件帐户发送电子邮件?

这是 CakePHP-2.0

【问题讨论】:

    标签: php email cakephp cakephp-2.0


    【解决方案1】:

    来自文档:

    您可以配置 SSL SMTP 服务器,例如 GMail。为此,请将“ssl://”放在主机的前缀中,并相应地配置端口值。示例:

    <?php
    class EmailConfig {
        public $gmail = array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => 465,
            'username' => 'my@gmail.com',
            'password' => 'secret'
        );
    }
    

    http://book.cakephp.org/2.0/en/core-utility-libraries/email.html?highlight=cakeemail#CakeEmail

    【讨论】:

      【解决方案2】:

      正确的配置是:

      public $gmail = array(
          'host' => 'ssl://smtp.gmail.com',
          'port' => 465,
          'username' => 'my@gmail.com',
          'password' => 'secret',
          'transport' => 'Smtp'
      );
      

      所以,不要忘记 transport 元素。

      【讨论】:

        【解决方案3】:

        只需设置from:

        <?php
        $email = new CakeEmail();
        $email->from(array('my@gmail.com' => 'Your Name'));
        $email->to('foo@stackoverflow.com');
        $email->subject('Sent from Gmail');
        $email->send('My message'); // or use a template etc
        

        应该这样做。

        您可能还想设置sender;我不是 100%,但我想在通过您自己的网站“从”gmail 发送电子邮件时它会很有用;也许是为了阻止电子邮件被当作垃圾邮件。

        $email-&gt;sender('noreply@mydomain.com', 'MyApp emailer');

        【讨论】:

          【解决方案4】:

          我目前正在使用 gmail 帐户发送出站邮件。我正在使用模板和可重复使用的电子邮件设置功能。这是我的工作代码的副本:

          // app/controllers/users_controller.php
          function sendemail($subject, $body, $to, $template) {
              $this->Email->smtpOptions = array(
                  'port'=>'465',
                  'timeout'=>'30',
                  'host' => 'ssl://smtp.gmail.com',
                  'username'=>'username@domain.com',
                  'password'=>'secret',
              );
              $this->Email->delivery = 'smtp';
              //$this->Email->delivery = 'debug';
              $this->Email->from    = 'Username <username@Domain.com>';
              $this->Email->to      = $to;
              $this->Email->subject = $subject;
              $this->set('body', $body);
              $this->set('smtp_errors', $this->Email->smtpError);
              $this->Email->send($content, $template);
          }
          
          // app/controllers/users_controller.php 
          // Excerpt from new user method in users controller:
          function add() {
              // ...other stuff
              $body['user'] = $user['User']['username'];
              $this->sendemail('Domain.com New User Signup!', $body, 'destination@Domain.com', 'newuser');
              // ...other stuff
          }
          
          // app/views/elements/email/text/newuser.ctp
          Everyone,
          Another new user just signed up for Domain. Stats below:
          User: <?php echo $body['user'] . "\r\r"; ?>
          Just thought you'd like to know :)
          -Janet
          

          【讨论】:

          • 这是较旧的电子邮件组件。
          【解决方案5】:

          使用 Swiftmailer 组件;这是最容易使用的组件。

          http://bakery.cakephp.org/articles/mhuggins/2008/06/11/improved-swiftmailer-component

          在使用 CakePHP 2.0 以后的版本时,您需要做一些更改。 CakePHP 2.0 提供了一个“电子邮件”视图目录,用于存储所有电子邮件模板。

          对组件的更改:

          1. 将所有var 声明更改为public
          2. public $layout = 'Emails'; 更改为public $viewPath = '/Emails';

          3. 更改_getBodyText中的渲染路径:

          $body = $this-&gt;controller-&gt;render($this-&gt;viewPath . DS . 'text' . DS . $view, $this-&gt;layout . DS . 'text'.DS.'default');

          1. 更改_getBodyHtml中的渲染路径:

          $body = $this-&gt;controller-&gt;render($this-&gt;viewPath . DS . 'html' . DS . $view, $this-&gt;layout . DS . 'html'.DS.'default');

          1. 注释掉这些行:

          $bodyText = $this->_getBodyText($view); $message->setBody($bodyText, "text/plain");

          如果您将 HTML 和 TEXT 都设置为活动状态,则 Swiftmailer 组件会发送一封空白电子邮件。它从电子邮件视图中读取并添加正文。如果您想发送 html 电子邮件,这就是注释这两行的原因。

          第二个原因是如果两者都被激活并且您在 email.html.ctpemail.text.ctp 文件中都有内容,它会产生一个标题问题,因为只有文本格式会显示在电子邮件中(实际上这两种格式都存在于标题,但它会抑制 html 部分并显示文本部分)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-08
            • 1970-01-01
            • 1970-01-01
            • 2012-03-30
            • 2013-03-24
            • 2022-12-15
            • 2017-10-22
            • 2015-10-22
            相关资源
            最近更新 更多