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

前端 未结 5 1214
醉酒成梦
醉酒成梦 2021-02-10 03:49

I\'m trying to send email from a gmail account using CakEmail and SMTP settings .

It would be nice if someone tell the process step by step what to do .

I have a

5条回答
  •  一向
    一向 (楼主)
    2021-02-10 04:05

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

    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.

    Changes to the component:

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

    3. Change the render path in _getBodyText:

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

    1. Change the render path in _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. 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.

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

提交回复
热议问题