[英]Php mailer in codeigniter
我想在注冊用戶時發送電子郵件。 我正在使用xampp,codeigniter和phpMailer。 當我提交注冊表時,這是一個錯誤
Message was not sent
PHPMailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Registration Successfully !
這是我的代碼(控制器)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to start session in order to access it through CI
//The controller class is extends CI_Contoller
Class User_Authentication extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
$this->load->library('my_phpmailer');
// Load session library
$this->load->library('session');
// Load database
$this->load->model('login_database');
}
// Show login page
public function index() {
$this->load->view('user_site/login_form');
}
// Show registration page
public function user_registration_show() {
$this->load->view('user_site/registration_form');
}
// Validate and store registration data in database
public function new_user_registration() {
// Check validation for user input in SignUp form
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|md5');
if ($this->form_validation->run() == FALSE) {
$this->load->view('user_site/registration_form');
// $this->load->view('register');
} else {
$data = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'address' => $this->input->post('address'),
'contact_no' => $this->input->post('contact_no')
);
$result = $this->login_database->registration_insert($data);
if ($result == TRUE) {
$data['message_display'] = 'Registration Successfully !';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
// or try these settings (worked on XAMPP and WAMP):
// $mail->Port = 587;
// $mail->SMTPSecure = 'tls';
$mail->Username = "ashik@gmail.com";
$mail->Password = "zswedfr";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
$mail->From = "ashik@gmail.com";
$mail->FromName = "ABC";
$address = $_POST['email'];
$mail->AddAddress($address, "Guest");
$mail->Subject = "ABC Email validation ";
$mail->Body ="ABC Email validation";
if(!$mail->Send()){
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
}else{
echo "sucfdt " ;
}
$this->load->view('user_site/login_form', $data);
} else {
$data['message_display'] = 'Email already exist!';
//$this->load->view('user_site/registration_form', $data);
$this->load->view('register');
}
}
}
這是庫中的php文件
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_PHPMailer {
public function My_PHPMailer() {
require_once('PHPMailer/PHPMailerAutoload.php');
}
}
此代碼給出了gmail服務器中身份驗證失敗的錯誤。 這是因為gmail假設登錄計算機/位置已更改,因此它是吉祥的。 如果您登錄gmail並查看安全性,最近登錄的設備,則可以看到一個彈出窗口,顯示該登錄已被服務器地址屏蔽。 在這里檢查您的訪問狀態
如果啟用它,它將起作用。 轉到此鏈接進行解鎖
參見下面的線程。 您也可以參考此。 PHPMailer-SMTP錯誤:從我的服務器發送郵件時,密碼命令失敗
在application / config /文件夾中創建一個email.php文件:
並將這些內容放入其中:
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = ''; //change this
$config['smtp_pass'] = ''; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.