1

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

这是错误信息

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

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

多谢你们

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

4

3 回答 3

0

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

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

于 2015-04-01T16:56:46.467 回答
0

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

public $default = array(
    'transport' => 'Mail',
    'from' => 'you@localhost', // remove this line
    ...
);
于 2015-05-07T03:01:27.233 回答
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();
    }

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

于 2020-02-14T07:29:00.330 回答