Sending Email With the PHP Mail Function

When you use the PHP mail() function, you end up sending email directly from your web server rather than your mail server. If you have a mail server through your web host, or even a mail server with a different host, it’s usually better to send mail through that instead. The problem is that the PHP mail() function doesn’t provide any built-in way to send mail via SMTP. If you want to open up that functionality, you’ll need to install an additional PHP class. Here are some options that work:

PHPmailerSwiftMailerZend_MailXpertMailerPEAR Mail

We’ll show you how to use PEAR Mail, but you can use any class that supports SMTP. If your web host already has one or more of these classes installed, it probably has tutorials pertaining to your situation. If so, go ahead and use the class that you have access to.

How to Use PEAR to Send Mail Via SMTP

Example PEAR Mail PHP Script For SMTP Mail

You can create your own script from scratch if you like, or modify the following example to your liking. Make sure to enter your web mail server name in the host variable, and use your login information for your web mail host in the username and password fields. " . $mail->getMessage() . " Message successfully sent!

Example PEAR Mail PHP Script For SMTP Authentication and SSL Encryption

If you want to use SMTP authentication and SSL encryption, you’ll have to make a few modifications to the previous example. You’ll need to point the host variable to your SSL mail server, and also specify a port number like 25, 465, 587, 2525 or 8025. Contact your web mail host for more information if you can’t figure out which port to use. " . $mail->getMessage() . " Message successfully sent!