PHP Code for sending Emails


Posted on : Fri, 17th Apr 15 11:25 am (Updated on : Fri, 17th Apr 15 11:25 am) UTC
Author : Code2care
100+ votes

The below code sends a email through PHP code,

File : sendEmail.html Language : Html
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Sending Email using PHP : Code2care Examples</title>
</head>

<body>

<form action="EmailSender.php" method="post">

To : <input name="to" type="text" />

<br/><br/>
Subject : <input name="subject" type="text" />

<br/><br/>
From : <input name="from" type="text" />

<br/><br/>
Body : <textarea name="body" rows="25" cols="100"></textarea>

<input type="submit" value="submit"> 
</form>

</body>
</html>
Advertisements



Below is the PHP file that is called when the form is submitted,

File : EmailSender.php Language : PHP
<?php

$mailTo = $_POST['to']."\r\n";
$mailSubject = $_POST['subject']."\r\n";
$mailBody = $_POST['body']."\r\n";
$mailHeaders = "From: ".$_POST['from']."\r\n";
$mailHeaders .= "Reply-To: [email protected] \r\n";
$mailHeaders .= "Return-Path: [email protected] \r\n";
$mailHeaders .= "X-Mailer: PHP5 \r\n";
$mailHeaders .= 'MIME-Version: 1.0' . "\r\n";
$mailHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

//Send Email Function
mail($mailTo,$mailSubject,$mailBody,$mailHeaders);


?>

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +