Codeigniter - Sending emails using different SMTP settings - php

I have a system that uses different emails for different parts of the system. I've got my config setup with a No-Reply email. However, if I try and send an email using different SMTP settings, it will try and use the Email config settings rather than the array settings I've specified.
These are the settings that are in my Email config file
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'REMOVED';
$config['smtp_pass'] = 'REMOVED';
$config['smtp_port'] = '587';
$config['mailtype'] = 'html';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = '120';
$config['smtp_crypto'] = 'tls';
Below is the list of settings I have specified in the array (These are the settings that I want to send this particular email through Gmail rather than Office365)
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'REMOVED';
$config['smtp_pass'] = 'REMOVED';
$config['smtp_port'] = '587';
$config['mailtype'] = 'html';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = '120';
$config['smtp_crypto'] = 'tls';
Is there any way to send emails via different SMTP settings rather than the ones specified in the Email config file? I have tried to send this email via the Gmail settings but it always appears to try and send through the Email config settings.
SOLUTION:
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'REMOVED';
$config['smtp_pass'] = 'REMOVED';
$config['smtp_port'] = '587';
$config['mailtype'] = 'html';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = '120';
$config['smtp_crypto'] = 'tls';
$this->email->initialize($config);
Fixed my issue and now I am able to send an email through an Ad-Hoc Gmail account rather than the Office365 one.

You can override the configuration in the config/Email.php (office365 in your case) file with a specific one (gmail, in your case) by building the $config array in the controller and then use it to initialize the email library with:
$this->email->initialize($config);
For this to work you must initialize the library using the overriding $config before actually doing anything else that requires the class, or the default settings will be used automatically

Related

Error Sending mail in code igniter

I'm trying to send an email with CodeIgniter, but this is what I get:
The following SMTP error was encountered: 110 Connection timed out
Unable to send email using PHP SMTP. Your server might not configured to send mail using this method.
Here is my code in email.php:
<?php
$CI =& get_instance();
if ($CI->config->item('installed') == true) {
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // Or "/usr/sbin/sendmail"
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$config['charset'] = strtoupper(get_option('smtp_email_charset'));
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$config['protocol'] = 'smtp';
$config['smtp_host'] = get_option('smtp_host');
$config['smtp_port'] = get_option('smtp_port');
$config['smtp_timeout'] = '30';
$config['smtp_user'] = get_option('smtp_email');
$config['smtp_pass'] = get_option('smtp_password');
// Set to "tls" if you need to use TLS
$config['smtp_crypto'] = '';
}

configuring codeigniter smtp on godaddy shared server

I have tried the code below and keep getting this error
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.keytimeng.com'; //change this
$config['smtp_port'] = '25';
$config['smtp_user'] = 'info#keytimeng.com'; //change this
$config['smtp_pass'] = 'xxxx'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$this->email->initialize($config);

Codeigniter emailer not working

$this->load->library('email');
// set email data
$this->email->from($this->input->post('sender_email'), $this->input->post('sender_name'));
$this -> email -> set_mailtype("html");
$this->email->to('');
$this->email->reply_to($this->input->post('sender_email'), $this->input->post('sender_name'));
$this->email->subject('From: '.$this->input->post('sender_name'));
$this->email->message("<table><tr><td> </td><td>".$this->input->post('sender_name')."</td></tr><tr><td> </td><td>".$this->input->post('sender_email')."</td></tr><tr><td> </td><td>".$this->input->post('phone')."</td></tr><tr><td>Message: </td><td>".$this->input->post('message')."</td></tr></table>");
$this->email->send();
// create a view named "succes_view" and load it at the end
redirect('contact?email_succ=1');
exit();
This is the code I'm using to send email from a form in CI and it works only when I use SMTP login along with 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'] = 'myemail#example.com';
$config['smtp_pass'] = 'emailpassword';
$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);
Having this code right after loading the library solves the issue. But what's wrong with the default code that has been working all this time but suddenly stopped?
UPDATE: adding just this line $this->email->initialize($config); solves the issue, But why?.
Try the following code.
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "smtp";
$config['smtp_host'] = "localhost";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->message = "Your Message";
$this->email->subject("Message Subject");
$this->email->from("user#gmail.com");
$this->email->to("admin#gmail.com");
$this->email->send();

How send to email using codeigniter

$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.tch-pnt.in"; //
$config['smtp_port'] = 587;
$config['smtp_user'] = "bnft#tch-pnt.in";
$config['smtp_pass'] = "********";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
Error...
I am using above configuration setup for sending email...
No error but i am not getting mails in my inbox.I tried 3 times.
I used default port as 25 still no mails
I think you should try
$config['smtp_host'] = "sslv2://smtp.tch-pnt.in";
or try port number 465.

CodeIgniter + MAMP : unable to send emails

I am trying to send emails with CI running on MAMP free. But it doesn't work, my script is encountering an infinite loop and nothing is happening...
Do i need to set up something especially to send emails from localhost?
Here is my email config for CI:
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = '*****#gmail.com';
$config['smtp_pass'] = '******';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = '\r\n';
Cheers
EDIT: Here is my code to send an email:
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = '****';
$config['smtp_pass'] = '*****';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = '\r\n';
$this->email->initialize($config);
$this->email->from('****');
$this->email->to($email);
$this->email->subject($title);
$this->email->message($content);
$this->email->send();
error_log($this->email->print_debugger());
With $title, $content and $email vars defined in another part of my script. Don't worry about this stuff, I've already checked that my issue is not due to these.
Finally I found the solution:
Send emails with MAMP (mail() PHP function)
Set up SSL: http://soundsplausible.com/2012/01/14/enable-ssl-in-mamp-2-0-5/
Set up Postfix: http://benjaminrojas.net/configuring-postfix-to-send-mail-from-mac-os-x-mountain-lion/
Into MAMP's php.ini (glance phpinfo() to know which version is used and conclude into which folder you need to edit), comment lines SMTP, smtp_port and sendmail_from. Uncomment line sendmail_path and set /usr/sbin/sendmail -t -i as new value.
You should be able to send emails now, if PostFix works fine (run test given in tutorial above).
Send emails with CI
To send emails with CI, you don't need to write your logins into a file for PostFix. However, you need to be able to run PostFix and SSL.
Here is an example of config file for a google account:
$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.gmail.com";
$config['smtp_port'] = "587";
$config['smtp_user'] = "*****";
$config['smtp_pass'] = "*****";
$config['smtp_crypto'] = "tls"; //very important line, don't remove it
$config['smtp_timeout'] = "5"; //google hint
$config['mailtype'] = "text";
$config['charset'] = "utf-8";
$config['newline'] = "\r\n";
Be careful with " " which are necessary, ' ' could create issues. Here I'm using a TLS connection. If you prefer SSL, use port 465 and fix correctly smtp_crypto value.
You need use sendmail(details how install it on MAMP). Or you can use solution below to store emails in localhost(something like emulate email sending).
I'm use this solution on my localhost(XAMPP). Maybe it will be helpful for you.
Specify path for send mail in php.ini
sendmail_path = "path/to/php path/to/sendmail.php"
The second step - you can try to use this script
define('DIR','path/to/sendmail_dir');
$stream = '';
$fp = fopen('php://stdin','r');
while($t = fread($fp,2048)){
if($t === chr(0)){
break;
}
$stream .= $t;
}
fclose($fp);
$fp = fopen(mkname(),'w');
fwrite($fp,$stream);
fclose($fp);
function mkname($i=0){
$fn = DIR.date('Y-m-d_H-i-s_').$i.'.eml';
if (file_exists($fn)){
return mkname(++$i);
}
else{
return $fn;
}
}

Categories