Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Difference between PHP mail function and using third party library

I have a little confusion about using PHP inbuilt mail() function and using Third Party Library. If we use mail() function then which email address and password it is using to send the mail. I have never seen that we specify email id and password in mail function UNLIKE third party library

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Bhaskar,

One of the biggest factors is 3rd party libraries such as PHPMailer offer built in security that rip out known attacks from the headers which are very common and often missed by new comers, other features include.

  • True SMTP support, PHP offers SMTP but not on the same level and you need to edit the php.ini file
  • Easy functions to send HTML emails, you can do this in vanilla code but it requires you to set the headers yourself
  • The ability to add and manage multiple recipients using array's
  • Intuitive functionality for adding attachments to emails, in vanilla code this can be a tad annoying if you have more than one attachment

and lots lots more.

Personally I always choose the 3rd party library because it offers much richer functionality and eliminates a lot of the headaches you need to deal with but I generally use it for medium to large scale sites.

Aaron Graham
Aaron Graham
18,033 Points

In *nix environments, mail() uses the system's configured MTA, i.e. Sendmail, Postfix, etc., by default. If you plan on using mail(), make sure that your local MTA is configured first.

I happen to support Chris Upjohn in his opinion though. This is probably something that is best left to a third party library. Some things are far to critical, and far too error prone, to try and build yourself. My big three are mail clients, input validators, and auth layers. Don't get me wrong, I have built a few, and I strongly recommend other people build some of these things on their own too, but only as a personal project and never to be used in production. It can be very instructional. When you really get into the nuts and bolts of how these things work you learn a lot about the language you are using, and you begin to see all the pitfalls that quickly begin to pop up. For at least the three things I mentioned (you might even find that there are others), find yourself a good third party library that has been properly vetted by a team of people that know something about security.