[英]Codeigniter Email PHP SMPT Error, cannot send mail through Gmail
我目前正在尝试在完成注册后发送电子邮件或验证电子邮件。 问题是由于此错误,我无法通过 gmail 发送验证:
遇到以下 SMTP 错误:无法使用 PHP SMTP 发送电子邮件。 您的服务器可能未配置为使用此方法发送邮件。
我已经尝试在 stackoverflow 中搜索解决方案,但我还没有找到任何适用于我的问题的解决方案。我也smtp_port
更改为465
但仍然没有解决我的问题。
我正在使用 Localhost 并在 xampp 文件夹上配置了php.ini
和sendmail.ini
。
配置文件
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = qwerty@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
发邮件
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=qwerty@gmail.com
auth_password=qwertyqwerty
force_sender=qwerty@gmail.com
我的代码
//get user inputs
$email = $this->input->post('email');
$password = $this->input->post('password');
//generate simple random code
$set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = substr(str_shuffle($set), 0, 12);
//insert user to users table and get id
$user['email'] = $email;
$user['password'] = $password;
$user['code'] = $code;
$user['active'] = false;
$id = $this->users_model->insert($user);
//set up email
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_user' => 'qwerty@gmail.com', // change it to yours
'smtp_pass' => 'qwertyqwerty', // change it to yours
'smtp_timeout' => "",
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,
'validate' => FALSE
);
$message = "
<html>
<head>
<title>Verification Code</title>
</head>
<body>
<h2>Thank you for Registering.</h2>
<p>Your Account:</p>
<p>Email: ".$email."</p>
<p>Password: ".$password."</p>
<p>Please click the link below to activate your account.</p>
<h4><a href='".base_url()."user/activate/".$id."/".$code."'>Activate My Account</a></h4>
</body>
</html>
";
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($config['smtp_user']);
$this->email->to($email);
$this->email->subject('Signup Verification Email');
$this->email->message($message);
//sending email
if($this->email->send()){
$this->session->set_flashdata('message','Activation code sent to email');
}
else{
$this->session->set_flashdata('message', $this->email->print_debugger());
}
您无需更改php.ini
或sendmail.ini
即可发送 SMTP。 请设为默认。
在我查看了您的 SMTP 配置之后,您错过了一件看起来很重要的小事:加密协议。
您可以在$config['smtp_host']
设置加密协议,如下所示:
$config['smtp_host'] = 'ssl://smtp.googlemail.com'
或者你也可以试试这个配置:
$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465"; // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl'; // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
然后将 $config 分配给电子邮件库: $this->load->library('email', $config)
;
请检查较少的安全性(在 gmail 设置中)是:已启用。
否则,您可以在 application/config/email.php 中创建 email.php 作为电子邮件配置库
把它放在那里:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.googlemail.com";
$config['smtp_port'] = "465"; // if you decided to use 465, then set smtp crypto to ssl, else for example 587, you need to set smtp crypto to tls.
$config['smtp_crypto'] = 'ssl'; // this is based on your smtp port -> ssl/tls
$config['smtp_timeout'] = "400";
$config['smtp_user'] = "youruser@gmail.com";
$config['smtp_pass'] = "yourpassword%&^$&$";
$config['validate'] = true;
$config['charset'] = 'utf-8';
$config['mailtype'] = "html";
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
?>
然后在你的控制器中,你不再需要设置你的 $config :D,但你所要做的只是调用$this->load->library('email');
在控制器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.