Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't I use PHP's mail() function?

Tags:

php

email

The general opinion when it comes to sending email messages in PHP is to stay clear of PHP's built-in mail() function and to use a library instead.

What I want to know are the actual reasons and flaws in using mail() over a library or extension. For example, the commonly specified headers that aren't included in a standard mail() call.

like image 570
Ross Avatar asked Dec 30 '10 18:12

Ross


People also ask

Does PHP mail function work?

Unless you mean delivering mail to the webmaster, it doesn't make sense for the webmaster to configure things. You're asking the wrong question - there is nothing wrong with the mail() function in PHP - it's not unreliable. The problem is contents and distribution of your mails.

Why it is advantages to use PHPMailer for sending and receiving email?

It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS. It can send an alternative plain-text version of email for non-HTML email clients.

Can we send email through PHP script?

PHP (Hypertext Preprocessor) is an easier programming language used for faster development. The PHP mail() function allows sending emails directly from a script.


2 Answers

Quoting:

Disadvantages of the PHP mail() function

In some cases, mails send via PHP mail() did not receive the recipients although it was send by WB without any error message. The most common reasons for that issue are listed below.

  • wrong format of mail header or content (e.g. differences in line break between Windows/Unix)
  • sendmail not installed or configured on your server (php.ini)
  • the mail provider of the recipeint does not allow mails send by PHP mail(); common spam protection

Errors in the format of header or content can cause that mails are treated as SPAM. In the best case, such mails are transfered to the spam folder of your recipient inbox or send back to the sender. In the worst case, such mails are deleted without any comment. If sendmail is not installed or not configured, no mails can be send at all.

It is common practice by free mail provider such as GMX, to reject mails send via the PHP function mail(). Very often such mails are deleted without any information of the recipient.

like image 70
moinudin Avatar answered Sep 25 '22 10:09

moinudin


PHP's mail() is said to garble headers and runs slowly. I can't say this from personal experience because I've never used it, because, like you, I've always been advised against it. If you look at the comments on the entry for mail() in the PHP manual, you can see some of the problems people have with it (esp. with headers).

It's definitely not suited for sending any substantial amount of email, because, according to the manual itself,

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

AFAIK, it's never preferable (performance-wise) to open and close a socket for each message you send regardless of the amount of mail you're sending.

Basically, it's a function that works, but not very well, and is eclipsed by a number of better libraries.

like image 24
Rafe Kettler Avatar answered Sep 24 '22 10:09

Rafe Kettler