使用 SMTP codeigniter 发送电子邮件
Sending Email using SMTP codeigniter
我正在尝试使用 smtp codeigniter 发送电子邮件。我使用的代码如下:
public function notify_marketing(){
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myvalidemail@gmail.com',
'smtp_pass' => '*******',//my valid email password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('myvalidemail@gmail.com');
$this->email->to('validreceiptent@gmail.com');
$this->email->subject('My Subject');
$this->email->message('Hello there');
if($this->email->send())
{
$this->session->set_flashdata("success","Email sent.");
}
else
{
show_error($this->email->print_debugger());
}
}
但是我在响应中收到以下错误。我尝试了该站点的其他解决方案,但没有用。
<div id="exception_error">
<h1><span class="type">An Error Was Encountered [ 500 ]</span></h1>
<div class="content">
<p><p>220 smtp.googlemail.com ESMTP bv4sm16669443pbb.86 - gsmtp
<br /><pre>hello: 250-smtp.googlemail.com at your service, [110.44.127.179]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
</pre>lang:email_smtp_auth_pw<br />lang:email_send_failure_smtp<br /><pre>User-Agent: CodeIgniter
Date: Tue, 18 Aug 2015 12:03:42 +0545
From: <********@gmail.com>
Return-Path: <********@gmail.com>
To: *******@gmail.com
Subject: =?iso-8859-1?Q?My_Subject?=
Reply-To: "********@gmail.com" <*********@gmail.com>
X-Sender: *******@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <55d2ce42747f5@gmail.com>
Mime-Version: 1.0
提前致谢。
代码完美。
您需要使用 gmail 的应用程序密码,而不是 gmail 密码。请从 gmail 设置中设置应用程序密码并在此处提供密码。
届时它将完美运行。查看来自 HERE
的更多内容
在 gmail 中设置应用程序密码的过程:
- 访问您的 App passwords page。系统可能会要求您登录 Google 帐户。
- 点击底部的 Select 应用,然后选择您正在使用的应用。
- 单击 Select 设备并选择您正在使用的设备。
- Select 生成。
- 按照说明在您的设备上输入 App 密码(黄色栏中的 16 位字符代码)。
- Select完成。
完成后,您将不会再看到该 App 密码。但是,您会看到已为其创建应用密码的应用和设备列表。
试试这个..
在 'application/config' 文件夹中创建一个名为 'email.php' 的文件,并向其中添加以下设置。
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user@gmail.com'; //change this
$config['smtp_pass'] = 'password'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
?>
在控制器中:
<?php
//send mail
function sendmail()
{
$this->load->library('email'); // load email library
$this->email->from('user@gmail.com', 'sender name');
$this->email->to('test1@gmail.com');
$this->email->cc('test2@gmail.com');
$this->email->subject('Your Subject');
$this->email->message('Your Message');
$this->email->attach('/path/to/file1.png'); // attach file
$this->email->attach('/path/to/file2.pdf');
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
?>
我正在尝试使用 smtp codeigniter 发送电子邮件。我使用的代码如下:
public function notify_marketing(){
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myvalidemail@gmail.com',
'smtp_pass' => '*******',//my valid email password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('myvalidemail@gmail.com');
$this->email->to('validreceiptent@gmail.com');
$this->email->subject('My Subject');
$this->email->message('Hello there');
if($this->email->send())
{
$this->session->set_flashdata("success","Email sent.");
}
else
{
show_error($this->email->print_debugger());
}
}
但是我在响应中收到以下错误。我尝试了该站点的其他解决方案,但没有用。
<div id="exception_error">
<h1><span class="type">An Error Was Encountered [ 500 ]</span></h1>
<div class="content">
<p><p>220 smtp.googlemail.com ESMTP bv4sm16669443pbb.86 - gsmtp
<br /><pre>hello: 250-smtp.googlemail.com at your service, [110.44.127.179]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
</pre>lang:email_smtp_auth_pw<br />lang:email_send_failure_smtp<br /><pre>User-Agent: CodeIgniter
Date: Tue, 18 Aug 2015 12:03:42 +0545
From: <********@gmail.com>
Return-Path: <********@gmail.com>
To: *******@gmail.com
Subject: =?iso-8859-1?Q?My_Subject?=
Reply-To: "********@gmail.com" <*********@gmail.com>
X-Sender: *******@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <55d2ce42747f5@gmail.com>
Mime-Version: 1.0
提前致谢。
代码完美。
您需要使用 gmail 的应用程序密码,而不是 gmail 密码。请从 gmail 设置中设置应用程序密码并在此处提供密码。
届时它将完美运行。查看来自 HERE
的更多内容在 gmail 中设置应用程序密码的过程:
- 访问您的 App passwords page。系统可能会要求您登录 Google 帐户。
- 点击底部的 Select 应用,然后选择您正在使用的应用。
- 单击 Select 设备并选择您正在使用的设备。
- Select 生成。
- 按照说明在您的设备上输入 App 密码(黄色栏中的 16 位字符代码)。
- Select完成。
完成后,您将不会再看到该 App 密码。但是,您会看到已为其创建应用密码的应用和设备列表。
试试这个..
在 'application/config' 文件夹中创建一个名为 'email.php' 的文件,并向其中添加以下设置。
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user@gmail.com'; //change this
$config['smtp_pass'] = 'password'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
?>
在控制器中:
<?php
//send mail
function sendmail()
{
$this->load->library('email'); // load email library
$this->email->from('user@gmail.com', 'sender name');
$this->email->to('test1@gmail.com');
$this->email->cc('test2@gmail.com');
$this->email->subject('Your Subject');
$this->email->message('Your Message');
$this->email->attach('/path/to/file1.png'); // attach file
$this->email->attach('/path/to/file2.pdf');
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
?>