fsockopen(): unable to connect to smtp.gmail.com:465 codeigniter live server - codeigniter

This code gives me an error when it is in live server. Its running in localhost but not running in live server. It gives error fsockopen():unable to connect
public function forgetPassword()
{
if(isset($_POST['email']) && !empty($_POST['email']))
{
$result = $this->Db_Function->loadDataForUpdate('wc_usr','wc_email',$_POST['email'],'nor');
if($result)
{
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_user' => 'nileshivarni#gmail.com',
'smtp_pass' => 'Nilesh#1234',
'smtp_port' => 465,
'mailtype' => 'html',
'charset' => 'utf-8',
'newline' => "\r\n"
);
$this->email->initialize($config);
$this->email->from('nileshivarni#gmail.com','Varni IT Solution');
$this->email->to($_POST['email']);
$this->email->subject('Password Recovery');
$msg = '<html><head></head><body>';
$msg .= '<p>Dear ' . $result[0]['wc_fullname']. ',</p>';
$msg .= '<p> <strong >Here is your Email and Password</strong></p>';
$msg .='<p>Email : <strong>'.$result[0]['wc_email'].'</strong></p>';
$msg .='<p>Password : <strong>'.base64_decode($result[0]['wc_password']).'</strong></p>';
$msg .='<p>Thank you</p>';
$msg .='<p>The team at Varni IT Solution</p>';
$msg .='</body></html>' ;
$this->email->message($msg);
if($this->email->send()){
$response['error'] = '1';
$response['success'] = "Your Password is successfully send to your email address !";
echo json_encode($response);
}
}
else
{
$response['error']='2';
$response['errorMsg']='Sorry ! Your Email is not registered with us!';
echo json_encode($response);
}
}

first of all if you are in any live server then it's not necessary to define :
$config = array( 'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_user' => 'nileshivarni#gmail.com',
'smtp_pass' => 'Nilesh#1234',
'smtp_port' => 465);
above values for sending a mail : just define
$config = array(
'mailtype' => 'html',
'charset' => 'utf-8',
'newline' => "\r\n");
That's completed. It's work in live server.

Related

How to send a mail using gmail smtp in codeigniter

I want to send an email from my Gmail account through the website. how do I do that? I've tried every possible way but couldn't get the desired output.
public function mailuser(){
$config = Array(protocol' => 'ssmtp','smtp_host' => 'ssl://ssmtp.gmail.com','smtp_port' => 465,'smtp_user' => 'xxxx#gmail.com','smtp_pass' => 'xxxx','mailtype' => 'html');
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxxx#gmail.com', 'Manoj');
$this->email->to('xxxxx#gmail.com');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
if($this->email->send()){
echo 'Success email Sent';
echo $this->email->print_debugger();
}
else{
echo 'Email Failed To Send';
echo $this->email->print_debugger();
}
}
I think you are using wrong parameters in config like ssmtp
protocol' => 'ssmtp','smtp_host' => 'ssl://ssmtp.gmail.com'
Your config array should look like :
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxx#gmail.com',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$this->email->from('xxxx#gmail.com', 'Manoj');
$this->email->to('xxxxx#gmail.com');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
$result = $this->email->send();
Also verify to enable extension extension=php_openssl.dll in php.ini.
if not please remove ;

error during sending email with codeigniter?

Email is not sent by codeigniter.please check and identify the erros.
MY controller code is:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'bilalshabbir3210#gmail.com', // change it to yours
'smtp_pass' => 'my pasword', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = '<h4>Email comessss</h4>';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('bilalshabbir3210#gmail.com'); // change it to yours
$this->email->to('bilalshabbir0909#gmail.com');// change it to yours
$this->email->subject('Resume from JobsBuddy for your Job posting');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
And error comes:
[]
enter image description here
The error makes it appear that your code cannot contact the SMTP server you've told it to use. A quick google implies that the server should be smtp.gmail.com.
Please use the following settings, this work very well with google
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 465,
'smtp_crypto' => 'ssl',
'smtp_timeout' => '30',
'smtp_user' => 'your_mail#gmail.com', // change it to yours
'smtp_pass' => 'your_gmail_password', // change it to yours
'charset' => 'iso-8859-1',
'crlf' => "\r\n",
'newline' => "\r\n",
'mailtype' => 'html',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('your_mail#gmail.com'); // change it to yours
$list = array('destination_list#gmail.com'); // change it to yours
$this->email->to($list);
$this->email->subject('This is an email test');
$message = '<h4>Email comessss</h4>';
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
} else {
show_error($this->email->print_debugger());
}

Zend mail - cannot read - connection closed?

Using the zend mail php package to connect to Gmail and getting an error
My Method looks like
//Get all mail
public function GetMail() {
$mail = new Imap([
'host' => 'imap.gmail.com',
'ssl' => 'tls',
'port' => '993',
'user' => 'example#gmail.com',
'password' => '*********',
]);
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
printf("Mail from '%s': %s\n", $message->from, $message->subject);
}
}
The error is as follows:
cannot read - connection closed?
Using Laravel as a framework
So i fixed using
$mail = new Imap(array(
'host' => 'imap.gmail.com',
'user' => 'noreply#gmail.com',
'password' => '*********',
'ssl' => 'ssl',
'port' => 993
));
$mail->countMessages();
$content = array();
foreach ($mail as $messageNum => $message) {
array_push($content, ['from' => $message->from,'subject' => $message->subject, 'id' => $message->messageId]);
}
return $content;

Send email by codeigniter

I am using codeigniter to send email.
function forget_pw() {
$this->load->library('email');
$config['smtp_host'] = 'host address';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
$this->email->initialize($config);
$this->email->from('admin#email.com', 'Admin');
$this->email->to('test#yahoo.com.hk');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
}
No any error show in console. But the email didn't send out.
You can create a function like this:
public function sendEmail($subject,$message)
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'xxxxx.xxxxxx.xxxxx',
'smtp_port' => 465,
'smtp_user' => 'xxxxx.xxxxxx.xxxxx',
'smtp_pass' => 'xxxxx.xxxxxx.xxxxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxxxx.xxxxxx.xxxxx');
$this->email->to('xxxxx.xxxxxx.xxxxx');
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
}
And use it like this $this->sendEmail('Title', 'Message'); Good luck

sending mail using codeigniter

This is my send email function
public function send_requs_mail()
{
$this->load->library('email');
$config=array(
'protocol' => 'smtp',
'smtp_host' =>'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxxxxx#gmail.com',
'smtp_pass' => 'yyyyyy'
);
$this->email->set_newline("\r\n");
$this->email->initialize($config);
$this->email->from($this->input->post('from'), 'iftekhar');
$this->email->to($this->input->post('to'));
$this->email->subject('Email Test');
$this->email->message($this->input->post('message'));
if($this->email->send())
{
echo'your email was sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
Now when I press Send button I get successful message I mean I get message "your email was sent" but in email Inbox I didn't get any message.
check if $this->input->post('from') and $this->input->post('to') return valid email addresses, otherwise it will not send the email!
use this $config Array
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 465,
'smtp_crypto' => 'ssl',
'smtp_user' => 'your mail',
'smtp_pass' => 'your password',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
And to install SSL
First see if there is open-ssl extension in your php.ini file if not then put the below code in the ini file in extension part.
extension=php_openssl.dll
Restart the Apache service.

Resources