I want to send an email verification link to the user with PHP CodeIgniter.
This is my controller function.
public function Sent_Confirmation_Email()
{
$emailid = $this->uri->segment(3);
$verificationLink = base_url() . 'MainController/Confirm_Activation/'.$emailid;
$msg .= "Please use the link below to activate your account..<br /><br /><br />";
$msg .= "<a href='".$verificationLink."' target='_blank'>VERIFY EMAIL</a><br /><br /><br />";
$msg .= "Kind regards,<br />";
$msg .= "Company Name";
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'sender#gmail.com',
'smtp_pass' => 'password'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->isHTML(true);
$this->email->from("sender#gmail.com");
$this->email->to("$emailid");
$this->email->subject("Email Confirmation - Courses and Tutors");
$this->email->message($msg);
if($this->email->send())
{
$this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
} else {
show_error($this->email->print_debugger());
}
}
Note that I am sending emails from my localhost. I receive the email but the problem is it shows the html tags as well. This is the email which I received:
Please use the link below to activate your account..<br /><br /><br /><a
href='http://localhost/tutorhunt/MainController/Confirm_Activation/fareedshuja#gmail.com'
target='_blank'>VERIFY EMAIL</a><br /><br /><br />Kind regards,<br />Company
Name
Try to initialize email library and add mailtype as following
$this->email->initialize(array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://mailserver',
'smtp_user' => 'user',
'smtp_pass' => 'password',
'smtp_port' => 465,
'crlf' => "\r\n",
'newline' => "\r\n",
'mailtype' => 'html',
));
To Send Verification Link In Email Using Email Template is simpler than adding HTML content as a message and send it.
Creare an array of the variable which you want to send in an email.
Load that email template view and load that variable array and set the variable in the template properly.
Here is the code sample:
$this->email->set_newline("\r\n");
$this->email->isHTML(true);
$this->email->from("sender#gmail.com");
$this->email->to("$emailid");
$this->email->subject("Email Confirmation - Courses and Tutors");
$template_data = array(
'verificationLink' => base_url() . 'MainController/Confirm_Activation/'.$emailid,
'message' => 'Please use the link below to activate your account..',
'company_name' => 'test company'
);
$body = $this->load->view ('your_view.php', ['template_data'=>$template_data], TRUE); //Set the variable in template Properly
$this->email->message ( $body );
if($this->email->send())
{
$this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
} else {
show_error($this->email->print_debugger());
}
Related
I am trying to send a registration verification email to my new user via Gmail SMTP. user data is successfully saved to the database but email is not sent to the user email ID.
I am working on localhost
here is my code
$subject = "Please verify email for login";
$message = "
<p>Hi " . $this->input->post('hospital_name') . "</p>
<p>This is email verification mail from Codeigniter Login Register system. For complete registration process and login into system.
First you want to verify you email by click this <a href='" . base_url() . "register/verify_email/" . $verification_key . "'>link</a>.</p>
<p>Once you click this link your email will be verified and you can login into system.</p>
<p>Thanks,</p>
";
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'tls://smtp.googlemail.com',
'smtp_port' => 587,
'smtp_user' => '****',
'smtp_pass' => '****',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('****');
$this->email->to($this->input->post('hospital_email'));
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
$this->session->set_flashdata('message', 'Check in your email for email verification mail');
redirect('register');
}else{
$this->session->set_flashdata('message', 'something wrong');
}
There could be a possibility that there might be something wrong with the code and it would be throwing some error. Did you check in the session, what is the message you're receiving?
If there's an error, try checking what the error could possibly be. You may check the error by using the below code.
if($this->email->send()) {
echo 'Your Email has successfully been sent.';
}
else{
show_error($this->email->print_debugger());
}
Applying for a job need to send an email with some of the details like name,phonenumber,email,current ctc etcc..Email is sending correctly but the problem is while sending an email in subject line i need to include name email ctc...for this i have done this but it is not accepting in this format.
$name = $this->input->post('fullname');
$email = $this->input->post('email');
$phone = $this->input->post('mobilenumber');
$currentemploymentstatus = $this->input->post('current_employment_status');
//set to_email id to which you want to receive mails
$to_email = 'yyyy#gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxxxx#gmail.com',
'smtp_pass' => 'PASSWORD123', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = array();
$message[] = 'Fullname : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Mobile : '.trim($phone).' ';
$message[] = 'Current Employment Status : '.trim($currentemploymentstatus).' ';
//$message = implode(PHP_EOL, $message);
$message = implode('<br>', $message);
//send mail
$this->load->library('email',$config);
$this->email->from($email);
$this->email->to($to_email);
//$list = array();
$this->email->subject($name|$email);
$this->email->message($message);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
if ($this->email->send())
{
$this->flash->success('Thank you for applying to this post we will get back to you soon!</div>');
redirect('apply');
}
else
{
$this->flash->success('There is error in sending mail! Please try again later');
redirect('apply');
}
}
Just Solved by adding this code.
$subject = $name .' | '.$currentemploymentstatus .' | '.$expectedctc .' | '.$primaryskills;
$this->email->subject($subject);
For your problem try changing "|" to .'|'
To clean up your code and to make your life easier:
If you are using CI, check out my repository on setting up an email_model to simplify the code you are using. Instead of loading the config stuff for the library all you will need to do is hit the model to send to email queue and hit the model a second time to process the queue
https://github.com/ddell003/Email_model
Once submittied the form sending an email with verification link to user clicking on that link getting 500 internal server error.Once i click on the link status is to changed to 1 from 0 and need to set the time limit for the link
Error:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
Here is my code for email verification.
Signup:
function signup()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('email','Email','required|is_unique[profile_details.email]');
if($this->form_validation->run()== FALSE)
{
$data['country'] = $this->signup_model->getcountry();
$data['mainpage']='signup';
$this->load->view('templates/template',$data);
}
else
{
$data=array(
'email'=>$this->input->post('email'),
);
if($this->signup_model->insertprofiledetails($data))
{
if ($this->signup_model->sendEmail($this->input->post('email')))
{
redirect("index.php/welcome/add");
}
else
{
redirect("index.php/welcome/add");
}
}
else
{
redirect('index.php/welcome/add');
}
}
}
function verify($hash=NULL)
{
if ($this->signup_model->verifyEmailID($hash))
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-success text-center">Your Email Address is successfully verified! Please login to access your account!</div>');
redirect('index.php/welcome/add');
}
else
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-danger text-center">Sorry! There is error verifying your Email Address!</div>');
redirect('index.php/welcome/add');
}
}
Model:
function sendEmail($to_email)
{
//configure email settings
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxx#gmail.com',
'smtp_pass' => 'yyyyy', //$from_email password
'mailtype' =>'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
//send mail
$this->load->library('email',$config);
$this->email->from('xxxx#gmail.com', 'Admin');
$this->email->to('yyyyy#gmail.com');
$this->email->subject('Email Verification');
$this->email->message('Dear User,<br /><br />Please click on the below activation link to verify your email address.<br /><br /> http://qa.domain.in/index.php/welcome/verify/' . md5($to_email) . '<br /><br /><br />Thanks<br />Mydomain Team');
$this->email->set_newline("\r\n");
if($this->email->send())
{
redirect('welcome/add');
} else { echo $this->email->print_debugger(); }
}
function verifyEmailID($key)
{
$data = array('status' => 1);
$this->db->where('md5(email)', $key);
return $this->db->update('profile_details', $data);
}
Check to see if you are missing any closing brackets for any methods in the signup_model class or the sign_up model class itself or for the controller
If I submit to reset the new password to my email, CodeIgniter is not sending mail. But it's returning a message that Password Reset Email Sent. So it doesn't trigger an error.
I'm using CodeIgniter 2.1.4 and IonAuth 2.5.2
$config['use_ci_email'] = TRUE;
I already set this config to TRUE, and still not sending mail.
Not change anything in 'ion_auth.php'. file as it is like :
$config['use_ci_email'] = FALSE; // Send Email using the builtin CI email class, if false it will return the code and the identity
$config['email_config'] = array(
'mailtype' => 'html',
);
and then 'Auth.php' which is controller file in change some code which is like :
if ($forgotten)
{
// if there were no errors
// $this->session->set_flashdata('message', $this->ion_auth->messages());
// redirect("auth/login"); //we should display a confirmation page here instead of the login page
$config = [
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html'
];
$data = array(
'identity'=>$forgotten['identity'],
'forgotten_password_code' => $forgotten['forgotten_password_code'],
);
$this->load->library('email');
$this->email->initialize($config);
$this->load->helpers('url');
$this->email->set_newline("\r\n");
$this->email->from('xxx');
$this->email->to("xxx");
$this->email->subject("forgot password");
$body = $this->load->view('auth/email/forgot_password.tpl.php',$data,TRUE);
$this->email->message($body);
if ($this->email->send()) {
$this->session->set_flashdata('success','Email Send sucessfully');
return redirect('auth/login');
}
else {
echo "Email not send .....";
show_error($this->email->print_debugger());
}
}
else
{
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect("auth/forgot_password");
}
I use config in codeigniter
application/config/email.php
$config['protocol'] = 'mail';
$config['wordwrap'] = false;
$config['mailtype'] = 'html';
application/config/autoload.php
$autoload['libraries'] = array('lang','template', 'email','form_validation','session','encrypt','pagination','upload','database' );
In controller
$this->email->from(MAIL,MAIL);
$this->email->to($mailto);
$this->email->subject($text_subject);
$this->email->message($this->load->view('email/template_email',$data,TRUE));
$this->email->send();
I was trying sent email using PHP CodeIgniter framework. after i uploaded that file into my browser it didn't show me any error. it said:"Your email was sent successfully". But i didn't get any email in my email account. I could not figure out what was the problem. I am using CodeIgniter version 2.1.3. can anyone please help me. I am new in PHP. Thank You.
Here is my code:
<?php
class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->library('email');
$this->email->from('hasib32#gmail.com', 'Hasan Hasibul');
$this->email->to('riar32#gmail.com');
$this->email->subject('email test');
$this->email->message('testing the email class. email sent');
if($this->email->send()){
echo"Your email was sent successfully";
}else
{
show_error($this->email->print_debugger());
}
}
}
You can try this:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '...#gmail.com',
'smtp_pass' => '....',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$email_setting = array('mailtype'=>'html');
$this->email->initialize($email_setting);
$email_body ="<div>hello world</div>";
$this->email->from('...#gmail.com', 'shahriar');
$list = array('...#gmail.com');
$this->email->to($list);
$this->email->subject('Testing Email');
$this->email->message($email_body);
$this->email->send();
echo $this->email->print_debugger();
}
this works for me. happy coding :)
Its because you don't have mail server setup in your localhost. You can either setup it or you can use your gmail account to send your mail like this-
$config = Array(
‘protocol’ => ‘smtp’,
‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
‘smtp_port’ => 465,
‘smtp_user’ => ‘myusername#gmail.com’,
‘smtp_pass’ => ‘mypassword’,
);
$this->load->library('email', $config);
$this->email->from('hasib32#gmail.com', 'Hasan Hasibul');
$this->email->to('riar32#gmail.com');
$this->email->subject('email test');
$this->email->message('testing the email class. email sent');
if($this->email->send()){
echo"Your email was sent successfully";
} else {
show_error($this->email->print_debugger());
}
Check e-mail server in the mail queue. probably waiting.