sending verification email via SMTP using gmail SMTP server failed in codeigniter - php

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());
}

Related

Unable to send email in live server in codeingiter

Error screentshot Hello guyz i am doing sending mail function in codeingniter, Same email function working in localhost but it did not work in live server i cant understand this error please help me..
This is my Email code:
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'email', // change it to yours
'smtp_pass' => 'password', // 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>Dear: ".$firstname."</p>
<p>Please click the link below to activate your account.</p>
<h4><a href='".base_url()."welcome/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','Register Successfull And your Activation link send in Email Please Verify Your Account');
}
else{
$this->session->set_flashdata('message', $this->email->print_debugger());
}
ERROR:
Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
The error you are showing is not related to your code rather its authentication error mean google servers are not allowing you to sign in.
Now in order to run this code, make sure you are using the right username and password.
After that, you have to turn on "Allow less secure apps" option from your Google/Gmail account.
After that, your code should be able to send an email.
Also, When you have uploaded your script into web host then you should use the email address of your domain name, like support#yourDomain.com it looks more professional.
Given below code is fully working code. Before you run it you must create an email address like admin#yourDomain.com from your hosting panel where there will be an option of email management.
$this->load->library('email');
$encodeEmail = bin2hex('email#gmail.com']);
$this->email->from('admin#yourDomain.com', 'Your Domain');
$this->email->to('email#gmail.com');
$this->email->subject('Email Verification Required');
$url = site_url() . 'verify/' . $data['verification'] . '/' . $encodeEmail;
$message = "";
$message .= "You have signed up with our website \r\n";
$message .= "Please click on given below link to verify and activitate your account. \r\n" . $url;
$this->email->message($message);
$this->email->send();

How to get the response of bounced mail when working with google mail smtp (smtp.googlemail.com)

I am working on making newsletters. I have bulk of 1200 emails but it's not verified. I am using google mail smtp for verification in codeigniter framework. Mail is properly sending but when I tested it for wrong email then also it shows send mail status. I want to ask that how to get the response of bounced mail when working with google mail smtp.
$check is containing array of emails :
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'mymail#gmail.com',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'send_multipart' => FALSE
);
$this->email->initialize($config);
$this->load->library('email', $config);
foreach($_POST['chk'] as $check)
{
$this->email->clear();
$data = array(
'userName'=> 'Myname'
);
$this->email->set_newline("\r\n");
$this->load->library('email',$config);
$this->email->from("mymail#gmail.com","Myname");
//echo $row->email;
$this->email->to($check);
$this->email->subject("THIS IS AN EMAIL TEST");
$gett = $this->load->view('home_test.php',$data,TRUE);
$this->email->message($gett);
$this->email->set_mailtype("html");
if($this->email->send())
{
echo "Your Mail send";
}
else
{
show_error($this->email->print_debugger());
exit();
}
}
Its always showing Your Mail send although if I provide fake email id. How to track this bounced mail in PHP.
You will not get status of the bounced emails immediately after sent. What you can do is, specify return path email as third parameter of $this->email->from("mymail#gmail.com","Myname");.
Change this line
$this->email->from("mymail#gmail.com","Myname");
to
$this->email->from("mymail#gmail.com","Myname", 'yourreturnemail#gmail.com');
For more details see codigniter's email documentation here
This way all bounced emails will come to the return path email address. Then you can create a cron or something to read and parse this emails. php's imap extension can be installed and use for read email bounced status.

sent email verification link with php condeigniter

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());
}

cannot send email using CodeIgniter

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.

Reply-to - Send emails in codeigniter with gmail smtp

I created a contact form in my website, and, for send the e-mails to website account (contact#mywebsite.com), I'm using gmail smtp, using the same e-mail that I'll receive the messages.
So, users go to my website, click in contact page, fill the form with:
Name, Email, Message.
Than I send the email with the following code:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'contact#mywebsite.com',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$email = $this->input->post('email');
$name = $this->input->post('name');
$msg = $this->input->post('msg');
$this->email->to('contact#mywebsite.com');
$this->email->reply_to($email); //User email submited in form
$this->email->from($email, $name);
$this->email->subject('Conctact form');
$this->email->message($msg);
if ($this->email->send())
{
return true;
} else
{
echo $this->email->print_debugger();
return false;
}
The e-mail goes to my inbox in "contact#mywwebsite.com", I can read the message normally, but... when I click in "reply" instead of replying to the user that sent me the message, it replys to "me" (to contact#mywebsite.com).
I already configured the reply_to, to reply to users address, not mine, but still goes to contact#mywebsite.com.
How to fix this?
Should I change something more in the code or in gmail settings?
(PS.: I use gmail interface to read emails, directly from the site mail.google.com)
Thanks, in advance.
--
Also, when I receive the e-mail, It shows:
"From: 'Name in submitted form' "
Not:
"From: 'Name in submitted form' <'email in submitted form'>"
Like It should be.
Gmail does this, and I don't believe there is any sort of work around.
The "from" address will only ever be the account you are using to send the email - you cannot simply "pass through" gmail's server.
If you need to do that, you'll need something like SendGrid or your own smtp server
I fixed By sending the e-mail through another user ...
I sent from me#mywebsite.com to contact#mywebsite.com, and when I clicked in reply, the user selected was the same that sent the message in form.
GMail appearing to ignore Reply-To

Categories