【发布时间】:2020-08-30 14:47:50
【问题描述】:
我目前正在尝试在完成注册后发送电子邮件或验证电子邮件。问题是由于此错误,我无法通过 gmail 发送验证:
遇到以下 SMTP 错误:无法使用以下方式发送电子邮件 PHP SMTP。您的服务器可能未配置为使用此功能发送邮件 方法。
我已经尝试在 stackoverflow 中搜索解决方案,但我没有找到任何适合我的问题的解决方案。我还完成了将 smtp_port 更改为 465 但仍然没有解决我的问题。
我正在使用 Localhost,并在 xampp 文件夹上配置了 php.ini 和 sendmail.ini。
php.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 codeigniter email