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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7915318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:37:53  来源:igfitidea点击:

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

phpemailcakephpcakephp-2.0

提问by shibly

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' => '[email protected]',
        'password' => 'secret'
    );
}

Now how can i send email to any email account from "[email protected]" ?

现在如何从“[email protected]”向任何电子邮件帐户发送电子邮件?

It's CakePHP-2.0

这是 CakePHP-2.0

回答by RichardAtHome

From the docs:

从文档:

You can configure SSL SMTP servers, like GMail. To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. Example:

您可以配置 SSL SMTP 服务器,例如 GMail。为此,请将“ssl://”置于主机中的前缀并相应地配置端口值。例子:

<?php
class EmailConfig {
    public $gmail = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => '[email protected]',
        '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

回答by Dirk

The right configuration is:

正确的配置是:

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

So, don't forget the transportelement.

所以,不要忘记传输元素。

回答by Ross

Just set the from:

只需设置from

<?php
$email = new CakeEmail();
$email->from(array('[email protected]' => 'Your Name'));
$email->to('[email protected]');
$email->subject('Sent from Gmail');
$email->send('My message'); // or use a template etc

should do it.

应该这样做。

You may want to set the senderas well; I'm not 100% but I imagine it will be useful when sending email "from" gmail via your own website; perhaps to stop the email being picked up as spam.

您可能还想设置sender;我不是 100%,但我想这在通过您自己的网站“从”gmail 发送电子邮件时会很有用;也许是为了阻止电子邮件被视为垃圾邮件。

$email->sender('[email protected]', 'MyApp emailer');

$email->sender('[email protected]', 'MyApp emailer');

回答by Tom Rose

I am currently using a gmail account to send outbound mail. I'm using templates and a reusable email setup function. Here is a copy of my working code:

我目前正在使用 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'=>'[email protected]',
        'password'=>'secret',
    );
    $this->Email->delivery = 'smtp';
    //$this->Email->delivery = 'debug';
    $this->Email->from    = 'Username <[email protected]>';
    $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, '[email protected]', '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

回答by Vijay Kumbhar

Use the Swiftmailer component; this is the easiest component to use.

使用 Swiftmailer 组件;这是最容易使用的组件。

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 provides an 'Emails' view directory, which is used to store all the email templates.

在 CakePHP 2.0 以后使用它时,您需要做一些更改。CakePHP 2.0 提供了一个“Emails”视图目录,用于存储所有电子邮件模板。

Changes to the component:

对组件的更改:

  1. Change all vardeclarations to public
  2. Change public $layout = 'Emails';to public $viewPath = '/Emails';

  3. Change the render path in _getBodyText:

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

  3. 更改渲染路径_getBodyText

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

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

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

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

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

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

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

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

The Swiftmailer component sends a blank email if you set both HTML & TEXT active. 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 和 TEXT 都设置为活动状态,Swiftmailer 组件会发送一封空白电子邮件。它从两个电子邮件视图中读取并添加文本正文。如果您想发送 html 电子邮件,这就是注释这两行的原因。

The second reason is if both are activated & you have content in both email.html.ctp& email.text.ctpfiles, 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.ctp&email.text.ctp文件中都有内容,它会产生一个标题问题,因为只有文本格式显示在电子邮件中(实际上这两种格式都存在于标题中,但它抑制了 html 部分 &显示文本部分)。