Web Hosting Talk







View Full Version : PHP contact form. ReplyTo:


BXmanagement
10-17-2007, 02:53 PM
Hi,
I have created a contact form for my clients to contact our technical support department. On the contact form, there is a text box where the user is required to put their email address ($Email). Once the user clicks "submit" the contact form details are sent to an email address. How can I have the "reply to:" in the submitted email, be the email address ($Email) the user supplied?

Here is my contact form page - helpinghandhost.com/support/templates/custom/supportticketsubmit-techsupport.html

Here is my php code -
<?php

// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "admin@helpinghandhost.com";
$Subject = "Technical Support Request";
$Name = Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Department = Trim(stripslashes($_POST['department']));
$Subject = Trim(stripslashes($_POST['subject']));
$Urgency = Trim(stripslashes($_POST['urgency']));
$cPanelUser = Trim(stripslashes($_POST['cpaneluser']));
$cPanelPass = Trim(stripslashes($_POST['cpanelpass']));
$Message = Trim(stripslashes($_POST['message']));

// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;
if (Trim($Email)=="") $validationOK=false;
if (Trim($Subject)=="") $validationOK=false;
if (Trim($Urgency)=="") $validationOK=false;
if (Trim($cPanelUser)=="") $validationOK=false;
if (Trim($cPanelPass)=="") $validationOK=false;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Subject = "Tech Support Request";
$Body = "";
$Body .= "EmailFrom: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Department: ";
$Body .= $Department;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject;
$Body .= "\n";
$Body .= "Urgency: ";
$Body .= $Urgency;
$Body .= "\n";
$Body .= "cPanel Username: ";
$Body .= $cPanelUser;
$Body .= "\n";
$Body .= "cPanel Password (1st character): ";
$Body .= $cPanelPass;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body);

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Thank you!

ThatScriptGuy
10-17-2007, 06:11 PM
$headers = "From: $Email";
mail($EmailTo, $Subject, $Body, $headers);

However, your main concern should be security in this script. Right now it's wide open for all kinds of fun stuff...Also, consider using header("Location: ok.htm"); instead of the meta refresh...

Kevin

berumelu
10-17-2007, 07:08 PM
You have to use the "Return-path" and "Reply-to" parameters

To have the "Reply-to" parameter add the next line:


$headers = "From: ".$EmailFrom."\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: ".$Name." <".$Email.">\r\n";
$headers .= "Reply-to: ".$Name." <".$Email.">\r\n";

$success = mail($EmailTo, $Subject, $Body, $headers);


Take a look to PHP mail() function:

http://www.php.net/manual/en/function.mail.php


Regards,


Luis Berumen

Steve_Arm
10-17-2007, 11:40 PM
Actually using mail() you can't set the return-path header. Sendmail discards it and set it's own (server's hostname).
You must use phpmailer for full control of the headers.

BXmanagement
10-18-2007, 12:26 AM
Hey Steve_Arm,

Thanks for pointing that out. I couldn't get the mail() to work. I'll search it on Google, but do you have an easy solution?

Thanks!

Steve_Arm
10-18-2007, 01:14 AM
http://phpmailer.sourceforge.net/

Use the example on the first page and you are set.

BXmanagement
10-18-2007, 01:30 AM
I have installed phpmailer on my server. Now, I am doing a test email, but the reply-to: email still won't show. I want the email that the user specifies to be the "reply to:" or "$mail->From" email when I view it in outlook.

Here is the page where the user will input information - helpinghandhost.com/support/templates/custom/supportticketsubmit-techsupport.html

Here is my php code (not finished!) -
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.helpinghandhost.com"; // SMTP server
$mail->From = "$Email";
$mail->AddAddress("admin@helpinghandhost.com");

$mail->Subject = "$Subject";
$mail->Body = "$Message";
$mail->WordWrap = 50;

$Email = 'email';
$Subject = 'subject';
$Message = 'message';

if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent.';
}
?>

Any suggestions?

Steve_Arm
10-18-2007, 01:48 AM
You missed something from the example:
Add this below AddAddress()

$mail->AddReplyTo("admin@helpinghandhost.com","Administrator");

BXmanagement
10-18-2007, 02:03 AM
Hi Steve_Arm,

Thanks for helping me here. I don't know if I'm not getting it or you don't understand what I'm trying to do. I'm trying to do the following;

1. The user fills out a contact form on my website. (includes, name, email, subject, message)

2. The user hits, "submit". The information in the contact form is then sent to my email (blank@helpinghandhost.com).

3. I open the email, and automatically, the "reply-to" or "sent from" field is filled in with the users email, specified in the contact form.

Does that make sense?

Thanks! I appreciate your help!

Steve_Arm
10-18-2007, 02:10 AM
Then you need to set the the reply-to to the user's e-mail address.

Have you tried filling the AddReplyTo() and From() with the user's e-mail?

I'm not sure if the From() will work.

BXmanagement
10-18-2007, 02:14 AM
Have you tried filling the AddReplyTo() and From() with the user's e-mail?

But every time a user submits the contact form, the email address will be different because a different user is filling in the form. How can I call their email address from the email address field in my contact form?

Steve_Arm
10-18-2007, 03:04 AM
OK, I can only assume that you have pasted first's posts code from somewhere.
Anyway, you get the suppplied address with the $_POST.

$mail->From($_POST['email']);
$mail->AddReplyTo($_POST['email']);

BXmanagement
10-25-2007, 02:31 PM
I have found an alternative method that will work until I can get the above code working correctly.

ThatScriptGuy
10-25-2007, 03:35 PM
My first post in this thread (Post number 2) explained exactly how to do this. You need to add headers to the email, and then modify the mail command that you're running.