Wednesday, 9 April 2014

Sending an email in php

sending email in php
To send the email in php using mail function. Its not going to work in out local-server because we don't have an support to done the email on my local server. When you may find an as well you can set this out with we had most paid hosting you will have access to able to send a email by using mail function in php. Let see example to send an basic email.

mail function in php
Screenshot1

Code to sending an email in php


<?php

$to = 'roxsmohammed@gmail.com';

$subject = 'This is an email.';

$body = 'This is a test email\n\nHope You got it.';

$headers = 'From: someone@gmail.com';



if (mail($to, $subject, $body, $headers)) {

echo 'Email has been sent to '.$to;

} else {

echo ' There was an error sending the email.';

}



?>

Output

Firstly we had created php tags now the mail function basically looks like this mail(); and its takes 5 parameters and 1 is optional in total. Now we need to create few variables in order to put inside mail function in php. First one is $to you can take name of the variable any here in this example i am taking $to which will be the email address where we want to send an email to so, in this case i will sent to roxsmohammed@gmail.com. This is my personal bussiness email address. $subject variable is going to be the subject of the email address. this is an email.Now we had variable $body to write an email body. and $headers variable in this example i will only work with basic headers we are just going to use From field we going to say From: someone@gmail.com this is the name which will displayed or an email address from where is email had come. If i am sending mail someone after they register on my website after the register process is compeleted i can sent it from noreply@gmail.com or i can send it from admin@gmail.com. Now to send an email i will use mail function an pass this arguments to actully send the email in php. So, the first one is specify is $to, is recipient's email address then you need to specify $subject of the email. So, our $subject is This is an email then you need to specify the $body so, our $body is equal to this is a test email\n\n Hope you got it. \n will take us to new line . Now we give $headers in $headers there are plenty of information we can pack into $headers but now its simple mail function tutorials i am just giving an idea how it works i am just saying from someone@gmail.com so this is name that will be display when we pickup the email.


Now We will Check whether mail function works so i will enclose mail function into if statement and say if mail then do something here so , i am going to comment inside Email has been sent. then i can say else there was an error sending the email. Just in case mail function failed for one or more reasons we can process there was an error sending the email otherwise email as been send we can say email has been sent to '.$to; This is an sending an email in php.


2 comments:

  1. Not Working and not getting any mail from sender

    ReplyDelete
    Replies
    1. Hello Mr, SOM

      This Script Will Only Run on Paid Hosting Please try in Hosting server.

      Delete

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates