为了安全起见,我的Web服务器已禁用邮件,我现在需要重新配置cakephp代码,以按照主机的建议通过SMTP发送电子邮件。
我的代码在启用了php邮件的本地主机上运行良好
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('[email protected]')
->from('[email protected]')
->subject($subject)
->send();
}
错误:无法发送电子邮件:由于安全原因,已禁用mail()Cake \ Network \ Exception \ SocketException
我的代码在启用了php邮件的本地主机上运行良好
它可以在localhost上正常工作,但不能在远程主机上正常工作,因为您的托管公司已将其禁用,并且您可能对其没有太多控制权。
要使用cakephp发送电子邮件,请使用Cakephp的Email类。3.在app.php的config文件夹下,在表EmailTransport中添加一个新条目。
您的情况是“ Smtp”。在其中指定主机,端口,用户名和密码:
'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'
]
],
现在,在Controller中,发送电子邮件的功能使用上面transport()函数中编写的条目,如下所示。
在控制器中添加路径-使用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);
}
还请记住,许多托管公司还阻止默认的smtp端口。(例如,我知道数字海洋已经做到了)。因此,您可能必须更改该端口或与他们联系以为您打开该端口(通常在进行某种形式的验证之后)。
关于我刚刚回答的一些参考:https : //www.digitalocean.com/community/questions/digital-ocean-firewall-blocking-sending-email
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句