Wednesday, April 21, 2010

Sending emails through CURL

HTML Form:
<form action="sendmail_contactus.php" method="post" id="form">
<div class="wrapper">
<div class="col-1">
<p>Enter Your Name:</p>
<div class="form1">
<input type="text" name="Name" />
</div>
<p>Enter Your E-mail:</p>
<div class="form1">
<input type="text" name="EmailFrom" />
</div>
<p>Enter Your State:</p>
<div class="form1">
<input type="text" name="State" />
</div>
</div>
<div class="col-2">
<p>Enter Your Message:</p>
<div class="form2">
<textarea cols="1" rows="1" name="Message"></textarea>
</div>
<p class="alignright"><a href="#" class="link" onclick="document.getElementById('form').reset()">clear</a>    
<a href="#" class="link" onclick="document.getElementById('form').submit()">submit</a></p>
</div>
</div>
</form>


CURL Code:
<?php
// the target url which contains scripts that accepts post request
$url = "http://abc.net/abc/contactus.php";

// user agent to mock else defaulted to (HTTPRetriever/1.0)
$useragent="YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";

$ch = curl_init();

// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// set the target url
curl_setopt($ch, CURLOPT_URL,$url);

// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// the parameter 'username' with its value 'johndoe'
curl_setopt($ch, CURLOPT_POSTFIELDS,$_POST);


// execute curl,fetch the result and close curl connection
$result= curl_exec ($ch);


/*if(curl_exec($ch) === false)
{
//echo 'Curl error: ' . curl_error($ch);
}
else
{
// echo 'Operation completed without any errors';
}*/

curl_close ($ch);

// display result
//print $result;
header("location:contactus-thanks.html");

?>


Email Script:
if( ($_POST["Name"] != '') && ($_POST["EmailFrom"] != '') && ($_POST["State"] != '') && ($_POST["Message"] != '') )
{

$to = "test@gmail.com";
$from = $_POST["EmailFrom"];

$subject = "Contact Us";

$message = "<B><font color='#CC0000'>Contactus Details</font></B><BR><BR><B><font color='#655112'>Name:</font></B> ".$_POST["Name"]." <BR><BR><B><font color='#655112'>Email From:</font></B>".$_POST["EmailFrom"]."<BR><BR><B><font color='#655112'>State:</font></B> ".$_POST["State"]."<BR><BR><B><font color='#655112'>Message:</font></B> ".$_POST["Message"]."<BR><BR>";


$headers = 'MIME-Version: 1.0'. "\n";
$headers.='Content-type: text/html; charset=iso-8859-1'."\n";
$headers .= "From: $from". "\n";

mail($to,$subject,$message,$headers);
}

No comments:

Post a Comment