简体   繁体   中英

Send a confirmation email using php

I am using the script below to return an email with the submitted fields to my email address.

I have recently found the need to have a confirmation email sent to who ever submitted the form in the first place but am unsure on how to alter the script. I basically need it to say something like "Dear $name, Thanks for contacting us....".

Please can someone help?

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Website Enquiry';

// Your email address. This is where the form information will be sent.
$emailadd = 'myemail@myemail.co.uk';

// Where to redirect after form is processed.
$url = 'thanks.php';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "WEBSITE ENQUIRY:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

The script allows for any form element to be sent in the email without the need of including it within the php script.

The email address is collected in the html

<span class="input">
  <input type="text" name="Email" id="Email"/>
  </span>

and the name...

<span class="input">
    <input type="text" name="Name" id="Name" />
 </span>

Many thanks

Craig

This code will send a confirmation to the user who submitted the form, but it doesn't check to make sure they've entered a valid email address. There's a handy script at isemail.info which will validate that for you.

Insert the code just above the ---Do not edit below this line--- line.

// Subject of confirmation email.
$conf_subject = 'Your recent enquiry';

// Who should the confirmation email be from?
$conf_sender = 'Organisation Name <no-reply@myemail.co.uk>';

$msg = $_POST['Name'] . ",\n\nThank you for your recent enquiry. A member of our 
team will respond to your message as soon as possible.";

mail( $_POST['Email'], $conf_subject, $msg, 'From: ' . $conf_sender );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM