简体   繁体   English

CakePHP-2.0:如何使用CakEmail和SMTP设置从Gmail帐户发送电子邮件?

[英]CakePHP-2.0: How can i send email from a gmail account using CakEmail and SMTP setting?

I'm trying to send email from a gmail account using CakEmail and SMTP settings . 我正在尝试使用CakEmail和SMTP设置从Gmail帐户发送电子邮件。

It would be nice if someone tell the process step by step what to do . 如果有人一步一步告诉过程该怎么做,那将是很好的。

I have added the following in app/Config/email.php=> 我在app / Config / email.php =>中添加了以下内容

<?php
class EmailConfig {
    public $smtp = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => 'my@gmail.com',
        'password' => 'secret'
    );
}

Now how can i send email to any email account from "my@gmail.com" ? 现在我如何从“my@gmail.com”向任何电子邮件帐户发送电子邮件?

It's CakePHP-2.0 这是CakePHP-2.0

From the docs: 来自文档:

You can configure SSL SMTP servers, like GMail. 您可以配置SSL SMTP服务器,例如GMail。 To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. 为此,请将'ssl://'放在主机的前缀中,并相应地配置端口值。 Example: 例:

<?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 http://book.cakephp.org/2.0/en/core-utility-libraries/email.html?highlight=cakeemail#CakeEmail

The right configuration is: 正确的配置是:

public $gmail = array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'my@gmail.com',
    'password' => 'secret',
    'transport' => 'Smtp'
);

So, don't forget the transport element. 所以,不要忘记运输元素。

Just set the from : 只需设置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

should do it. 应该这样做。

You may want to set the sender as well; 您可能还想设置sender ; I'm not 100% but I imagine it will be useful when sending email "from" gmail via your own website; 我不是100%,但我想通过你自己的网站“从”gmail发送电子邮件会很有用; perhaps to stop the email being picked up as spam. 也许是为了阻止电子邮件被收集为垃圾邮件。

$email->sender('noreply@mydomain.com', 'MyApp emailer');

Use the Swiftmailer component; 使用Swiftmailer组件; this is the easiest component to use. 这是最容易使用的组件。

http://bakery.cakephp.org/articles/mhuggins/2008/06/11/improved-swiftmailer-component http://bakery.cakephp.org/articles/mhuggins/2008/06/11/improved-swiftmailer-component

There are some changes that you need to do while using this with CakePHP 2.0 onwards. 在使用CakePHP 2.0之后,您需要进行一些更改。 CakePHP 2.0 provides an 'Emails' view directory, which is used to store all the email templates. CakePHP 2.0提供了一个“电子邮件”视图目录,用于存储所有电子邮件模板。

Changes to the component: 对组件的更改:

  1. Change all var declarations to public 将所有var声明更改为public
  2. Change public $layout = 'Emails'; 改变public $layout = 'Emails'; to public $viewPath = '/Emails'; to public $viewPath = '/Emails';

  3. Change the render path in _getBodyText : _getBodyText更改渲染路径:

$body = $this->controller->render($this->viewPath . DS . 'text' . DS . $view, $this->layout . DS . 'text'.DS.'default');

  1. Change the render path in _getBodyHtml : _getBodyHtml更改渲染路径:

$body = $this->controller->render($this->viewPath . DS . 'html' . DS . $view, $this->layout . DS . 'html'.DS.'default');

  1. Comment out the lines: 注释掉这些台词:

$bodyText = $this->_getBodyText($view); $message->setBody($bodyText, "text/plain");

The Swiftmailer component sends a blank email if you set both HTML & TEXT active. 如果您同时设置HTML和TEXT,Swiftmailer组件将发送空白电子邮件。 It reads from both the email views & adds the body for text. 它从电子邮件视图中读取并添加文本正文。 That's the reason to comment these two lines if you want to send html emails. 如果你想发送HTML电子邮件,这就是评论这两行的原因。

The second reason is if both are activated & you have content in both email.html.ctp & email.text.ctp files, it creates an header issue in that only the text format gets displayed in emails (in reality both the formats are present in header, but it suppresses the html part & shows the text part). 第二个原因是如果两者都被激活并且您在email.html.ctpemail.text.ctp文件中都有内容,则会产生标题问题,因为只有文本格式才会显示在电子邮件中(实际上两种格式都存在)在标题中,但它抑制html部分并显示文本部分)。

I am currently using a gmail account to send outbound mail. 我目前正在使用Gmail帐户发送出站邮件。 I'm using templates and a reusable email setup function. 我正在使用模板和可重复使用的电子邮件设置功能。 Here is a copy of my working code: 这是我的工作代码的副本:

// 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.

相关问题 我如何在Cakephp中发送电子邮件-从本地主机到gmail帐户 - how can i send email in cakephp - to gmail account from localhost CakePHP-2.0:当有人在CakePHP-2.0中命中URL http:// site / controller / unknownact​​ion时,如何重定向到主页? - CakePHP-2.0: How can i redirect to home page when someone hits url http://site/controller/unknownaction in CakePHP-2.0? CakePHP-2.0和Migration插件:如何使id列为auto_increment? - CakePHP-2.0 and Migration plugin: How can i make id column auto_increment? CakePHP-2.0:如何检查控制器的缺失动作/任务视图? - CakePHP-2.0: How can i check missing action/ missiong view for a controller? CakePHP-2.0,如何设置与CakeDC-Users用户模型和Post模型之间的模型关系? - CakePHP-2.0, How can i set model relation with CakeDC-Users User model with Post model? 如何使用gmail SMTP从IIS PHP发送电子邮件? - How to send email from IIS PHP using gmail SMTP? 无法将项目升级到CakePHP-2.0 - Can't upgrade the project to CakePHP-2.0 尝试使用 smtp.gmail.com 从 gmail 发送电子邮件 - Trying to send email from gmail using smtp.gmail.com 如何使用php将使用php的邮件发送到Gmail帐户 - How to send mail using php to gmail account using SMTP 使用gmail SMTP发送电子邮件,“发件​​人”字段重要吗? 我需要首先设置帐户权限吗? - sending email using gmail SMTP, does the 'from' field matters? do I need to set account permission first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM