php表单发送到电子邮件字段中输入的电子邮件

I would like my simple php mailer form, to maintain sending me the filled out form; but also I would like to send them a confirmation message based on their inputted email address field - wondering if it's possible to send to email inserted in the form.

Eg: User fills out email address in the field as asked; this is where custom confirmation will be sent upon submit (along with form submission to me)

Note: // Same confirmation will be sent to every form user. I simply would like to write; with PHP - send this message to the email address inserted by current user in email field.

Updated:


I currently have the form submitting at my email:

$emailAddress = 'myemail@myemail.com';

I'm getting the fields with:

Email:  '.$_POST['email'].'<br />

How do I edit it to send to the user inputted 'email as their email address' -- and spit out the generic message with it?


Recent attempt:

$emailAddress = 'myemail@myemail.com';
$to      = $_POST['email'];
$message = 'Thank you for submitting the form on our website.!';

Form submits as normally; but doesn't send confirmation to '$to = $_POST['email'];'

Any pointers?


Complete code:

<?php


$emailAddress = 'myemail@myemail.com';
$to      = $_POST['email'];
$message = 'Thank you for submitting the form on our website.!';

/* config end */


require "phpmailer/class.phpmailer.php";

session_name("fancyform");
session_start();    


foreach($_POST as $k=>$v)
{
    if(ini_get('magic_quotes_gpc'))
    $_POST[$k]=stripslashes($_POST[$k]);

    $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}


$err = array();

if(!checkLen('name'))
    $err[]='The name field is too short or empty!';

if(!checkLen('email'))
    $err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
    $err[]='Your email is not valid!';

/* if(!checkLen('phone'))
    $err[]='The phone field is too short or empty!'; */

/*if(!checkLen('subject'))
    $err[]='You have not selected a subject!';*/

/* if(!checkLen('message'))
    $err[]='The message field is too short or empty!';*/

if((int)$_POST['captcha'] != $_SESSION['expect'])
    $err[]='The captcha code is wrong!';


if(count($err))
{
    if($_POST['ajax'])
    {
        echo '-1';
    }

    else if($_SERVER['HTTP_REFERER'])
    {
        $_SESSION['errStr'] = implode('<br />',$err);
        $_SESSION['post']=$_POST;

        header('Location: '.$_SERVER['HTTP_REFERER']);
    }

    exit;
}


$msg=
'Name:  '.$_POST['name'].'<br />
Phone:  '.$_POST['phone'].'<br />
Email:  '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Referred By:    '.$_POST['referer'].'<br /><br />
Message:<br /><br />

'.nl2br($_POST['message']).'

';


$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";

$mail->MsgHTML($msg);

$mail->Send();


unset($_SESSION['post']);

if($_POST['ajax'])
{
    echo '1';
}
else
{
    $_SESSION['sent']=1;

    if($_SERVER['HTTP_REFERER'])
        header('Location: '.$_SERVER['HTTP_REFERER']);

    exit;
}

function checkLen($str,$len=2)
{
    return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
    return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}

?>

How could I output:

$to      = $_POST['email'];
$message = 'Thank you for submitting the form on our website.!';

Have a look at PHPMailer. It's a pretty hand library for sending emails.

http://phpmailer.worxware.com

You can then just use GET or POST variables to send the email to the server

Sure you can do this. It's just as straight forward as it sounds. I recommend using a mailer helper like phpmailer, but you do it with the php mail function as well.

//check the email is real
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $to      = $_POST['email'];
    $subject = 'Thanks for filling out my form';
    $message = 'The message';
    $headers = 'From: me@mywebsite.com' . "
" .
    'Reply-To: webmaster@mywebsite.com' . "
" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
}

straight outta the manual

I believe you are looking for the built-in PHP mail() function. You post a form to your PHP page, with HTML like so:

<form action="your_php_script.php" method="post">
E-mail:&nbsp;<input type="text" name="email" />
<input type="submit" value="submit">
</form>

And your_php_script.php like so:

<?php
$to      = $_POST['email'];
$subject = 'Confirmation E-mail';
$message = 'Thank you for submitting the form on our website.';
$headers = 'From: donotreply@yoursite.com' . "
" .
    'Reply-To: webmaster@yoursite.com' . "
" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

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