PHP mail()

Hi all,

after a long time spending some time on writing a note, a tech note in fb… this is specially for some one with php knowledge.

Yaa, these days I;m working on php. In the holy ramadhan I spend my time in writing a web component for people to show the prayer times and an google chrome component to show the same thing in facebook wall.

After a 3 weeks gap, I started developing an email client in php for an idea I got while I was in hispital. My idea id forbidden but I would like to share something about php-email with u.

All I’m shareing because, I had no one to ask help in these topics, except ALLAH, I just google these things and finished my works withthe help pf ALLAH, the almighty. alhamdu lillah ūüôā

PHP mail()

PHP is always powerful, it has almost all the things in web 2.0 and it comes with mail() which is available in both PHP 4.x and 5.x. This function allows us to send email directly form a script.

mail ( string $to , string $subject , string $message);

this is the basic code to send an email


mail('', 'My Subject', 'This is the message');

Actually this is a mail() is a boolean return-type function, which sets TRUE if the mail is sent else it returns FALSE. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

This mail() in manual comes like this

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

the additional_parameters parameter added in php 4.0.5 and the all custom headers like From, Cc, Bcc and Date are supported from php 4.3.0 but only in windows.

A simple example to send mail

// The message

$message = "Line 1nLine 2nLine 3";


// In case any of our lines are larger than 70 characters, we should use wordwrap()

$message = wordwrap($message, 70);


// Send

mail('', 'My Subject', 'This is the message');


here, we have a 3 line message and if our lines are larger then 70 chars, we opted to  use wordwrap(). We can use this or not and use for some more chars also. The final line is the thing.

Send Email from a PHP Script Using SMTP Authentication

To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:

  • Make sure the¬†PEAR Mail package¬†is installed.
    • Typically, in particular with PHP 4 or later, this will have already been done for you. Just give it a try.
  • Adapt the example below for your needs. Make sure you change the following variables at least:
    • from: the email address from which you want the message to be sent.
    • to: the recipient’s email address and name.
    • host: your outgoing SMTP server name.
    • username: the SMTP user name (typically the same as the user name used to retrieve mail).
    • password: the password for SMTP authentication

Sending Mail from PHP Using SMTP Authentication – Example

require_once "Mail.php";

$from = " ";

$to = "";

$subject = "Hi!";

$body = "Hi,nnHow are you?";

$host = "";

$username = "smtp_username";

$password = "smtp_password";

$headers = array ('From' => $from,   'To' => $to,   'Subject' => $subject);

$smtp = Mail::factory('smtp',   array ('host' => $host,     'auth' => true,     'username' => $username,     'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail))


echo("" . $mail->getMessage() . "");  




echo("Message successfully sent!");  



These are the basic things of php mail, I wanted to share. I hope this will help some very beginners a lot.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s