How to send Email Using PHP

Sending emails using php on your web-server is not such a big task if you are familiar with basic functions of PHP.

You can write a mail function and provide subject, message as parameter to it.
This mail function is inbuilt and your can use it by using the keyword  main() in your code.

How to use Mail() function in PHP?

Open your PHP file in any editor such as Notepad. If you dont have any existing PHP file, then create new PHP file and start writing the code given below one by one while understanding its use.

Mail to Variable:

$email="email_addressof_the_person_you_want_to_send_mail@domain.com";

The variable "$email" is used to store the address of the person who will recieve the email.

Subject Variable:

$subject="SUBJECT OF THE E-MAIL";

We will use this variable "$subject" to store the value for e-mail. Here I have given the value itself as "SUBJECT OF THE E-MAIL" for simplicity.

Message Variable:

$message="WRITE YOUR MESSAGE HERE";

"$message" variable would hold the value for the message of email. 

Mail Function Inplementation:

mail($email, $subject, $message, "From: <youremailid@domain.com>\r\n");

This is the main function used to send email. It contains 4 parameters here. Now, use these variables and functions in your php file and start sending e-mails from your own server or website.

Below is the complete look of the php sample page:

<?php

$email="receiver@gmail";
 
$subject="Mail function's testing";
 
$message="Testing of mail function on my server was successfull. Thanks";
 
mail($email, $subject, $message, "From: <youremailid@domain.com>\r\n");
 
echo "Mail Sent!";

?>

Try out the above sample script by replacing $email variable's value with your email address and check whether your have received the email or not. 

That's all. If you are getting any error in code above, feel free to comment below.

Comments

Popular posts from this blog

AGRICULTURAL SCIENTIST RECRUITMENT BOARD

CIVIL SERVICES' (I.A.S.) EXAMINATION

HSC Exam Time Table 2013 Arts, Commerce, Science