user2576961
user2576961

Reputation: 415

Why isn't the Codeigniter email class sending mail with sendmail?

I'm attempting to send an email address out to registrants who register for my site however this section of my code is not running. I'm attempting to find out why. I've followed all the necessary documentation. I replaced this section with php's mail function for debugging to see if it'll send an email on my server and it will. Does anybody have any thoughts on what it could be?

// User was successfully created and the user needs to verify their account.
// Send registered an email informing them how to validate their account.
$this->load->library('email');
$this->email->from('[email protected]', 'Owner Name');
$this->email->to($post_email_address);
$this->email->subject('Site Name Here Registration');
$this->email->message('Thank you for registering for our site. Here is your registration key to activate your account: '.$registration_key.' Please click on the following link to activate your account.<br />'.anchor('project-manager/login/verify/'.$registration_key, 'Click Here To Activate Your Account', ''));

$this->email->send();

Here is the configurations for the email.com inside of the application/config directory.

<?php 
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

Upvotes: 1

Views: 361

Answers (1)

user50049
user50049

Reputation:

I know that it seems unnecessary, but I typically always use SMTP with the mail class. There are just too many server configuration issues that can result in mail not being sent. Sometimes anonymous (e.g. 'nobody' or 'www-data') users are restricted from using sendmail - a very common problem if the server isn't properly configured with suexec.

Additionally, using SMTP relies on known-good DNS / SPF settings of a known-working mail server - might not be the case on individual app servers that you use.

You could probably get to the bottom of the issue with a bit more digging, and possibly even get it corrected - but then you've got the same issue all over again if you need to move your app, or bring up more servers.

Just follow the email class documentation to use SMTP, which is just really a matter of changing the $config['protocol'] accordingly, then add the SMTP host / user / pass variables. Set up an email account, and give it a go. Mail forwarding settings can handle things like noreply a bit more gracefully as well.

To try it, just override the $config array in your controller, e.g.:

$myTempConfig['protocol']  = 'smtp'; 
$myTempConfig['smtp_host'] = 'mail.somedomain.com';
$myTempConfig['smtp_user'] = 'username';
$myTempConfig['smtp_pass'] = 'swordfish';
$myTempConfig['smtp_port'] = 25;
$myTempConfig['smtp_timeout'] = 30; // adjust as needed for busy mail servers

then call:

 $this->email->initialize($myTempConfig)

so it loads the settings. You could just call it $config if you want, I just like making it clear what I'm doing when I do that.

It's really just better to rid yourself of the problem and be done with it once and for all. Once you have SMTP setup, your code just works, no matter where you put it.

Most decent hosting utilizes suexec correctly which means the mail class will 'just work' using sendmail. However, if you tend to deploy a lot of stuff with platform-as-a-service providers, or your own servers, it just makes better sense to use SMTP and save some headache and work.

Upvotes: 3

Related Questions