Codeigniter SMTP Email with Amazon SES - codeigniter

I think yesterday Amazon announced SMTP support for SES (Simple Email Service).
I tried to send SMTP email with Codeigniter with no luck.
I have a verified sender and everything looks good:
$this->load->library('email');
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'email-smtp.us-east-1.amazonaws.com',
'smtp_user' => 'SMTP USERNAME',
'smtp_pass' => 'SMTP PASSWORD',
'smtp_port' => 465,
'mailtype' => 'html'
);
$this->email->initialize($config);
$this->email->print_debugger();
$this->email->from('verified_email_address#something.com', 'Test From');
$this->email->to('email#example.com', 'Test To');
$this->email->subject('Test');
$this->email->message('test');
$this->email->send();
I tried the folowing smtp_host:
email-smtp.us-east-1.amazonaws.com
tls://email-smtp.us-east-1.amazonaws.com
ssl://email-smtp.us-east-1.amazonaws.com
When i echo the print_debugger() i get:
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-194655181
hello: 421 Timeout waiting for data from client.
These tests run on a mediatemple (gs) server.

I got that timeout message until I added the line:-
$this->email->set_newline("\r\n");
I have my host set as ssl://email-smtp.us-east-1.amazonaws.com

You need to do 3 things to get CI to work with Amazon Simple Email Service (SES)
Need to set newline = \r\n or you will get a timeout.
Need to set smtp_crypto to something. (New requirement)
Need to make sure "from" email address is approved in Amazon SES. I made my "from" email address "no-reply#mydomain.com"
Additionally, you should set up DKIM for your "from" email address to prevent emails from getting put in spam folders. This involves going into Amazon SES -> Identity Management -> Email Addresses -> DKIM, hitting the enable button, and adding 3 DNS entries to your website's DNS.
No need to do anything special to set up SPF. The envelope domain amazonses.com passes SPF.
Finally, make sure to use "reply-to" if you want users to be able to reply to an e-mail address different from your approved "from" e-mail address.
Example working code:
$obj = &get_instance();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'email-smtp.us-west-2.amazonaws.com';
$config['smtp_user'] = 'USER';
$config['smtp_pass'] = 'PASS';
$config['smtp_port'] = '587';
$config['newline'] = "\r\n";
$config['smtp_crypto'] = 'tls';
$obj->email->initialize($config);
$obj->email->set_mailtype('html');
// don't html_escape email header variables
$obj->email->from(MV_FROM_EMAIL, $from_name);
$obj->email->reply_to($from_email, $from_name);
$obj->email->to($to);
$obj->email->subject($subject);
$obj->email->message($obj->load->view($path, html_escape($data), true));
$obj->email->send();

public function enviar_email($para, $assunto, $mensagem, $formato='html'){
$this->CI->load->library('email');
$config['mailtype'] = $formato;
$config['useragent'] = 'Post Title';
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'tls://email-smtp.us-east-1.amazonaws.com';
$config['smtp_user'] = 'smtpuser';
$config['smtp_pass'] = 'smtppass';
$config['smtp_port'] = '465';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$this->CI->email->initialize($config);
$this->CI->email->from('Your Verified Sender Email', 'Post Title');
$this->CI->email->to($para);
$this->CI->email->subject($assunto);
$this->CI->email->message($mensagem);
if($this->CI->email->send()):
return TRUE;
else:
$this->CI->email->print_debugger();
endif;
}

I also needed to add the line
$config['smtp_crypto'] = 'tls';
to my config array
this is supported by CI 2.1.0 and greater

The setup that worked for me looks like this:
$test_config['protocol'] = 'smtp';
$test_config['smtp_host'] = 'ssl://email-smtp.us-east-1.amazonaws.com';
$test_config['smtp_port'] = '465';
$test_config['smtp_user'] = 'XXXXXXXXXXXXXXXXXXX';
$test_config['smtp_pass'] = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY';
$test_config['newline'] = "\r\n";
$this->email->initialize($test_config);
$this->email->from('from#test.com', 'From at Test.com');
$this->email->to('To#Test.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
The newline character must be set to "\r\n", and can bet set in the config file, if properly set as "\r\n", not '\r\n' as noted above.

Related

Email Attachment not working in codeigniter

Mail function with attachment file is not working in codeigniter, i m not receiving any mail with attachment.. before i received the mail without attachment but after i used "$_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;", i didnt receive any mail.. kindly help me.. Here my coding is
$image_name = "some_name";
$content = "some content";
$subject = "Subject Name";
$this->load->helper('email');
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from('abc#gmail.com');
$this->email->to('xxx#gmail.com');
$this->email->subject($subject);
$this->email->message($content);
$attched_file = $_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;
$this->email->attach($attched_file);
$this->email->send();
You should use config parameter for email library, and set 'mailtype' to 'html'(Default Value is 'text'). If it still fails, you should use the print_debugger() function to see why.
$config = Array(
'mailtype' => 'html'
);
$image_name = "some_name";
$content = "some content";
$subject = "Subject Name";
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('abc#gmail.com');
$this->email->to('xxx#gmail.com');
$this->email->subject($subject);
$this->email->message($content);
$attched_file = $_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;
$this->email->attach($attched_file);
if($this->email->send()){ echo 'Email send.';}
else {show_error($this->email->print_debugger()); }

how to send email using codeigniter

today i have sending email using code igniter but not work properly .so please any one help me.
this is my controller page code here;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {enter code here
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
// $this->load->library('pagination');
$this->load->library('session','form_validation');
}
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'siva#gmail.com', // here goes your mail
'smtp_pass' => 'mygamilpassword', // here goes your mail password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$message = 'hiiiiii';
$this->load->library('email', $config);
$this->email->set_newline("rn");
$this->email->from('siva#gmail.com'); // here goes your mail
$this->email->to('sam#gmail.com');// here goes your mail
$this->email->subject('Resume from JobsBuddy');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
} $this->sendMail;
}
and in my localhost open php.extension->php.openssl enable and also the port number change for 465, and smtp:smtp.gmail.com this are all enable it.
but not working help me.?
Try this code. This is the working code:
function sendMail()
{
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.googlemail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "siva#gmail.com";
$config['smtp_pass'] = "mygamilpassword";
$message = "Your email body text goes here";
$config['mailtype'] = "html";
$ci = & get_instance();
$ci->load->library('email', $config);
$ci->email->set_newline("\r\n");
$ci->email->from("siva#gmail.com");
$ci->email->to("siva#gmail.com");
$ci->email->subject("Resume from JobsBuddy");
$ci->email->message($message);
$ci->email->send();
echo $this->email->print_debugger();
}
Try this code. This is the working code and allow-less-secure-apps-access-gmail-account setting open
public function send_mail() {
ini_set('SMTP', "server.com");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "yourmail#gmail.com");
$this->load->library('email');
$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'] = 'yourmail#gmail.com';
$config['smtp_pass'] = 'yourpassword';
$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->set_newline("\r\n");
$from_email = "frommailid#gmail.com";
$to_email = "tomailid#gmail.com";
//Load email library
$this->load->library('email');
$this->email->from($from_email, 'Identification');
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library');
//Send mail
if($this->email->send())
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
else
show_error($this->email->print_debugger());
$this->session->set_flashdata("email_sent","You have encountered an error");
$this->load->view('contact_email_form');
}

Can't send email from gmail in codeigniter, SMTP Authentication error

I'm trying to send an email in codeigniter, but something wrong and i dont know how to fix it.
It success when I send in localhost, but when I upload my app into host, it cant send. This is my code
$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'] = 'username';
$config['smtp_pass'] = '****';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'html'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->send();
echo $this->email->print_debugger();
And this is my error when I submit an email
Failed to authenticate password. Error: 535 Incorrect authentication data
*from: 250 OK
to: 550-Please turn on SMTP Authentication in your mail client, or login to the
550-IMAP/POP3 server before sending your message. mb2d247.vdrs.net
550-(hanghieunara.com) [112.78.2.247]:51939 is not permitted to relay through
550 this server without authentication.*
The following SMTP error was encountered: 550-Please turn on SMTP Authentication in your mail client, or login to the 550-IMAP/POP3 server before sending your message.
mb2d247.vdrs.net 550-(hanghieunara.com) [112.78.2.247]:51939 is not permitted to relay through 550 this server without authentication.
See if your email account is not active with difinições security for external access, if you need to grant access to your site's SMTP server (your email address) so you can perform authentication.
To send smtp gmail autenticated with codeigniter
1 - Be sure if openssl in your php instalation is enabled.
2- This command is fundamental to script works: $this->email->set_newline("\r\n");
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'gmail.login#googlemail.com',
'smtp_pass' => 'your_password',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('gmail.login#googlemail.com', 'Your Name');
$this->email->to('recipient#destination.tld');
$this->email->subject(' CodeIgniter Rocks Socks ');
$this->email->message('Hello World');
if (!$this->email->send())
show_error($this->email->print_debugger());
else
echo 'Your e-mail has been sent!';
Full solution link: https://ellislab.com/forums/viewthread/84689/

Email sent in localhost but not in server

When I try to send emails from localhost, everything works fine. But when I uploaded my app to the server, the email functionality doesn't work anymore! I uses a Gmail account.
Here is my config/email.php file:
$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'myemail#gmail.com';
$config['smtp_pass'] = 'my_password';
$config['smtp_port'] = 465;
$config['smtp_timeout'] = 80;
$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;
$config['sender_name'] = 'Name';
$config['from_email'] = 'myemail#gmail.com';
$config['to_email'] = 'myemail#gmail.com';
$config['email_subject'] = 'Email Subject';
And this is how I sent an email in my controller:
$this->load->library('email');
$this->email->from($this->config->item('from_email'), $this->config->item('sender_name'));
$this->email->to($this->config->item('to_email'));
$this->email->subject($this->config->item('email_subject'));
$this->email->message($string);
if (!$this->email->send())
{
$data['success'] = FALSE;
}
else
{
$data['success'] = TRUE;
}
$this->_example_output('layouts/sendemail_confirm.php', $data);
And this is the error I receive:
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to
ssl://smtp.googlemail.com:465 (Connection refused)
Filename: libraries/Email.php
Line Number: 1689
A PHP Error was encountered
Severity: Warning
Message: fwrite() expects parameter 1 to be resource, boolean given
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fgets() expects parameter 1 to be resource, boolean given
Filename: libraries/Email.php
Line Number: 1869
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection refused)
It's possible that your server's IP address is blacklisted by Google due to previous spamming attempts... Is it a shared hosting account? A cloud hosting account? Etc? All those are susseptable to stuff like this.
There might also be a reverse DNS lookup mismatch with the domain name and from address, or the domain's DNS might have SPF policy settings that are preventing Google from accepting mail, or an envelop issue with the host-name, or it's using 127.0.0.1 as the originating address.
Also check your php.ini settings to make sure you are using / or not-using sendmail_from= and mail.add_x_header= the same way on both systems.
Try using smtp host as "smtp.gmail.com"
$config['smtp_host'] = 'smtp.gmail.com';
Use this config options
$config['protocol'] = 'smtp';
$config['mail_path'] = 'ssl://smtp.googlemail.com';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
And add this line, after loading email library
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$config = Array(
'protocol' => 'mail',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'iskpro.it#gmail.com', // change it to yours
'smtp_pass' => 'afycon#123', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($from); // change it to yours
$this->email->to($to);// change it to yours
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();

How to send an email with content from a View in codeigniter

I want to send an email to user from my application with the content of the email loaded from a view . This is the code i've tried out till now:
$toemail = "user#email.id";
$subject = "Mail Subject is here";
$mesg = $this->load->view('template/email');
$this->load->library('email');
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();
You need to call $this->load->library('email'); within the controller as well for the email in CI to work.
Also , in your code : $fromemail is not initialized.
You need to have SMTP support on your server.
$config should be declared as an array before assigning values and keys.
Working Code:
$this->load->library('email');
$fromemail="ad#c.com";
$toemail = "user#email.id";
$subject = "Mail Subject is here";
$data=array();
// $mesg = $this->load->view('template/email',$data,true);
// or
$mesg = $this->load->view('template/email','',true);
$config=array(
'charset'=>'utf-8',
'wordwrap'=> TRUE,
'mailtype' => 'html'
);
$this->email->initialize($config);
$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();
Edit:
$mesg = $this->load->view('template/email',true); should be having the true as pointed out by lycanian. By setting it as true , it doesn't send data to the output stream but it will return as a string.
Edit:
$this->load->view(); need a second parameter with data or empty like $mesg = $this->load->view(view,data,true);, if not it wont work
This line $mesg = $this->load->view('template/email',true); should be like this
$mesg = $this->load->view('template/email','',true);
with the single quotes before the value true, and it will work perfectly
Email template send In codeigniter we need to put and meta tag before sending email
$this->data['data'] = $data;
$message = $this->load->view('emailer/create-account', $this->data, TRUE);
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from($email, $name);
$this->email->to('emailaddres#mail.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
U will try it!! it's working for mine after many errors facing
$subject = 'New message.';
$config = Array(
'protocol' => 'sendmail',
'smtp_host' => 'Your smtp host',
'smtp_port' => 465,
'smtp_user' => 'webmail',
'smtp_pass' => 'webmail pass',
'smtp_timeout' => '4',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from('from mail address', 'Company name ');
$data = array(
'message'=> $this->input->post('message')
);
$this->email->to($toEmail);
$this->email->subject($subject);
$body = $this->load->view('email/sendmail.php',$data,TRUE);
$this->email->message($body);
$this->email->send();

Resources