Use php to send email from a from (with /f switch)


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

  1. Email must be sent to, or from, an existing email address hosted by Calco. This must be a mailbox on the same domain name on which the form to mail script is being hosted. (NOTE: we can in awkward situations set up a dummy email address e.g. which forwards to a GMAIL or HOTMAIL address etc, thus satisfying the requirements of our security scripts)
  2. To stop misuse of your form by third parties the sendmail_from variable should be set to your Calco  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.
  3. 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:

ini_set("sendmail_from", "");
mail($email_to, $email_subject, $email_message, $headers, "");

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.

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" />

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 click 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 our form is submitting the data to.
In order to send an email, we need certain information (variables), so lets set them first.

$email_to = "";
$name = $_POST["name"];
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Feedback from website";
$headers = "From: " . $email_from . "\n";
$headers .= "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)
} else {
echo "There has been an error sending your message. 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.


Using third party scripts to send email

Third party scripts using sendmail will also work on Fasthosts servers, although some will need slight changes made in order to work correctly.
If you are using a third party script to send email it must meet the requirements of our system above. Remember to set the sendmail_from variable, add the fifth -f parameter, and send the email either to, or from, a Calco hosted email address.



There are spam conditions implemented (quite rightly) by Fasthosts that prevent the normal use of FormMail - the most important of which is the F switch

-f switch, referrers & recipients variables

On the left you will see there are two main steps to getting ReCAPTCHA working. The first step is getting a modern version of FormMail (we used v1.93 from here ). Once this has been edited with the Fasthosts additions on the left, you can start finding the relevant lines of code and inserting the Google reCAPTCHA "stuff"

All queries to Fashosts or Google please!

How to add a CAPTCHA

Website design with Calco UK is as easy as 1 - 2 - 3
1. Hosting » 2. Design » 3. SEO