sending email with php
hari wrote:can any body guide me of sending email with php?
in PHP is the field names for the information you are gathering. "From" will be converted into a variable with the same name by php (it would become $from), the "subject" will be made into $subject and the "contents" of the message will be made into $contents. For submitting the actual form, we will activate the php script by using the form action tag and linking to send_mail.php3:
Code:<form action="send_email.php3" method="POST">
The $to variable will be defined in the send_mail.php3 script to point to my email address (you'll see the php code momentarily), of course. I also prefill the subject with the words "diary entry suggestion" which visitors can change to something else, if they want:
Code:<input type="text" size="22" name="subject" value="diary entry suggestion">
Ok, let's take a look at the code to mail the contents of the above form to me and if the process is successful it will redirect you right back to this page. In order for this code to work you will need to know the path to sendmail on your server.
Code:<?
$to = "webmaster@php-scripts.com";
$from_header = "From: $from";
if($contents != "")
{
//send mail - $subject & $contents come from surfer input
mail($to, $subject, $contents, $from_header);
// redirect back to url visitor came from
header("Location: $HTTP_REFERER");
}
else
{
print("<HTML><BODY>Error, no comments were submitted!");
print("</BODY></HTML>");
}
?>
Notes: I make sure there are some comments submitted or else show an error message by using the != (not equal) with an if statement. When you want to redirect the browser using the header function (like when using the setcookie function) you must do it before any HTML. With the header and $HTTP_REFERER in the code above I am simply sending people who submit the form back where they came from, which should will be this page that calls the form. I could have easily changed the header line to read:
Code:header("Location: thankyou.html");
And at a page named thankyou.html I could have thanked them for submitting the form.