简体   繁体   中英

Codeigniter - Sending emails using different SMTP settings

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM