簡體   English   中英

CakePHP-2.0:如何使用CakEmail和SMTP設置從Gmail帳戶發送電子郵件?

[英]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提供了一個“電子郵件”視圖目錄,用於存儲所有電子郵件模板。

對組件的更改:

  1. 將所有var聲明更改為public
  2. 改變public $layout = 'Emails'; to public $viewPath = '/Emails';

  3. _getBodyText更改渲染路徑:

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

  1. _getBodyHtml更改渲染路徑:

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

  1. 注釋掉這些台詞:

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

如果您同時設置HTML和TEXT,Swiftmailer組件將發送空白電子郵件。 它從電子郵件視圖中讀取並添加文本正文。 如果你想發送HTML電子郵件,這就是評論這兩行的原因。

第二個原因是如果兩者都被激活並且您在email.html.ctpemail.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM