CodeIgniter email throws error - php

I cannot send mail using CodeIgniter. It displays the following error.
fwrite(): send of 28 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.
This my settings in email.php library
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.mailhostbox.com';
public $smtp_user = 'xxx#xx.in';
public $smtp_pass = 'xxxxxx';
public $smtp_port = 25;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = '';
public $newline = "\r\n";
I could send email till yesterday. But from today morning, it shows this error.
UPDATE :
I have changed my host to gmail , but still not working. The following are the changes I made
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.gmail.com';
public $smtp_user = 'noreply.xxx#gmail.com';
public $smtp_pass = 'xxxxxx';
public $smtp_port = 465;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = 'ssl';
public $newline = "\r\n";
But it shows error as
Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465:465 (Failed to parse address "smtp.gmail.com:465:465")
I changed the port from 465 to 587 as follows
public $smtp_port = 587;
public $smtp_crypto = 'tls';
But I got this error
Message: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
I couldn't understand what is the real issue, because it throws errors in every possible conditions

You need to initialise the config.see here
For Example
function send_email($attributes) {
$this->load->library('email');
$this->email->set_newline("\r\n");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'host';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#smtp.com';
$config['smtp_from_name'] = 'FROM NAME';
$config['smtp_pass'] = 'XXX';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from($config['smtp_user'], $config['smtp_from_name']);
$this->email->to($attributes['to']);
$this->email->cc($attributes['cc']);
$this->email->bcc($attributes['cc']);
$this->email->subject($attributes['subject']);
$this->email->message($attributes['message']);
if($this->email->send()) {
return true;
} else {
return false;
}
}

Related

How to solve SMTP connection problem in CodeIgniter?

I have a mail function. And when I start the service it send e-mail from my array. My array include 60 mail. When start the service it send 10-15 mail and after got error like this.
from: The following SMTP error was encountered:
Unable to send data: QUIT
quit:
The following SMTP error was encountered:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
function sendMail($to,$cc="",$subject,$message,$title="",$attach="",$from="")
{
$CI = & get_instance();
$CI->load->library("email");
$CI->email->clear(TRUE);
$CI->email->set_newline("\r\n");
if($title==""){
$title="q Group";
}
if($from=="") {
$from="q#q.com.tr";
}
$CI->email->from($from, $title);
if($cc<>"")
$CI->email->cc($cc);
$CI->email->subject($subject);
$CI->email->message($message);
if($attach<>"")
{
$CI->email->attach($attach);
}
$mailList = explode(",",$to);
$sendMailList = "";
foreach ($mailList as $key=>$item) {
if (filter_var($item, FILTER_VALIDATE_EMAIL)) {
$sendMailList .=$item.",";
}
else{
MailLogModel::setLog($item,$subject,0,"Mail Adresi Hatalı");
if(count($mailList)==1)
return ["status"=>false,"message"=>"Mail Adresi Hatalı"];
}
}
$sendMail = substr($sendMailList,0,-1);
$CI->email->to($sendMail);
if (! $CI->email->send()){
MailLogModel::setLog($sendMail,$subject,0,html_entity_decode(imap_utf8($CI->email->print_debugger('headers','subject'))));
return ["status"=>false,"message"=>$CI->email->print_debugger(array('headers'))];
}
else{
MailLogModel::setLog($sendMail,$subject,1);
return ["status"=>true];
}
}
My config file is here:
$config['protocol'] = "smtp";
$config['SMTPAuth'] = true;
$config['smtp_host'] = "mail.MYSERVICE.com.tr";
$config['smtp_user'] = "";
$config['smtp_pass'] = "";
$config['smtp_port'] = "25";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = 5;
How can I solve this problem?

unable to send email to valide users using codeigniter email library and google smtp settings

i have this code in my public function inside my controllei am trying to validate users after registration by sending the user a mail.
After registration, the user doest not receive any email.
if ($validation_code)
{
$email_template = $this->template_model->get_email_template(1);
if($email_template['status'] == "1") {
// build the validation URL
$encrypted_email = sha1($this->input->post('email', TRUE));
$validation_url = base_url('user/validate') . "?e={$encrypted_email}&c={$validation_code}";
$site_name = $this->settings->site_name;
$name_user = $this->input->post('first_name') . ' ' . $this->input->post('last_name');
$rawstring = $email_template['message'];
$placeholders = array('[SITE_NAME]', '[CHECK_LINK]', '[NAME]');
$vals_1 = array($site_name, $validation_url, $name_user);
$str_1 = str_replace($placeholders, $vals_1, $rawstring);
$this -> email -> from($this->settings->site_email, $this->settings->site_name);
$this->email->to($this->input->post('email', TRUE));
//$this -> email -> to($user['email']);
$this -> email -> subject($email_template['title']);
$this -> email -> message($str_1);
$this->email->send();
}
No email is received when user is registered. i have tried using different ports.i have also enabled openssl in php.ini. Any help will be appreciated thanks.
the smtp settings
<?php
$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';
//$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'ssl://smtp.gmail.com ';
$config['smtp_user'] = 'xxxxxx';
$config['smtp_pass'] = 'xxxxxx';
$config['smtp_port'] = 465;
$config['smtp_timeout'] = 5;
$config['wordwrap'] = TRUE;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['validate'] = FALSE;
$config['priority'] = 3;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['bcc_batch_mode'] = FALSE;
$config['bcc_batch_size'] = 200;
?>

Cannot send Email in codeigniter

I'm using codeigniter email library to send mail. The following is my email.php library file configuration
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.mailhostbox.com';
public $smtp_user = 'myemailid';
public $smtp_pass = 'mypassword';
public $smtp_port = 25;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = '';
public $wordwrap = TRUE;
public $wrapchars = 76;
public $mailtype = 'html';
public $charset = 'iso-8859-1';
and here is my send email function in controller:
$message = '<div>test</div>';
$this->email->message($message);
$this->email->to(ADMIN_EMAIL);
$this->email->from($email, $userName); // if $email = 'myemailid', then its working
$this->email->subject('SenderID Request');
if($path != '')
$this->email->attach(USER_PROFILE_PATH.$path);
if($this->email->send()){
$result = $this->User_model->senderIDStatus($senderIDID, 3);
if($result == 1) {
$this->session->set_flashdata('Status','success');
$this->session->set_flashdata('Message','SenderID submitted for approval, we will notify you soon');
}
else {
$this->session->set_flashdata('Status','failure');
$this->session->set_flashdata('Message', 'An error occured, please try again');
}
}
else {
print_r($this->email->print_debugger());
}
I can send email if, $this->email->from() is myemailid, which is set in email library. If I tried to send email from another email id it shows an error like
The following SMTP error was encountered: 553 5.7.1 : Sender address rejected: not owned by user myemailid
The mail server is rejecting your email because you're trying to send from a username which differs from the username you're logging in as, e.g. you've set the From: to be no-reply#mydomain.com, which is obviously not the same as myemailid

PHPMailer Failed to connect to server - Settings are correct

I am testing SMTP on my server, using PHPMailer.
SMTP server works fine, on my VBulletin forums, but on my own website engine (from localhost, but port 80 is open, so I am doing it via my ip) I get the following errors:
2014-04-14 12:14:18 SMTP ERROR: Failed to connect to server: SMTP connect() failed.
I asked my mate, whom is the manager of the server, he says that there are no connections that are coming from my IP.
My PHPMailer settings:
public function create() {
$mailer = new PHPMailer();
$mailer->isSMTP();
$mailer->CharSet = "UTF-8";
$mailer->SMTPSecure = "tls";
$mailer->host = Config::$SMTP_HOST;
$mailer->SMTPDebug = 2;
$mailer->SMTPAuth = true;
$mailer->Port = Config::$SMTP_PORT;
$mailer->Username = Config::$SMTP_USER;
$mailer->Password = Config::$SMTP_PASS;
return $mailer;
}
// SMTP details
public static $SMTP_PORT = 25;
public static $SMTP_HOST = "argonite.net";
public static $SMTP_USER = "recover#argonite.net";
public static $SMTP_PASS = "password";
public static $RECOVER_EMAIL = "recover#argonite.net";
And this is how I use it:
$mailer = (new MailFactory())->create();
$mailer->addAddress($email, $username);
$mailer->setFrom(Config::$RECOVER_EMAIL, "Recover");
$mailer->Subject = "Password Recovery - DO NOT REPLY";
$mailer->AltBody = "Argonite has sent you your password as requested.";
$mailer->msgHTML($this->buildMessage($username, $this->getPassword($username, $email)));
if (!$mailer->send()) {
$fh = fopen("logs/.mailer", 'a');
fwrite($fh, "\n[" . date("Y-m-d H:i:s") . "]: " . $mailer->ErrorInfo . "\n");
fclose($fh);
return "An error has occured while sending your request. Administrators have been notified.";
}
else {
return "success";
}
Why is it failing? Could it be something with my PHPMailer settings, version or my Apache/php configurations?
php: 5.5.9
Fixed - I don't really know how but that's the settings I set:
$mailer->SMTPSecure = "none";
$mailer->host = Config::$SMTP_HOST;
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = false;
$mailer->Port = Config::$SMTP_PORT;
$mailer->Username = Config::$SMTP_USER;
$mailer->Password = Config::$SMTP_PASS;
After adding secure = "none" and auth = false, and port 25, it worked.

Send emails from ubuntu virtual machine and use catchmail: Could not execute: smtp://localhost:1025

I am using ubuntu on my virtual machine. I would like to send emails using catchmail as described here: http://berk.es/2011/05/29/mailcatcher-for-drupal-and-other-php-applications-the-simple-version/
I am trying to send emails like that:
//Mailer class:
class Mailer extends PHPMailer
{
public $UTF8Encode = false;
public function __construct($param = null)
{
parent::__construct($param);
$this->Mailer = 'sendmail';
$this->Sendmail = 'smtp://localhost:1025';
$this->From = 'xxxx#xxxx.com';
$this->FromName = 'Support';
$this->WordWrap = 50;
$this->CharSet = 'UTF-8';
}
}
....etc....
And:
//Sending emails
$mail = new Mailer();
$mail->Body = "xxxx";
$mail->Subject = "xxx";
$mail->From = 'xxxx#xxxx.org';
$mail->FromName = 'Support';
$mail->WordWrap = 50;
$mail->AddAddress(xxxx#xxxx.com);
And I am getting the error:
Could not execute: smtp://localhost:1025
$this->Mailer = 'sendmail';
$this->Sendmail = 'smtp://localhost:1025';
The problem with this is you're telling PHPMailer to use a command line program called sendmail instead of using smtp. And PHPMailer tries to do something like:
exec("smtp://localhost:1025 --args-and-stuff");
And as you can tell this won't work.
To tell PHPMailer to use smtp you need to do the following:
$this->Mailer = 'smtp';
$this->Host = 'localhost';
$this->Port = 1025;
If your SMTP server needs Authentification you can do that as follows:
$mail->SMTPAuth = true;
$mail->Username = "yourname#yourdomain";
$mail->Password = "yourpassword";

Categories