Nice PHP contact form script and a few alterations

A colleague from the Women Designer’s Group has a great little PHP contact form script posted on her blog. I recently used it for a client with just a bit of easy modification to the CSS styles to make it play nice with the rest of the page.

However, this client needed to have two different emails sent upon submitting the form – two emails with two different headers. Here’s how you take care of that:

Download either script – the Simple one just has name, email and a textarea for comments, while the Complex one has other input elements like radio buttons and dropdown lists. I used the Simple one.

Open up index.php, this is the actual form processing script. Note that what I’m doing here is not the only customization you’ll need to do to use this script! But it’s very well-documented and should make sense to anyone with a smattering of PHP.

Scroll down to the bottom and find this section:

$mailheader = "From: $name < $email>nContent-Type: text/html";

mail("YOUR NAME OR COMPANY NAME HERE","$subject","$message","$mailheader");

The first line pulls the user’s name and email from the form that’s being processed and sets that up as the ‘from’ information for the email that’s coming to you.

The second line is where the email’s actually going – pretty self-explanatory. You enter your company name and email address, and it pulls in the subject (which you’ll define earlier in this script), the message (which includes all the information the user submitted), and the mailheader (the ‘from’ information above).

Pretty simple.

So what happens if you need to send two emails? What if you’d like one email to come to you, but you’d like another to go back to the user who filled out the form?

Not too difficult, really. What you’ll do is create copies of the two lines shown above. We already have ‘$mailheader,’ so just copy that and change it to ‘$mailheader2.’

For $mailheader2, let’s change it so it indicates that it came from your company:

$mailheader2 = "From: YOUR COMPANY NAME \nContent-Type: text/html";

Now copy the ‘mail’ line and we’ll change that like so:

mail("YOUR COMPANY'S VISITOR < $email>","$subject","$message","$mailheader2");

This will send an email with the subject line ‘Your Company’s Visitor’ to the email address entered by the user, the subject line you created earlier in the script, the message containing all the data submitted by the user, and your second mailheader showing your company as the ‘From’ address.

Make sense?

Leave a Reply

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