如何使用CakePHP电子邮件组件? [英] How do I use the CakePHP Email Component?

查看:189
本文介绍了如何使用CakePHP电子邮件组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我最近的努力是使用电子邮件组件。



我有合同。当我创建合同时,我添加一个用户。当我第一次保存新合同...时,我想向合同中的用户发送一封电子邮件,允许他们点击链接返回合同,然后接受或拒绝合同。



如何发送此电子邮件?



我可以得到更多的细节,回答更好。我读过的一切都令人惊讶的混乱。我需要配置smtp设置吗?如何在合同中保存并将其传递给合同中的用户,并将合同的链接发送到电子邮件?我如何知道电子邮件是否已经发送,每次都不去查看我的电子邮件?



这是我在我的电子邮件contract_controller.php中的代码发送函数:(A合同属于用户,用户有多个合同。)

  function _sendContractEmail($ id){
$ this-> Email-> smtpOptions = array(
'port'=>'465',
'timeout'=> '30',
'host' =>'ssl://smtp.gmail.com',
'username'=>'username',
'password'=>'password'
);
$ this-> Email-> delivery ='smtp';
$ User = $ this-> Contract-> User-> read(null,$ id);
$ this-> Email-> to ='jeremiah@jeremiahotis.com';
$ this-> Email-> subject ='';
$ this-> Email-> replyTo ='no-reply@goodvaluation.com';
$ this-> Email-> from ='Jeremiah Oits< jeremiah@jeremiaotis.com>';
$ this-> Email-> template ='simple_message';
$ this-> Email-> sendAs ='html';
$ this-> set('User',$ User);
$ this-> Email-> _debug = true;
$ this-> Email-> send('Test Email');
$ this-> redirect(array('controller'=>'contracts','action'=>'index'));
}



这里是我在我的contract_controller.php add

  function add(){
if(!empty($ this-> data)){
$ this-> Contract-> create();
if($ this-> Contract-> save($ this-> data)){
$ this-> Session-> setFlash(_ ,true));
$ this-> _sendContractEmail($ this-> Contract-> User-> id);
} else {
$ this-> Session-> setFlash(__('无法保存合同,请重试。
}

这是在我的contracts_controller.php文件的顶部$ name和$ helpers:

  var $ components = array('Email'); 

我想我应该指出,我在使用模板时遇到问题,包括电子邮件的正文直接在send(),我指定了电子邮件地址,而不是使用一个变量。依然没有!没有错误,没有电子邮件。



任何帮助将非常感谢!我真的不知道我做错了什么!



谢谢,



Jeremiah

解决方案

http://book.cakephp.org/view/481/Sending-A-Message-Using-SMTP



使用代码示例从页面,特别是最后一部分获取错误通知

  / *检查SMTP错误。 * / 
$ this-> set('smtp-errors',$ this-> Email-> smtpError);

您可以使用 pr(smtp-errors)打印smtp错误



电子邮件的布局应位于

下。

  app / view / layout / email / html 
/ app / view / layout / email / text

如果您要复制和修改默认模板,可以在

下找到它们。

  / libs / view / layouts / email / html 
/ libs / view / layouts / email / text

问题可能是SMTP服务器拒绝了电子邮件。



哦,你还应该检查debug.log和error.log在 / app / tmp /日志


I am new to CakePHP and trying desperately to learn!

My most recent struggle is with the Email Component.

I have a contract. When I create the contract, I add a user. When I save the new contract...for the first time, I want to send an email to the user in that contract that allows them to click on a link back to the contract, and then accept or reject the contract.

How do I send this email?

The more details I can get, answer-wise, the better. Everything I have read out there is surprisingly confusing. Do I need to configure smtp settings? How do I grab the user in the contract after it has been saved and pass it and the link to the contract on to the email? How do I know if the email has been sent, without going and checking my email every single time?

Here is the code I have in my contracts_controller.php for the email sending function: (A Contract belongsTo a User, and a User hasMany Contracts.)

function _sendContractEmail($id) {
        $this->Email->smtpOptions = array(
        'port'=>'465',
        'timeout'=>'30',
        'host'=>'ssl://smtp.gmail.com',
        'username'=>'username',
        'password'=>'password'
        );
        $this->Email->delivery = 'smtp';
        $User = $this->Contract->User->read(null,$id);
        $this->Email->to = 'jeremiah@jeremiahotis.com';
        $this->Email->subject = '';
        $this->Email->replyTo = 'no-reply@goodvaluation.com';
        $this->Email->from = 'Jeremiah Oits <jeremiah@jeremiaotis.com>';
        $this->Email->template = 'simple_message';
        $this->Email->sendAs = 'html';
        $this->set('User', $User);
        $this->Email->_debug = true;
        $this->Email->send('Test Email');
        $this->redirect(array('controller'=>'contracts', 'action'=>'index'));
    }

Here is the code I have in my contracts_controller.php add() function:

function add() {
        if (!empty($this->data)) {
            $this->Contract->create();
            if ($this->Contract->save($this->data)) {
                $this->Session->setFlash(__('The Contract has been saved', true));
                $this->_sendContractEmail($this->Contract->User->id);
            } else {
                $this->Session->setFlash(__('The Contract could not be saved. Please, try again.', true));
            }

And, this is at the top of my contracts_controller.php file after the $name and $helpers:

var $components = array('Email');

I guess I should point out that I was having trouble with the template, so to test it, I included the body of the email directly in the send(), and I specified the to email address rather than using a variable. Still...nothing! No error, no email.

Any help would be greatly appreciated! I really have no idea what I am doing wrong!

Thanks,

Jeremiah

解决方案

http://book.cakephp.org/view/481/Sending-A-Message-Using-SMTP

Use the code sample from the page, specifically the last section to get error notices

/* Check for SMTP errors. */
    $this->set('smtp-errors', $this->Email->smtpError);

You can print the smtp errors using pr(smtp-errors) your view.

The layouts for emails should be located under

/app/view/layout/email/html
/app/view/layout/email/text

If you want copy and modify default templates they can be found under

/libs/view/layouts/email/html
/libs/view/layouts/email/text

Your code looks correct. The problem is probably with the SMTP server rejecting the email.

Oh and you should also check you debug.log and error.log under /app/tmp/logs

这篇关于如何使用CakePHP电子邮件组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆