[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Sending Email using PHP

Published on
10,713 Points
3 Endorsements
Last Modified:
Are you going to be sending email in PHP? Don't use the mail() function, unless your site's email needs are very basic and you are running on a Linux/Unix machine. It is light on features and not built in to the Windows version.

The best approach is to use a 3rd party mail program. In the past I have used PHPMailer and I love it. I make many posts with this solution to SMTP email problems in PHP and this is the fix!


Below is the PHP code to use it. Place class.phpmailer.php into your php includes directory.

Replace text in << >> with your server information: http://www.emailaddressmanager.com/tips/mail-settings.html
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host     = "<<smtpserver>>:<<stmpport>>"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "<<uname>>"; // SMTP username
$mail->Password = "<<pword>>"; // SMTP password
$mail->From = "<<email address from>>";
$mail->FromName = "<<email name from>>";
$mail->AddAddress("<<email address to>>"); 
$mail->AddReplyTo("<<email address from>>","<<email name from>>");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject  =  "Subject";
$mail->Body     =  "Body text";
$mail->AltBody  =  $mail->Body;

Open in new window

LVL 40

Expert Comment

by:Richard Quadling
LVL 49

Expert Comment

Can you please clarify:
>>It is ... not built in to the Windows version.

Is this a new 6.X default setting I am not aware of?
LVL 40

Expert Comment

by:Richard Quadling
Mail is a standard function within PHP, for both win32 and non win32 platforms.

As long as you know how to build MIME mail, the builtin mail() function is fine.

As of 5.3.0 it can also produce a log of all mail sent.


But there is a significant difference between the win32 and non win32 implementations as summarised in this note on that above page ...

Note: The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.
As such, the to parameter should not be an address in the form of "Something <someone@example.com>". The mail command may not parse this properly while talking with the MTA.

So, whilst it is not perfect, it does exist.

And having said all that, using a class to coordinate all of this is a LOT easier.

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Expert Comment

SWIFT is one the PHP built in code to access and send the mail using "SMTP Sever". Here you can found library as well as tutorial to implement it.

LVL 15

Expert Comment

May i know what else setting has to be done on the linux server. So as to send the mail.
Does any services like 'sendmail','postfix','qmail' has to be configured.
I want to send the basic mail through my server.
LVL 12

Expert Comment

by:Avinash Zala
i think this link has all the things which you need..

PHP Send Mail using SMTP

Featured Post

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Join & Write a Comment

The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month