i have a library in codeigniter which autoloads
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Send_email {
private $CI;
public $from_email;
public $from_name;
public $reply_to;
public $to_email;
public $subject;
public $message;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('email');
}
public function send_email($f_email,$f_name,$t_email,$rep_to,$sub,$msg){
$this->from_email = $f_email;
$this->from_name = $f_name;
$this->to_email = $t_email;
$this->reply_to = $rep_to;
$this->subject = $sub;
$this->message = $msg;
$this->CI->email->from($this->from_email, $this->from_name);
$this->CI->email->to($this->to_email);
$this->CI->email->reply_to($this->reply_to);
$this->CI->email->subject($this->subject);
$this->CI->email->message($this->message);
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "****#gmail.com";
$config['smtp_pass'] = "****";
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->CI->email->initialize($config);
if($this->CI->email->send()){
return TRUE;
}else{
return $this->CI->email->print_debugger();
}
}
}
?>
and while calling it from controller
$this->send_email->send_email(......)
After calling this method i am getting this error
Cannot send mail with no "From" header.How can i solve this problem?
Check $this->from_email value coming properly or not
echo $this->email->print_debugger();
which will return header, subject & body content
Related
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?
I am trying to send mass emails via Codeigniter email library. However, only the first few emails are sent successfully.
Here is my Mail_model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mail_model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->library('email');
}
public function send_mail($email, $firstname, $data){
$subject = $data['subject'];
$message = 'Dear '.$firstname.',<br /><br />'.$data['message'].'<br />'.DOMAIN_NAME;
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_timeout'] = 5;
$config['smtp_host'] = SMTP_HOST; //smtp host name
$config['smtp_port'] = SMTP_PORT; //smtp port number
$config['smtp_user'] = FROM_EMAIL;
$config['smtp_pass'] = FROM_PASSWORD; //$from_email password
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$config['validation'] = TRUE;
$this->email->initialize($config);
//send mail
$this->email->from(FROM_EMAIL, DOMAIN_NAME);
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
//echo json_encode($this->email->print_debugger());
$this->email->send();
}//end method send_mail
}//end class
Here is the bit of code from my controller that I am calling send_mail function from the model
$customers = $this->customer_model->get_customers();
foreach($customers as $customer){
$this->mail_model->send_mail($customer['email'], $customer['firstname'], $this->input->post());
sleep(10);
}
I added sleep(10) at the end of the loop to see if everything will work fine but it does not help. I am using Gmail.
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;
}
}
/*send email Using at Index.php page*/
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
//include 'classes/phpmailer/mail.php';
//function Send_Mail($to,$subject,$body)
$mail = new Mail();
//$mail->setFrom(SITEEMAIL);
//$mail->Debugoutput = 'html';
$mail->addAddress($to);
//$mail->setFrom('noreply#domain.com', 'noreply');
//$mail->addReplyTo('replyto#example.com', 'First Last');
$mail->subject($subject);
//$mail->IsHTML(true);
$mail->body($body);
$mail->send();
/*PHP Mailer Code used in mail.php*/
<?php
include('class.phpmailer.php');
class Mail extends PhpMailer
//function Send_Mail($subject,$body)
{
// Set default variables for all new objects
public $From = 'rssbmonthlyreport#gmail.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'isSMTP';
public $SMTPAuth = true;
public $Username = 'abc#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $Port = 587;
public $Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
public $CharSet = 'UTF-8';
public $Encoding = '8bit';
public $ContentType = 'text/html; charset=utf-8\r\n';
//public $From = 'rssbmonthlyreport#gmail.com';
//public $FromName = 'GMail Test';
public $WordWrap = 75;
public function subject($subject)
{
return $this->Subject = $subject;
}
public function body($body)
{
return $this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
I am Using these code above at Index page and this in email.php and email.php was included in config.But it is not working in place of this default mail() function running.....I tried every thing even the latest code of phpmailer its crashing..Anyone has idea so please help
This is wrong:
$Mailer = 'isSMTP';
It should be:
$Mailer = 'SMTP';
A better approach would be to call $this->isSMTP() from an overridden constructor.
I'd also recommend using the built-in html2text method if you're going to forcibly set AltBody like that.
If you're going to override things, read the code.
Here's an improved version:
class Mail extends PhpMailer
//function Send_Mail($subject,$body)
{
// Set default variables for all new objects
public $From = 'rssbmonthlyreport#gmail.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $SMTPAuth = true;
public $Username = 'abc#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $Port = 587;
public $Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
public $CharSet = 'UTF-8';
public $Encoding = '8bit';
//public $From = 'rssbmonthlyreport#gmail.com';
//public $FromName = 'GMail Test';
public $WordWrap = 75;
public function __construct($exceptions = null)
{
parent::__construct($exceptions);
$this->isSMTP();
$this->isHTML();
}
public function subject($subject)
{
return $this->Subject = $subject;
}
public function body($body)
{
return $this->Body = $body;
}
public function send()
{
$this->AltBody = $this->html2text($this->Body);
return parent::send();
}
}
}
which email you send
ex: gmail or hotmail
if ssl not present
hotmail automatically deletes email without even notifying. You have add exception for particular sender.
I am using this code to send email via localhost.
function send_email(){
date_default_timezone_set('Asia/Manila');
$ci =& get_instance();
$ci->load->library('email');
$config['protocol'] = "mail";
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "myemail#smsgt.com";
$config['smtp_pass'] = "";
$config['charset'] = "utf-8";
$config['mailtype'] = "text";
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('sender#email.com', 'company email');
$ci->email->to('receiver#gmail.com');
$ci->email->subject('test subject');
$ci->email->message("testing email");
$check = $ci->email->send();
//echo $ci->email->print_debugger();
if ($check){
echo "true";
}else{
echo "false";
}
}
It always shows true but still I cant received it. What is problem with my codes? or is there something wrong?
**Your smtp and port is not working**
$config['smtp_host'] = "ssl://mail.smsgt.com";
$config['smtp_port'] = "25";
**Change into this**
$config['smtp_host'] = "ssl://smtp.mail.yahoo.com;ssl://smtp.gmail.com";
$config['smtp_port'] = 465;