How to send mail from a PHP script.

PHP makes web development easy and dynamic.
Sending mail from PHP is also one of the benefits of using PHP.

Continue reading to find how to send emails using PHP script.

The mail() function in PHP to send mail.

The mail() function is the function using which you can send mail using a PHP script.

so the basic syntax for the function mail is:
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

And the description of parameters are:

to: Specifies the receiver of the mail.

subject: Specifies the subject of message sent.

message: this is the message sent in the email.

additional_headers: this is the additional header data like from , Cc and Bcc. This is optional and my not be used in the function.

additional_parameters: this is used to specify some commands to the program that is used to send email. This is also an optional data and may not be used in function.

one simple script that can be used to send email using PHP is:


<?php
// The subject
$sub = "subject"

// The message
$message = "Line 1\nLine 2\nLine 3";

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

// Send
mail('reciever@domain.com', $sub, $message);
?>

A little bit more complex code to send email using PHP code can be:


<?php
$to = 'reciever1@abc.com' . ', '; // you should use this comma
$to .= 'reciever2@abc.com';

// subject
$subject = 'The subject of mail';

// message
$message = 'Place some message here.';

// Additional headers
$headers .= 'To: First person. <reciever1@abc.com>, 2'nd person.<reciever2@abc.com>' . "\r\n";
$headers .= 'From: sender's name <sender@abc.com>' . "\r\n";

// Mail Function.
mail($to, $subject, $message, $headers);
?>

So sending mail suing PHP is as easy as that.

So enjoy and develop your own PHP script which can send emails to peoples.

For some instance if the function mail() does not work for you or you need to send a large volume of emails then, In that case you need to send email from a PHP script using SMTP authentication. Google that for more info.



Popular Searches:



 

About the author

More posts by admin | Visit the site of admin

 

2 Comments

  1. Siddharth says:

    Good .. i use this :)
    Thanks for the share !!

Leave a Comment

 

You must be logged in to post a comment.