php - 遇到以下 SMTP 错误:0 无法使用 PHP SMTP 发送电子邮件
问题描述
this is controller code
public function register(){
$this->form_validation->set_rules('email', 'Email', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[30]');
$this->form_validation->set_rules('password_confirm', 'Confirm Password', 'required|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('register', $this->data);
}
else{
//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' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'testsourcecodester@gmail.com', // change it to yours
'smtp_pass' => 'mysourcepass', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$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());
}
redirect('register');
}
}
This is view
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CodeIgniter Signup with Email Verification</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="page-header text-center">CodeIgniter Signup with Email Verification</h1>
<div class="row">
<div class="col-sm-4">
<?php
if(validation_errors()){
?>
<div class="alert alert-info text-center">
<?php echo validation_errors(); ?>
</div>
<?php
}
if($this->session->flashdata('message')){
?>
<div class="alert alert-info text-center">
<?php echo $this->session->flashdata('message'); ?>
</div>
<?php
}
?>
<h3 class="text-center">Signup Form</h3>
<form method="POST" action="<?php echo base_url().'user/register'; ?>">
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" name="email" value="<?php echo set_value('email'); ?>">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" value="<?php echo set_value('password'); ?>">
</div>
<div class="form-group">
<label for="password_confirm">Password:</label>
<input type="password" class="form-control" id="password_confirm" name="password_confirm" value="<?php echo set_value('password_confirm'); ?>">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
<div class="col-sm-8">
<h3 class="text-center">Users Table</h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>UserID</th>
<th>Email</th>
<th>Password</th>
<th>Code</th>
<th>Active</th>
</tr>
</thead>
<tbody>
<?php
foreach($users as $row){
?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->password; ?></td>
<td><?php echo $row->code; ?></td>
<td><?php echo $row->active ? 'True' : 'False'; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
在 CodeIgniter 中发送电子邮件时出现错误。
遇到以下 SMTP 错误:0 无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。
我在本地主机上做。代码中有什么问题是否可以使用localhost在CodeIgniter中发送电子邮件如何解决代码中的问题?如果需要更多会帮助你。我必须进行什么类型的设置才能发送电子邮件?
解决方案
This is a working code to send mails from localhost
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxx@gmail.com', // change it to yours
'smtp_pass' => 'xxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = 'hi test Mailer';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxx@gmail.com'); // change it to yours
$this->email->to('xxx@gmail.com');// change it to yours
$this->email->subject('Test mail from localhost');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
推荐阅读
- java - 石头剪刀布 Java 决定
- postgresql - PostgreSQL 过滤不完整年份
- jquery - 如何将变量传递给另一个 JSP 页面?
- javascript - 如何在深层嵌套对象中保存状态?
- mysql - 在 SQL 中将行转换为列
- azure - 如何删除自定义指标应用洞察?
- dataset - 下载 KITTY 评估数据集的方法
- jms - apache active mq消息已出队,但未调用@JmsListener的方法
- cobbler - 使用 cobbler(kickstart 文件)配置第二个接口并分配静态 IP
- visual-studio-code - 如何在 VSCode 中禁用代码片段的 Enter 按钮