this is my code to send email with gmail but it is not work , I am try hard to make it work for 4 days but still not work.
please help me, it was work before 6 day , after that not work , the server told me to change PHPMailer to SMTP Gmail , I am changed but still not work ...
i am register in Amazon SES Email , but i do not know how to use it, so i have 2 option : make this run in my normal server or change to Amazon SES , if SES is better chance learn me how to use it .
my site to test
my Controller
public function Send_Single_Email_Try ()
{
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'xx#gmail.com',
'smtp_pass' => 'xx',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"
);
$this->load->library('email', $email_config);
$this->email->from('someuser#gmail.com', 'invoice');
$this->email->to('test#test.com');
$this->email->subject('Invoice');
$this->email->message('Test');
$this->email->send();
if($send)
{
echo 1;
}
else
{
echo 0;
}
}
my JS
$(document).on('click','#SendEmailTry',function(e){
e.preventDefault();
$.ajax({
url:"<?php echo
base_url('Email/Send_Single_Email_Try/')?>",
type: "POST",
dataType: "text",
success:function(data)
{
if (data == 1)
{
alert("send");
}
else
{
alert("notsend");
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert("error")
}
});
});
Try this....
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'sender_mailid#gmail.com';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('sender_mailid#gmail.com', 'sender_name');
$this->email->to('recipient#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
check and set your csrf protection from your site config file(yourproject/config/config.php) such as,
$config['csrf_protection'] = FALSE;
your rewrite code:
public function Send_Single_Email_Try ()
{
$this->output->enable_profiler(FALSE);
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'xx#gmail.com',
'smtp_pass' => 'xx',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"
);
$this->load->library('email', $email_config);
$this->email->from('someuser#gmail.com', 'invoice');
$this->email->to('test#test.com');
$this->email->subject('Invoice');
$this->email->message('Test');
$send = $this->email->send();
if($send)
{
echo 1;
}
else
{
echo 0;
}
}
Related
I am using Codeigniter email library to send emails. I have integrated email settings like this in my model
public function savedetails($data) {
$firstmail = $this->sendmail(1, $data);
$secondmail = $this->sendmail(2, $data);
}
public function sendmail($flag, $data) {
$this->load->library('email');
$config = array(
'protocol' => 'smtp',
'smtp_host' => $this->config->item('emailhost'),
'smtp_user' => $this->config->item('emailusername'),
'smtp_pass' => $this->config->item('emailpassword'),
'smtp_port' => 25,
'smtp_crypto' => 'tls',
'mailtype' => 'html'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
if($flag == 1) {
$this->email->from('someone#example.com', 'Someone');
$this->email->to('someone#example.com, 'Someone');
$this->email->subject('This is a test message');
$message = $this->load->view('email/firstmail.php', $data, true);
} else {
$this->email->from('someone#example.com', 'someone');
$this->email->to('someone#example.com', 'someone');
$this->email->subject('This is a test message');
$message = $this->load->view('email/secondmail.php', $data, true);
}
$this->email->message($message);
return $this->email->send();
}
So first email goes successfully but the second email fails everytime. Second email is not going delivered and giving false each time but first email delivered successfully. What is the issue here ? How can i get second email delivered successfully too in Codeigniter ?
I am working on sending the email in loop in codeigniter.
What actually happening is I am receveing the email but it doest not shwing the message which I had included. Following is script:
<?php
Class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myemail',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$this->load->database();
$query = $this->db->query('SELECT email FROM users');
// $this->email->initialize($config);
// $this->email->set_newline('\r\n');
foreach ($query->result() as $row)
{
$this->email->clear();
$this->email->set_newline("\r\n");
$this->load->library('email',$config);
$this->email->from("mymail","myname");
$this->email->to($row->email);
$this->email->subject("THIS IS AN EMAIL TEST");
$this->email->message('Hello, We are <strong>Example Inc.</strong>');
$this->email->set_mailtype("html");
if($this->email->send())
{
echo "Your Mail send";
}
else
{
show_error($this->email->print_debugger());
}
}
echo 'Total Results: ' . $query->num_rows();
}
}
?>
I am expecting Hello, We are <strong>Example Inc.</strong>' this message in email but what I actually getting is
No mesage in email. Where I am making mistake.
Replace your code with this
<?php
Class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'myemail',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$this->load->database();
$query = $this->db->query('SELECT email FROM users');
// $this->email->initialize($config);
// $this->email->set_newline('\r\n');
foreach ($query->result() as $row)
{
$this->email->clear();
$this->email->set_newline("\r\n");
$this->load->library('email',$config);
$message = '<html><body>';
$message .= 'Hello, We are <strong>Example Inc.</strong>';
$this->email->from("mymail","myname");
$this->email->to($row->email);
$this->email->subject("THIS IS AN EMAIL TEST");
$this->email->set_mailtype("html");
$this->email->message($message);
if($this->email->send())
{
echo "Your Mail send";
}
else
{
show_error($this->email->print_debugger());
}
}
echo 'Total Results: ' . $query->num_rows();
}
}
?>
I have a code in controller codeigniter framework as follows :
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
$this->load->library('email');
$this->email->from('example#yahoo.com', 'example');
$this->email->to('example#gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
}
This code correctly sends email . but when that i am config email class setting , this code not work and not send email . for example follow code will not send email :
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
$this->load->library('email');
$this->email->from('example#yahoo.com', 'example');
$this->email->to('example#gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');
$config['protocol'] = 'sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
if ($this->email->send())
echo "Mail Sent!";
else
echo "There is error in sending mail!";
}
}
Please tell me where is the problem .
thanks .
Protocol can be Sendmail, SMTP and mail.
You should load library after configuration:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('example#yahoo.com', 'example');
$this->email->to('example#gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');
if ($this->email->send())
{
echo "Mail Sent!";
}
else
{
echo "There is error in sending mail!";
}
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
my controller
if (isset($_POST['send']))
{
$email = $this->input->post("email");
$reco=$this->ui_model->check($email);
if($reco){
foreach($reco as $row)
if($row['email'])
{ $username=$row['username'];
$email=$row['email'];
$password=$row['password'];
$this->email->from('systron#micronix.com','Systron');
$this->email->to($email);
$this->email->subject('Email Test');
$this->email->message('"Testing the email class.');
$test=$this->email->send();
echo"Request has been sent plz check your Email for password";
$this->load->view('login_view.php');
}
}else
{
echo "Email id is not exist";
$this->load->view('forget.php');
}
my model
function check($email)
{
$this->db->select('*');
$this->db->from('admin');
$this->db->where('email',$email);
$check=$this->db->get();
$res = $check->result_array();
return $res;
}
Try This:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '',
'smtp_pass' => '');
$email=$row['email'];
$msg='your message';
$this->load->library('email', $config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->from('from#gmail.com');
$this->email->to('to#gmail.com');
$this->email->subject('subject');
$this->email->message($msg);
if (!$this->email->send())
{
show_error($this->email->print_debugger());
}