【问题标题】:sending email with cakephp2用 cakephp2 发送电子邮件
【发布时间】:2012-05-04 15:27:23
【问题描述】:

我在使用 cakephp2 发送邮件时遇到问题,我知道我可以发送电子邮件,因为我配置了后缀,并且我可以使用命令行或 php 发送电子邮件。所以,请你给我发一个 cakephp2 发送电子邮件的例子。

这是错误信息

无效的电子邮件:“you@localhost” 错误:发生内部错误。

我也尝试过通过 gmail 使用 ssl,但它也不起作用,这让我很难过。

谢谢大家

顺便说一句,我正在尝试这个网址的确切示例http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

【问题讨论】:

  • CakePHP documentation中有一个使用Gmail的例子,是不是也失败了?
  • 您的电子邮件不包含域(例如 .com),请尝试添加域。
  • 它已经在工作了,我配置了我的 postfix 服务器并重定向到 gmail,cakephp2 的 gmail 示例对我不起作用。无论如何,谢谢你们=)

标签: email cakephp-2.0


【解决方案1】:

您的应用程序/配置/电子邮件。

class EmailConfig { public $gmail = array( 'port' => '465', 'timeout' => '300', 'host' => 'ssl://smtp.gmail.com', 'username' => '<your_email>@gmail.com', 'password' => '<you_password>', 'transport' => 'Smtp' ); }

你的文件 = app/controller/appController.php 插入这个函数

public function sendEmail($type, $options){
    try {
        $Email = new CakeEmail($type);
        $Email->config($options);
        $Email->template = "email_confirmation";
        $Email->emailFormat('html');
        //$this->idCrudRash = $options;
        $Email->send();
    } catch (SocketException $e) {
        die('Erro ao enviar email:'. $e->getMessage());
        $this->log(sprintf('Erro ao enviar email: %s', $e->getMessage()));

    }
}

对于用户:app/controller/contato.php

$options = array( 'emailFormat' => 'html', 'from' => array( $config['email_noanswer'] => $config['site_name'] ), 'subject' => 'Confirmação de Cadastro', 'to' => $this->request->data['User']['email'], //'template' => 'default', 'template' => 'email_confirmation', 'viewVars' => array( 'title_for_layout' => 'Confirmação de Email ' . $config['site_name'], 'name' => $this->request->data['User']['name'], 'email' => $this->request->data['User']['email'], //'cpf' => base64_encode($this->request->data['User']['cpf']), 'site_name' => $config['site_name'], ), ); $this->sendEmail('gmail', $options);

【讨论】:

    【解决方案2】:

    在您的 email.php 文件中,请删除默认的 'from' 值,它会覆盖您传递的参数。

    public $default = array(
        'transport' => 'Mail',
        'from' => 'you@localhost', // remove this line
        ...
    );
    

    【讨论】:

      【解决方案3】:
      In app/config/Email 
      
      public $smtp = array(
              'transport' => 'Smtp',
              'from' => array('no-reply@xyz.com' => 'no-reply@xyz.com'),
              'host' => 'ssl://smtp.abc.com',
              'port' => 465,
              'timeout' => 30,
              'username' => 'username',
              'password' => 'password',
              'client' => null,
              'log' => false,
          );
      
      In Your Controller
      
      App::uses('AppController', 'Controller');
      App::uses('CakeEmail', 'Network/Email');
      
      public function index()
          {
              $this->layout = 'layout';
              $this->set('title', "Title");
              if ($this->request->is('Post')) {
                  if (!empty($this->request->data)) {
                      if ($this->Modal->Save($this->request->data)) {
                          $to = 'test@anc.com';
                          $subject = 'Your_subject';
                          $message = $this->request->data;
                          if ($this->sendmail($to, $subject, $message)) {
      
                              echo"sent";die;
                          }
                      } else {
                          echo"wrong";die;
                      }
                  }
              }
          }
          public function sendmail($to = null, $subject = '', $messages = null, $ccParam = null, $from = null, $reply = null, $path = null, $file_name = null)
          {
              $this->layout = false;
              $this->render(false);
              $name = $messages['Modalname']['name'];
              $email = $messages['Modalname']['email'];
              $Email = new CakeEmail();
              $Email->config('smtp');
              $Email->viewVars(array('name' => $name, 'email' => $email));
              $Email->template('comman_email_template', 'comman_email_template');
              return $Email->emailFormat('html')
                  ->from(array('no-reply@xyz.com' => 'no-reply@xyz.com'))
                  ->to($to)
                  ->subject($subject)
                  ->send();
          }
      

      为电子邮件模板创建布局和视图。并添加已发送的数据值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-16
        • 1970-01-01
        • 2016-04-17
        • 2017-12-07
        • 1970-01-01
        • 1970-01-01
        • 2012-04-09
        • 1970-01-01
        相关资源
        最近更新 更多