![](/img/trans.png)
[英]how can i send email in cakephp - to gmail account from localhost
[英]CakePHP-2.0: How can i send email from a gmail account using CakEmail and SMTP setting?
我正在尝试使用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
来自文档:
您可以配置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
正确的配置是:
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my@gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);
所以,不要忘记运输元素。
只需设置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->sender('noreply@mydomain.com', 'MyApp emailer');
使用Swiftmailer组件; 这是最容易使用的组件。
http://bakery.cakephp.org/articles/mhuggins/2008/06/11/improved-swiftmailer-component
在使用CakePHP 2.0之后,您需要进行一些更改。 CakePHP 2.0提供了一个“电子邮件”视图目录,用于存储所有电子邮件模板。
对组件的更改:
var
声明更改为public
改变public $layout = 'Emails';
to public $viewPath = '/Emails';
在_getBodyText
更改渲染路径:
$body = $this->controller->render($this->viewPath . DS . 'text' . DS . $view, $this->layout . DS . 'text'.DS.'default');
_getBodyHtml
更改渲染路径: $body = $this->controller->render($this->viewPath . DS . 'html' . DS . $view, $this->layout . DS . 'html'.DS.'default');
$bodyText = $this->_getBodyText($view); $message->setBody($bodyText, "text/plain");
如果您同时设置HTML和TEXT,Swiftmailer组件将发送空白电子邮件。 它从电子邮件视图中读取并添加文本正文。 如果你想发送HTML电子邮件,这就是评论这两行的原因。
第二个原因是如果两者都被激活并且您在email.html.ctp
和email.text.ctp
文件中都有内容,则会产生标题问题,因为只有文本格式才会显示在电子邮件中(实际上两种格式都存在)在标题中,但它抑制html部分并显示文本部分)。
我目前正在使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.