Generate a auto response  email using PHP that is from

Posted on 2014-03-31
Last Modified: 2014-05-02
Hi PHP developers

I have a script that is part of my contact page on my web site. Basically the script sends a email to hosted mail box then forwards the email to a distribution group on a exchange server as shown below:



$emailBody =   "Name: ".$name."\nEmail: ".$email."\nPhone: ".$phone."\n\nMessage: \n\n".$message."\n";                                

$headers = 'From:' . "\r\n" .
    'Reply-To:' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail('', 'Email from Website', $emailBody, $headers);

header('Location: ../contact-thanks.html');

What I would like to do is by pass the hosted server and delivery directly to exchange 2010 mailbox which has forwarding setup to a distribution group all ready setup on the exchange server.

I have created the mailbox and forwarding to the distribution group but what I would really like is the php script to send a No-reply email back to the sender thanking them etc maybe a link for more help.

Anyone got any ideas on the best way to achieve this?

In addition would I need to create a spf record to achieve this?


Question by:cybersi
  • 3
  • 2
LVL 109

Expert Comment

by:Ray Paseur
ID: 39966691
I'm not completely clear on what you want but I think I might have an idea.  Let me try to paraphrase...

When emails are received at a certain address, they are to be processed automatically on your server.  The automatic processing should include, inter alia, a response to the sender.  The automatic processing would include forwarding to the distribution list.

Does that capture the idea?  If so, you might want an "email pipe."

Author Comment

ID: 39967958
Hi Ray
In a nut  shell I would like to add to my php script to send a no reply email if it can be done?
LVL 109

Expert Comment

by:Ray Paseur
ID: 39969259
You can send an email message from within a PHP script.  All contents of the email can be made up by you (that is one of the problems with email -- no accountability).  You use this function.

Since there are several layers of technology involved in sending email you would want to set up some tests, paying close attention to the headers that your server creates.  You can specify some of the headers, as shown in the online documentation about additional_headers.  The "From" address is one of the additional headers.

You may also want to familiarize yourself with this:

If you're new to PHP and you want to get a foundation, this article can help.

Author Comment

ID: 39969506
Hi Ray

You guessed it I have zip knowledge when it comes to PHP.  What I would like to do is capture the email address from the above field and then send a auto response to the captured email address thanking them and providing a link to a search facility I have on my web site.  So I need the capture and send email elements to be added to the above script. As it stands the script obviously already sends me an email with information from this page


LVL 109

Accepted Solution

Ray Paseur earned 500 total points
ID: 39970653
In the generated source code for the contact-us page, the form starts on line 262.  Stripped of fluff and formatting it looks more or less like this:

<form action="scripts/mail.php" method="post" id="contactform">
  <p class="nm">
    <label for="customername">Name:</label> 
    <input type="text" name="customername" id="customername" />
    <label for="telephone">Telephone number:</label>
    <input type="text" name="telephone" id="telephone" />
    <label for="email">Email Address:</label>
    <input type="text" name="email" id="email" />
    <label for="message" >Message:</label>
    <textarea name="message" rows="10" cols="20" id="message"></textarea>
    <input type="submit" name="contactus" class="noti contactbtn" value="" />

Open in new window

The HTML form will be submitted to the URL of the action= attribute of the form tag.  This is scripts/mail.php.  The email address is expected to be found in the PHP variable named $_POST['email'].  It will be there because this is a POST-method request and the form contains an input control with name="email".  The name in the form input control is the name that is used for the array element in the request array.

Your server-side automation will need to filter the request array and determine if the data is suitable for further use.  For example, you will need to decide if the email address is acceptably formatted and if the address is routable.  Since this is external data, your PHP script must work under the assumption that the data is tainted and is an attack vector.  The rule here is "accept only known good values."

When you start capturing email addresses and sending out messages one of the first things that is going to happen is that a hacker will discover your form and will start sending honeypot addresses to the form.  Your script will, in turn, send automated email messages to the honeypots and you'll quickly be branded a spammer.  Your hosting company may shut you down.  And there may even be criminal penalties involved (in the USA you can get prison time for something like this).  So you will want to have some way of controlling who can put information into the form.  A good way to prevent an automated attack is with a CAPTCHA test.

As you may have guessed by now, there are a lot of moving parts to something like this.  You might want to consider hiring a professional PHP programmer to help you with the implementation details.  It's not a big job, but there are so many nuances and subtle risks that it's very easy to get something wrong.  A pro would have been through those risks before and would have ways of mitigating the risks.  Just a thought... ~Ray

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question