简体   繁体   English

配置CakePhp发送邮件用SMTP

[英]Configure CakePhp to send mail with SMTP

My web servers have disabled mail for security purposes I now need to reconfigure my cakephp code to send the emails via SMTP as recommended by the host.出于安全目的,我的 web 服务器已禁用邮件我现在需要重新配置我的 cakephp 代码,以便按照主机的建议通过 SMTP 发送电子邮件。

My code runs fine on localhost with php mail enabled我的代码在启用 php 邮件的 localhost 上运行良好

use Cake\Mailer\Email;

class LoansController extends AppController

public function sendtestemail(){
$email = new Email();
$email->setViewVars(['name' => 'test test', 'subject'=>'subject test', 
'message'=>'testit']);
$email
->template('bulkemail')
->emailFormat('html')
->to('info@test.co.ke')
->from('info@test.co.ke')
->subject($subject)
->send();
}

error: Could not send email: mail() has been disabled for security reasons Cake\Network\Exception\SocketException错误:无法发送 email:出于安全原因,mail() 已被禁用 Cake\Network\Exception\SocketException

My code runs fine on localhost with php mail enabled我的代码在启用 php 邮件的 localhost 上运行良好

It works fine in localhost but not in your remote hosting because your hosting company disabled it and you probably do not have much control over it.它在 localhost 中运行良好,但在您的远程托管中却不行,因为您的托管公司禁用了它,您可能对它没有太多控制权。

To send an email in cakephp use Email class of Cakephp 3. In app.php under config folder, add a new entry in the table EmailTransport. To send an email in cakephp use Email class of Cakephp 3. In app.php under config folder, add a new entry in the table EmailTransport.

In your case 'Smtp'.在你的情况下'Smtp'。 Specify host, port, username and password in it:在其中指定主机、端口、用户名和密码:

'EmailTransport' => [
        'default' => [
            'className' => 'Smtp',
            // The following keys are used in SMTP transports
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => 'user',
            'password' => 'secret',
            'client' => null,
            'tls' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
            ‘mail’=> [
                    'host' => 'smtp.gmail.com',
                    'port' => 587,
                    'username' =>xxxxx', //gmail id
                    'password' =>xxxxx, //gmail password
                    'tls' => true,
                    'className' => 'Smtp'
            ]
    ],

Now in Controller, the function to send email uses above-written entry in transport() function as below.现在在 Controller 中,function 发送 email 在传输中使用上面写的条目() ZC1C4254274E68A94D1 如下。

Add path in controller- use Cake\Mailer\Email:在控制器中添加路径 - 使用 Cake\Mailer\Email:

function sendEmail()
{           
           $message = "Hello User";            
            $email = new Email();
            $email->transport('mail');
            $email->from(['Sender_Email_id' => 'Sender Name'])
            ->to('Receiver_Email_id')
            ->subject(‘Test Subject’)
            ->attachments($path) //Path of attachment file
            ->send($message);

}

Also have in mind that many hosting companies also block default smtp ports.还要记住,许多托管公司也会阻止默认的 smtp 端口。 ( I'm aware that digital ocean does it, for example ). (例如,我知道数字海洋就是这样做的)。 So, you might have to change that port or contact them to get it opened for you ( usually after some sort of verification ).因此,您可能必须更改该端口或联系他们才能为您打开它(通常在某种验证之后)。

Some reference about what I just answered: https://www.digitalocean.com/community/questions/digital-ocean-firewall-blocking-sending-email关于我刚刚回答的一些参考: https://www.digitalocean.com/community/questions/digital-ocean-firewall-blocking-sending-email

What worked for me is fromhttps://book.cakephp.org/2/en/core-utility-libraries/email.html对我有用的是https://book.cakephp.org/2/en/core-utility-libraries/email.html

Edit /app/Config/email.php编辑 /app/Config/email.php

public $smtp = array(
       'transport' => 'Smtp',
        'from' => array('xxxxxxxxxx@gmail.com' => 'Betting site'),
        'host'      => 'ssl://smtp.gmail.com',
        'port'      => 465,
        'username'  => 'xxxxxxxxxx@gmail.com',
        'password'  => 'xxxxxxxxxxpass',
        'client'    => null,
        'log'       => true
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM