Before we startTo 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 email address hosted by Fasthosts. An email address hosted by Fasthosts is not the same as a domain name hosted by Fasthosts. If your domain's MX record points to another email provider, it will not count as being hosted by Fasthosts.
2) To stop misuse of your form by third parties the sendmail_from variable should be set to your Fasthosts 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:
<?php
ini_set("sendmail_from", "
user@yourdomain.com ");
mail($email_to, $email_subject, $email_message, $headers,
'-f'user@yourdomain.com);
?>
Provided that you set the sendmail variable, before attempting to send the email. Specify the from address as a fifth parameter in the sendmail function, and the email is either to, or from, a Fasthosts hosted email address you should have no problems.
ExampleThis 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. In this example we will use bobsdomain.co.uk as the domain name. There is a mailbox hosted with Fasthosts called
bob@bobsdomain.co.uk.
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>
example feedback form
Not the prettiest form, but it can be tidied up, and 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 the our form is submitting the data. To send an email, we need certain information (variables), so lets set them first.
<?php
ini_set('sendmail_from', $email_from);
$email_to = "bob@bobsdomain.co.uk";
$name =$_POST['name'];
$email_from =$_POST['email'];
$email_subject = "Feedback from website";
$comments = $_POST['message'];
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
Now lets build the message of the email with the users name and comments.
$message= "Name: ". $name . "\nComments: " . $comments;
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.
$sent = mail($email_to, $email_subject, $message, $headers, '-f' .$email_from);
if ($sent)
{
header( "Location:
http://www.bobsdomain.co.uk/thankyou.html" );
} else {
echo 'There has been an error sending your comments. Please try later.';
}
?>
example sendmailscript form
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 emailThird 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 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, a Fasthosts hosted email address.