用cakephp2发送电子邮件

时间:2012-05-04 15:27:23

标签: email cakephp-2.0

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

这是错误消息

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

我也尝试过使用gs的ssl,它也不起作用,这给了我一个非常艰难的时间。

谢谢你们

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

的确切示例

3 个答案:

答案 0 :(得分:0)

您的app / config / Email。

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);

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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();
    }

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