Mikey Baptista
Mikey Baptista

Reputation: 33

The difference between CodeIngiter Email Protocol sendmail,mail and SMTP?

I´m currently doing a presentation on CodeIngiter Email library vs External. I dont understand the difference between the several email configuration protocols except SMTP. There is an avaible question but seems confusing and I appreciate a simpler aproach pointing the clear differences.

Also I can´t find in CodeIgniter manual anything about it, simply states that exists.

Upvotes: 1

Views: 1358

Answers (1)

Indigo
Indigo

Reputation: 853

I think there is a confusion here.

SMTP, Simple Mail Transfer Protocol, is really the underlying protocol used for email. Every existing solutions, libraries or services are based on this.

CodeIgniter gives us a useful library class Mail, which is basically an object-oriented wrapper aiming to simplify the sending of mail from your PHP application. For instance, it allows you to do this:

$this->load->library('email');

$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

This is fairly straightforward and nice to read.

Now, this class can be configured in a lot of different way. And I agree that the documentation does a poor job explaining it. And I guess the thing that may have confused you is this sentence: Multiple Protocols: Mail, Sendmail, and SMTP, which in the configuration table is given as:

protocol: mail, sendmail, or smtp The mail sending protocol.

It would be used like this:

$config = ['protocol' => 'sendmail'];
$this->email->initialize($config);

What does these value means?


mail

The default value is mail.

It means that the CodeIgniter library will use the internal mail() PHP function to try to send the mail.

How does it works? How does PHP know how to send the mail?

On Unix/Linux it invokes the sendmail binary, which then uses the mail configuration to route the email. On Windows, it sends to a SMTP server. In both cases the sysadmin sets up the mail system.

Thanks to this SO answer

In any case, the sendmail binary will then use a SMTP server to send the mail, as configured by the admin.

sendmail

The second possible value is sendmail.

Using sendmail value for the configuration means that the CodeIgniter library will use directly the sendmail binary without using the PHP mail() function.

The path to the binary can be configured through the option mailpath (which is /usr/sbin/sendmail by default).

This means that this can only be used on Linux/Unix platform, as Windows doesn't have any sendmail binary.

Now why would you want to use the sendmail binary directly since the PHP internal mail() function already uses it (and is compatible with Windows)?

Well, for one the mail() internal function could be disabled in your PHP environment by your hosting provider. Or you may want to call a special sendmail binary different than the one used by the PHP internal function.

In any case, the sendmail binary will then use a SMTP server to send the mail, as configured by the admin.

SMTP

The last possible value is smtp.

Using smtp value for the configuration means that the CodeIgniter library will connect directly to a SMTP server in order to send the mail.

The way the connection is performed can be configured with the relevant smtp_* options, which are smtp_host, smtp_user, smtp_pass, smtp_port and so on...

This option is really useful when you are not the admin of the server (e.g. in a shared hosting environment) and thus can't configure the SMTP provider for the server.

It is also better to choose this rather than the other alternative, because your application will no longer depends on the server proper configuration.


Summary

The main issue here is that the class and the documentation wrongly use the term protocol.

SMTP is the protocol for email.

The option mail, sendmail and smtp are more like endpoints, or sending methods, i.e. what the library should use in order to send the mail.

I hope this clarifies the documentation a little bit.

Upvotes: 7

Related Questions