I have heard that all of these methods are valid ways to send email in PHP.
What are their advantages and disadvantages?
Sendmail is an application that includes SMTP functionality and configurations, but SMTP is the protocol used to send email messages. Cloud hosts offer a commercial application named Sendmail that can be used to send email via an application on a Windows or Linux server.
If you are on a Unix-like system, then PHP's "default mailer" is sendmail, so you don't actually have to change anything if that's all you need. For more details, you can see the relevant php. ini configuration options in the documentation (http://us3.php.net/manual/en/mail.configuration.php).
When you use the PHP mail function, you are sending email directly from your web server. This can cause issues if the FROM address isn't set properly or if your email isn't hosted with DreamHost. Sending mail via SMTP is recommended as email is sent from the mail server rather than the web server.
The three options aren't quite in the same league, presuming the first means the mail()
function.
Using the mail()
function usually calls the local mail injector, commonly a binary program supplied by the MTA actually called "sendmail". The problem with mail()
is that it is a non-straightforward interface with a number of gotchas and traps which are not well documented. This is because it mimics (IMO badly) the call to the Unix CLI mail
command.
It is possible to call the local injector yourself, but this documented even less well. You may as well call mail()
, anyway, since this is what the latter does.
Using SMTP comes with its own set of problems, however. If there is a local MTA that accepts and forwards mail, then it is not a bad solution. If there isn't, you will have to figure out which host you should be sending to. This will either be figuring out which external host should be doing your forwarding, or doing the actual MX lookup yourself. You will also need to know the SMTP protocol and be able to handle refusals for any reason. And you have to decide how you handle the need to re-try the send.
Doing the SMTP yourself also has the problem of not de-coupling the email sending from the reason the email is being sent. If there is a delay, or a problem, you have a page that will appear to be stuck. Using the local injector hands the former problem to the MTA; all you've done is queue the email for delivery. But then you don't have to worry about things like re-sending.
These three solutions also do not help you assemble your message, such as rich content, alternate content and attachments. You have to do all that (and add the correct headers!) yourself.
The normal recommendation is to locate a library that does all that for you, is robust and has a decent API. The usual one is PHP Mailer. The advantages of this library is that it also does all the message assembly, as well as figuring out how to do the delivery. But it's main win is that it Just Send The Message, trying mail()
and local SMTP and even remote SMTP if it must. All transparantly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With