Sending Emails in PHP

Using an SMTP Server:

SMTP is an acronym for Simple Mail Transfer Protocol, and an SMTP server is a machine that transports mail, just like a Web server is a machine that displays Web pages when requested. An SMTP server is sometimes referred to as an outgoing mail server, which brings me to the point—you need one in order to complete the exercises in this chapter. On Linux/UNIX, Sendmail and Qmail are popular packages. On Windows, the SMTP service in the Windows NT Service Pack, or the service built into the Windows 2000 operating system, is often used.
However, if you have installed Apache, PHP, and MySQL as part of a development environment on your personal machine, you probably do not have SMTP running locally. If that’s the case, you can access an outgoing mail server that might already be available to you.

A Simple Feedback Form:

A simple feedback form usually contains fields for the respondent’s name and e- mail address and a text area for some sort of message. In this section, you’ll create two files: one for the feedback form, and one for the PHP script to process the form, send the mail, and return a response to the browser.

Creating the Feedback Form:

<HTML>
<HEAD>
<TITLE>Simple Feedback Form</TITLE>
</HEAD>
<BODY>
<FORM METHOD=”POST” ACTION=”send_simpleform.php”>
<P><strong>Your Name:</strong><br>
<INPUT type=”text” NAME=”sender_name” SIZE=30></P>
<P><strong>Your E-Mail Address:</strong><br>
<INPUT type=”text” NAME=”sender_email” SIZE=30></P>
<P><strong>Message:</strong><br>
<TEXTAREA NAME=”message” COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></P>
<P><INPUT TYPE=”submit” NAME=”submit” VALUE=”Send This Form”></P>
</FORM>
</BODY>
</HTML>

Save the file with the name simple_form.html, and place this file in the document root of your Web server.

Creating a Script to Mail Your Form:
<?
if (($_POST[sender_name] == “”) ||
($_POST[sender_email] == “”) ||
($_POST[message] == “”)) {
header(“Location: simple_form.html”);
exit;
}
$msg = “E-MAIL SENT FROM WWW SITEn”;
$msg .= “Sender’s Name:t$_POST[sender_name]n”;

$msg .= “Sender’s E-Mail:t$_POST[sender_email]n”;
$msg .= “Message:t$_POST[message]n”;
$to = “[email protected]”;
$subject = “Web Site Feedback”;
$mailheaders = “From: My Web Site <genericaddress[email protected]>n”;
$mailheaders .= “Reply-To: $_POST[sender_email]n”;
mail($to, $subject, $msg, $mailheaders);
?>
<HTML>
<HEAD>
<TITLE>Simple Feedback Form Sent</TITLE>
</HEAD>
<BODY>
<H1>The following e-mail has been sent:</H1>
<P><strong>Your Name:</strong><br>
<? echo “$_POST[sender_name]”; ?>
<P><strong>Your E-Mail Address:</strong><br>
<? echo “$_POST[sender_email]”; ?>
<P><strong>Message:</strong><br>
<? echo “$_POST[message]”; ?>
</BODY>
</HTML>

Save the file with the name send_simpleform.php and place this file in the document root of your Web server.

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Leave a Reply

Your email address will not be published. Required fields are marked *