简体   繁体   English

PHP 联系表 - 回复以获取提交者的电子邮件

[英]PHP Contact Form - make the Reply-To to have the submitter email

I inspected different topics there, but all of them are not working with my code.我在那里检查了不同的主题,但它们都不适用于我的代码。 I need to get the ability to answer direct to submitter email via my email.我需要能够通过我的电子邮件直接回复提交者的电子邮件。 Here is my code:这是我的代码:

    <?php

// configure
$from = '<mymail@gmail.com>';
$sendTo = '<mymail@gmail.com>';
$subject = 'New message from PROMO form';
$fields = array('name' => 'Name', 'city' => 'City', 'tel' => 'Tel', 'email' => 'Email', 'message' => 'Message', 'age' => 'Age', 'info' => 'Info', 'checkboxtwo' => 'Checkboxtwo'); // array variable name
$okMessage = 'Спасибо.';
$errorMessage = 'Извините, ошибка.';
$headers = 'From: ' . $fields['email'] . "\r\n" . 'Reply-To: ' . $fields['email'];

try
{
    $emailText = "You have new message from online form form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    mail($sendTo, $subject, $emailText, $headers);

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}

I will highly appreciate any help.我将非常感谢任何帮助。 Thank you.谢谢你。

THE ANSWER答案

Re-write your foreach like this:像这样重写你的 foreach:

foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
            $fields[$key] = $value; //It set the values on the array
        }
}

Then add the headers:然后添加标题:

$headers = 'From: ' . $fields['email'] . "\r\n" . 'Reply-To: ' . $fields['email'];

So just send the mail.所以只能发邮件了。

mail($sendTo, $subject, $emailText, $headers);

Other answers (ignore them)其他答案(忽略它们)

Just set the headers with the "Reply-To".只需使用“回复”设置标题即可。

$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from;

mail($sendTo, $subject, $emailText, $headers);

You will get any replies to the email that you used to submit the message.您将收到对用于提交邮件的电子邮件的任何回复。

EDIT编辑

Get the desired email by using this:使用以下方法获取所需的电子邮件:

$sendTo = $fields['email'];

Then you will be able to send the email using this var.然后您将能够使用此变量发送电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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