我怎样才能得到这种形式的姓名,电子邮件和主题成为我从服务器接收邮件中的实际值?

问题描述 投票:0回答:1

如何配置从顶部$主题&$变量,以使服务器发送我的电子邮件使用这些值,而不是静态的文字?

<?php

// configure
$from = 'Demo contact form <[email protected]>';
$sendTo = 'Demo contact form <[email protected]>'; // Add Your Email
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// let's do the sending

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

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

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

$headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
);

mail($sendTo, $subject, $emailText, implode("\n", $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'];
}

我想从$使用该名称值,$主题使用主题值。

php contact-form
1个回答
1
投票

使用以下命令:

# checks if the 'name' key has been $_POST'ed
$from = isset($_POST['name'])?$_POST['name']:$from;

# checks if the 'subject' key has been $_POST'ed
$subject = isset($_POST['subject'])?$_POST['subject']:$subject; 
© www.soinside.com 2019 - 2024. All rights reserved.