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("
");
$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' => "
",
'newline' => "
",
'mailtype' => 'html',
));
To Send Verification Link In Email Using Email Template is simpler than adding HTML content as a message and send it.
Here is the code sample:
$this->email->set_newline("
");
$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());
}