php send mail

Hey Technoz! We are now here to see the important and interesting part of php website i.e. email. php is a language which allows to send emails directly from the php scripts. So lets start the tutorial. We’ll first see the html form part. create the file mail.html and put the following code in it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Contact Form Style Demo</title>
<link href="style.css" rel="stylesheet" media="screen"> 
</head>

<body><br/><br/>
<center><h1>Php Send Mail Tutorial</h1></center><br/>
<div id="stylized" class="myform">
<form id="form1" id="form1" action="mail-send.php" method="POST">
    <label>Email<font color="red">*</font>
        <span class="small">Enter user's mail address </span>
    </label>
<input type="text" name="email" required="required">
<br />
<br />
    <label>Message
        <span class="small">Enter Your Message</span>
    </label>
<textarea name="message" rows="6" cols="25"></textarea><br />
    <button type="submit" value="Send" style="margin-top:15px;">Send</button><br/>
	<font color="red"><b>*</b></font> Required field
<div class="spacer"></div>
</form>
</div>
</body>
</html>

We’ve given the good look also to the form. For this, we’ve given the css styling. create file style.css and paste
the following code in it.

body{
font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif; 
font-size:12px;
}
p, h1, form, button{border:0; margin:0; padding:0;}
.spacer{clear:both; height:1px;}
/* ----------- My Form ----------- */
.myform{
	margin:0 auto;
	width:400px;
	padding:14px;
}
	/* ----------- basic ----------- */
	#basic{
		border:solid 2px #DEDEDE;	
	}
	#basic h1 {
		font-size:14px;
		font-weight:bold;
		margin-bottom:8px;
	}
	#basic p{
		font-size:11px;
		color:#666666;
		margin-bottom:20px;
		border-bottom:solid 1px #dedede;
		padding-bottom:10px;
	}
	#basic label{
		display:block;
		font-weight:bold;
		text-align:right;
		width:140px;
		float:left;
	}
	#basic .small{
		color:#666666;
		display:block;
		font-size:11px;
		font-weight:normal;
		text-align:right;
		width:140px;
	}
	#basic input{
		float:left;
		width:200px;
		margin:2px 0 30px 10px;
	}
	#basic button{ 
		clear:both;
		margin-left:150px;
		background:#888888;
		color:#FFFFFF;
		border:solid 1px #666666;
		font-size:11px;
		font-weight:bold;
		padding:4px 6px;
	}


	/* ----------- stylized ----------- */
	#stylized{
		border:solid 2px #b7ddf2;
		background:#ebf4fb;

	}
	#stylized h1 {
		font-size:14px;
		font-weight:bold;
		margin-bottom:8px;
	}
	#stylized p{
		font-size:11px;
		color:#666666;
		margin-bottom:20px;
		border-bottom:solid 1px #b7ddf2;
		padding-bottom:10px;
	}
	#stylized label{
		display:block;
		font-weight:bold;
		text-align:right;
		width:140px;
		float:left;
	}
	#stylized .small{
		color:#666666;
		display:block;
		font-size:11px;
		font-weight:normal;
		text-align:right;
		width:140px;
	}
	#stylized input{
		float:left;
		font-size:12px;
		padding:4px 2px;
		border:solid 1px #aacfe4;
		width:200px;
		margin:2px 0 20px 10px;
	}
	#stylized button{ 
		clear:both;
		margin-left:160px;
		width:125px;
		height:31px;
		background:#444;
		text-align:center;
		line-height:31px;
		color:#FFFFFF;
		font-size:11px;
		font-weight:bold;
	}

Now you can see the form we created as shown in following image.

php send mail tutorial

Looking good. isn’t it? But it is only the front end. Now we have to build the php beck end which will enable the email to be send to the intended address.

Create a file named mail-send.php and write the following code in it.

<?php
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" \n Message: $message\n \nYou have successfully implemented the php mail tutorial.\n Keep Coding! \n\n Regards, \n Technopoints\n https://technopoints.co.in";
$recipient = $email;
$subject = "php mail tutorial";
$mailheader = "From: admin@technopoints.co.in \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<center>Your mail has been sent successfully!</center>" . "<center> -</center>" . "<center><a href='mail.html' style='text-decoration:none;color:#ff0099;'> Return Home</a></center>";
?>

Now run the mail.html file, enter the email address of the recipient and the message to send and click send. The email is sent and we’ll see the following screen.

php send mail tutorial

Also, check your mailbox. You’ll see the like following this.

php send mail tutorial

Hurrey! It’s working fine. If any difficulties, Feel free to use our source code below.

You can also see android tutorial to implement map in android app.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.