Jump to content

Recommended Posts

Hello, I have a contact form that uses SMTP authentication. I want the form to be sent from the e-mail address the user enters (so I can hit reply when I get the message) and not from the address used to authenticate.

 

The form works perfectly until I try to get it sent from any other address than the one in the account.

 

Is this possible? I can't image it's not. does anyone have sample code I can view?

 

Thanks.

 

Todd

Link to comment
https://forums.phpfreaks.com/topic/271624-web-form-with-smtp-authentication/
Share on other sites

Thank you Christian.

 

as I am new to PHP might I ask anyone to help with implementing the Reply-to? I've tried and keep missing something.

 

My form is coming from TEST<todd@xxxxxx.com> - as shown in below code but I need it to come from $email so that when the e-mail is replied to it goes to that address, not TEST<todd@xxxxxx.com>.

 

 

This is my original code. Thank you!

 

 

<?php

if(isset($_POST['email'])) {

 

function died($error) {

// your error code can go here

echo "We are very sorry, but there were error(s) found with the form you submitted. ";

echo "These errors appear below.<br /><br />";

echo $error."<br /><br />";

echo "Please go back and fix these errors.<br /><br />";

die();

}

 

// validation expected data exists

if(!isset($_POST['contact_name']) ||

!isset($_POST['email']) ||

!isset($_POST['telephone']) ||

!isset($_POST['comments'])) {

died('We are sorry, but there appears to be a problem with the form you submitted.');

}

 

$contact_name = $_POST['contact_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['telephone']; // required

$comments = $_POST['comments']; // not required

 

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z 0-9.'-]+$/";

if(!preg_match($string_exp,$contact_name)) {

$error_message .= 'The Contact Name you entered does not appear to be valid.<br />';

}

if(!preg_match($string_exp,$telephone)) {

$error_message .= 'The Telephone # you entered does not appear to be valid.<br />';

}

 

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";

 

function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}

 

$email_message .= "Contact Name: ".clean_string($contact_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Telephone: ".clean_string($telephone)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";

 

ini_set('display_errors',1);

error_reporting(E_ALL);

 

require_once "Mail.php";

 

$from = "TEST<todd@xxxxxx.com>";

$to = "<anyone@xxxxxxxx.com>";

$subject = "Test message";

$body = "$email_message";

 

$host = "mail.xxxxxxxx.com";

$username = "todd@xxxxxxxx.com";

$password = "xxxxxxxxxx";

 

$headers = array ('From' => $from,

'To' => $to,

'Subject' => $subject);

$smtp = Mail::factory('smtp',

array ('host' => $host,

'auth' => true,

'username' => $username,

'password' => $password));

 

$mail = $smtp->send($to, $headers, $body);

 

if (PEAR::isError($mail)) {

echo("<p>" . $mail->getMessage() . "</p>");

} else {

echo("<p></p>");

}

?>

 

 

<!-- include your own success html here -->

message sent

 

<?php

}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.