Skip to content
Advertisement

Add ‘From’ Header on email using Codeigniter

I’m working with Codeigniter in CPanel and my code already sends a mail, but when it gets to the receiver, the hostname is shown on the sender. I tried some answer to questions as : Change the sender name php mail instead of sitename@hostname.com but in Codeigniter, they don’t work.

This is my code:

 $config = Array(
                'protocol' => 'ssmtp',
                'smtp_host' => 'ssl://ssmtp.googlemail.com',
                'smtp_port' => 465,
                'smtp_user' => 'mail@domainiwant.com', 
                'smtp_pass' => 'password',            
                'mailtype'  => 'html',
                'charset'   => 'iso-8859-1',
                'useragent' => 'MY NAME',
            );
            $this->load->library('email', $config); 
            $this->email->set_newline("rn");
            $this->email->from('mail@domainiwant.com', 'MY NAME');

            $email_to = 'receiver@gmail.com';
            $this->email->to($email_to);

            $this->email->message('Message testing ...');
            $this->email->send();

However, as I said, when the mail gets to the receiver, they appear with the hostname and a completely different mail address like the one I put on $config

I know this only sets the envelope sender but I want to set the mail address to be mail@domainiwant.com instead of receiving the mail with somemail@host.com.ex

Advertisement

Answer

According to the documentation of CodeIgniter’s email library available HERE, your whole problem is a simple typo.

$config['protocol'] allows mail, sendmail and smtp as values. If you don’t set the variable, or use a value which is not allowed, the whole library defaults to mail which attempts to use your own server as the mail gateway (which explains why your sender address shows as username@servername)

Change the protocol from ssmtp to smtp so that you actually use the Google SMTP server you intend to use and you’ll get the results you expect

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement