Web hosting blog


+ Updates | follow the latest releases of applications

+ Promotions | find our periodical offers and discounts

+ Articles | get advice and tips from our professionals

form2mail – sending mail using a PHP script

This example will take the information from a feedback form, send it to you in an email message, then forward the customer to a “thank you for your comments” page.

A simple feedback form:

First of all we need to create a feedback form that will receive the data. We will call this form feedback.html. In its most basic form, it can look like this:

<form method=’post’ action=’sendmail.php’>
Email address: <input name=’email’ type=’text’ /><br />
Name: <input name=’name’ type=’text’ /><br />
Message:<br />
<textarea name=’message’ rows=’15′ cols=’40′>
</textarea><br />
<input type=’submit’ />
</form>

This is just a smaple form that can be edited and enhanced, validation can be added at a later date. This form has three fields (email address, name and message) that users can fill in. Once the user clicks the ‘Submit’ button, it will collect the information contained within the fields, tag the information as “email, name and message”, then send the information to sendmail.php.

The sendmail script:

Now we have a form that sends information to a script, we need to create a script to send the email. In this example, we will name the script sendmail.php as this is the address the our form is submitting the data to.

In order to send an email, we need certain information (variables), so lets set them first.

<?php
$email_to = “
you@yourdomain.com“;
$name = $_POST["name"];
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = “Feedback from website”;
$headers =
“From: $email_from .\n”;
“Reply-To: $email_from .\n”;

The code above sets the email address you will send your email to and gets the users’ name, email address, and message from the previous form. It also sets the variables that contain the email subject, and headers to specify the from and reply to email addresses.

Now lets build the message of the email with the users’ name and comments.

$message = “Name: “. $name . “\r\nMessage: ” . $message;

Finally, let’s send the email. If the email is sent we will go to a thank you page, if there is an error we will display a brief message.

ini_set(“sendmail_from”, $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, “-f” .$email_from);
if ($sent)
{
header(“Location:
http://www.yourdomain.com/thankyou.html“);
} else {
echo “There has been an error sending your comments. Please try later.”;
}
?>

This script does not have any validation or error checking, so it is not recommended that you copy it directly to your website, however, it does show the basics of sending email from our webservers, and can be used as a framework for your own scripts.

IMPORTANT:

To prevent spam being sent through our webservers, there are certain conditions that must be met before our SMTP servers will send the email.

Email must be sent to, or from, an existing email address hosted by espinda.com. This must be a mailbox on the same domain name on which the form to mail script is being hosted.

To stop misuse of your form by third parties the sendmail_from variable should be set to your espinda hosted email address. While access to the php.ini file is restricted on our shared environment, you can sent this variable using the ini_set() command, shown below.
A fifth parameter, -f, should be added to the sendmail function. This will set the name of the from email address.
In its basic form, a simple sendmail script will look like this:

<?php
ini_set(“sendmail_from”, “user@yourdomain.com”);
mail($email_to, $email_subject, $email_message, $headers, “-fuser@yourdomain.com”);
?>
Provided that your form to mail script meets the requirements above, you should have no problems.

If you don’t specify an existing mailbox on your domain as the to or from email address, your script may produce a 554: Recipient address rejected: Relay access denied error.
Using third party scripts to send email:

Third party scripts using sendmail will also work on espinda servers, although some will need slight changes made in order to work correctly.
If you are using a third party script to send email remember to set the sendmail_from variable (using ini_set(‘sendmail_from’,email_from)), add the fifth -f parameter, and send the email either to, or from, an email address that’s hosted by espinda.com.


Post your comment:

You must be logged in to post a comment.