Codeigniter 3:发送账户激活邮件失败
Codeigniter 3: send account activation email fails
我正在使用 CodeIgniter 3 和 Twitter 注册和登录 应用程序Bootstrap。
当用户registers时,应向提供的地址he/she发送一封电子邮件,并附上帐户确认link。问题是确认电子邮件 没有发送 。
在 Usermodel 我有:
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
{
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.code-love.tk',
'smtp_port' => 465,
'smtp_user' => 'razvan@code-love.tk',
'smtp_pass' => '******',
'smtp_crypto' => 'ssl',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$messg = 'Wellcome, '. $first_name . ' ' . $last_name . '! Click the <strong><a href="'.site_url('/signin/activateaccount/'. $verification_key).'">confirmation link</a></strong> to confirm your account.';
$this->email->initialize($config);
$this->email->set_newline('\r\n');
$this->email->from('mail.code-love.tk','Razvan Zamfir');
$this->email->to($email);
$this->email->subject('Account activation');
$this->email->message($messg);
if (!$this->email->send()) {
show_error($this->email->print_debugger());
}
else {
echo 'Your e-mail has been sent!';
}
}
在我的注册控制器中我有这个代码:
public function signup() {
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$verification_key = md5($email);
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$active = 0;
// Load user model
$this->load->model('Usermodel');
// If email does not already exist in the database
// signup new user
if (!$this->Usermodel->email_exists($email)) {
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $verification_key, $date_created, $date_updated, $active) && $this->Usermodel->activationEmail($first_name, $last_name, $email, $verification_key)) {
$this->session->set_flashdata("signup_success", "Your account has just bean created. You must confirm it before you can sign in. We have send you a confirmation email at $email for this purpose.");
} else {
// unless sigup does not fail for whatever reason
$this->session->set_flashdata("signup_failure", "We ware unable to create your account.");
}
redirect('signup');
} else {
// If email is already in the database
// urge user to sign in (redirect to signup page too)
$this->session->set_flashdata("email_exists", "The email address $email already exists in the database. Please signin.");
redirect('signin');
}
} else {
$this->load->view('signup');
}
}
注册确实发生了,下面的错误,验证邮件没有发送。我不是从本地 XAMPP/WAMP/MAMP 服务器执行此操作,而是从 "live" 服务器执行此操作,您可以 signup yourself.
Severity: Warning
Message: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Filename: libraries/Email.php
Line Number: 1950
Backtrace:
File: /home/roxoqdat/code-love.tk/ciauth/application/models/Usermodel.php
Line: 52
Function: send
File: /home/roxoqdat/code-love.tk/ciauth/application/controllers/Signup.php
Line: 42
Function: activationEmail
File: /home/roxoqdat/code-love.tk/ciauth/index.php
Line: 292
Function: require_once
我做错了什么?
首先,设置 flashdata 后,我没有看到您将用户重定向到他们可以看到它的地方...
还有,先发邮件,然后你设置flash数据为An Activation Link Has Been Sent To Your Email, Please Check Your Email To Activate Your Account
!
所以,
用户注册,
仅当电子邮件不存在时才会向他们发送电子邮件,
为可能出现的任何错误设置闪存数据...例如:
给邮件发送函数赋一个变量:
$send_email = $this->activationEmail('params');
在你的 activationEmail
函数中,确保有一个像这样的简单控制结构:
$success=$this->email->send();
if($success){ return true; } else{ return false; }
现在在你的控制器中,比较邮件是否真的发送了:
if($send_email == true){ $this->session->flashdata('success','Your success message');
// use the redirect() helper function to the function that loads your view
} else { //set failed flashdata and redirect to where you want it seen like above}
你就知道问题出在哪里了。
先让程序在脑海中流动,然后再执行。先试试,然后马上回复我。
希望能有所帮助
调试参数是否通过(特别是 last_name)。尝试像这样编写模型方法以避免显示错误
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
尝试将 header 添加到您的电子邮件(您设置了两次邮件类型,可能是个问题)。
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
检查您是否有所需的 SMTP 服务。
尝试使用 google SMTP 服务。
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('mygmail@gmail.com', 'myname');
$this->email->to('target@gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$result = $this->email->send();
接下来试试:
$config = array('mailtype' => 'html');
从 $config = array 中删除其余部分。
我正在使用 CodeIgniter 3 和 Twitter 注册和登录 应用程序Bootstrap。
当用户registers时,应向提供的地址he/she发送一封电子邮件,并附上帐户确认link。问题是确认电子邮件 没有发送 。
在 Usermodel 我有:
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
{
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.code-love.tk',
'smtp_port' => 465,
'smtp_user' => 'razvan@code-love.tk',
'smtp_pass' => '******',
'smtp_crypto' => 'ssl',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$messg = 'Wellcome, '. $first_name . ' ' . $last_name . '! Click the <strong><a href="'.site_url('/signin/activateaccount/'. $verification_key).'">confirmation link</a></strong> to confirm your account.';
$this->email->initialize($config);
$this->email->set_newline('\r\n');
$this->email->from('mail.code-love.tk','Razvan Zamfir');
$this->email->to($email);
$this->email->subject('Account activation');
$this->email->message($messg);
if (!$this->email->send()) {
show_error($this->email->print_debugger());
}
else {
echo 'Your e-mail has been sent!';
}
}
在我的注册控制器中我有这个代码:
public function signup() {
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$verification_key = md5($email);
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$active = 0;
// Load user model
$this->load->model('Usermodel');
// If email does not already exist in the database
// signup new user
if (!$this->Usermodel->email_exists($email)) {
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $verification_key, $date_created, $date_updated, $active) && $this->Usermodel->activationEmail($first_name, $last_name, $email, $verification_key)) {
$this->session->set_flashdata("signup_success", "Your account has just bean created. You must confirm it before you can sign in. We have send you a confirmation email at $email for this purpose.");
} else {
// unless sigup does not fail for whatever reason
$this->session->set_flashdata("signup_failure", "We ware unable to create your account.");
}
redirect('signup');
} else {
// If email is already in the database
// urge user to sign in (redirect to signup page too)
$this->session->set_flashdata("email_exists", "The email address $email already exists in the database. Please signin.");
redirect('signin');
}
} else {
$this->load->view('signup');
}
}
注册确实发生了,下面的错误,验证邮件没有发送。我不是从本地 XAMPP/WAMP/MAMP 服务器执行此操作,而是从 "live" 服务器执行此操作,您可以 signup yourself.
Severity: Warning
Message: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Filename: libraries/Email.php
Line Number: 1950
Backtrace:
File: /home/roxoqdat/code-love.tk/ciauth/application/models/Usermodel.php
Line: 52
Function: send
File: /home/roxoqdat/code-love.tk/ciauth/application/controllers/Signup.php
Line: 42
Function: activationEmail
File: /home/roxoqdat/code-love.tk/ciauth/index.php
Line: 292
Function: require_once
我做错了什么?
首先,设置 flashdata 后,我没有看到您将用户重定向到他们可以看到它的地方...
还有,先发邮件,然后你设置flash数据为An Activation Link Has Been Sent To Your Email, Please Check Your Email To Activate Your Account
!
所以,
用户注册,
仅当电子邮件不存在时才会向他们发送电子邮件,
为可能出现的任何错误设置闪存数据...例如:
给邮件发送函数赋一个变量:
$send_email = $this->activationEmail('params');
在你的 activationEmail
函数中,确保有一个像这样的简单控制结构:
$success=$this->email->send();
if($success){ return true; } else{ return false; }
现在在你的控制器中,比较邮件是否真的发送了:
if($send_email == true){ $this->session->flashdata('success','Your success message');
// use the redirect() helper function to the function that loads your view
} else { //set failed flashdata and redirect to where you want it seen like above}
你就知道问题出在哪里了。 先让程序在脑海中流动,然后再执行。先试试,然后马上回复我。 希望能有所帮助
调试参数是否通过(特别是 last_name)。尝试像这样编写模型方法以避免显示错误
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
尝试将 header 添加到您的电子邮件(您设置了两次邮件类型,可能是个问题)。
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
检查您是否有所需的 SMTP 服务。 尝试使用 google SMTP 服务。
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('mygmail@gmail.com', 'myname');
$this->email->to('target@gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$result = $this->email->send();
接下来试试:
$config = array('mailtype' => 'html');
从 $config = array 中删除其余部分。