引导邮件表格$来自< $ email>

时间:2017-04-06 06:06:40

标签: php email html-email mail-form formmail

我指的是这个页面。 https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form

我想要mailheader“name”

但是这个PHP代码是

<?php

// configure
$from = 'Demo contact form <demo@domain.com>';
$sendTo = 'Demo contact form <demo@domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', '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'];
}

所以我试过

$ sendTo ='演示联系表单'; =&GT;我的邮件。

$ from ='演示联系表'; =&GT; $ name&lt;'$ email'&gt;  但结果是“没人”

所以我再试一次。

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

=&GT;

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

但它失败了......

我该怎么办?

我试过

来自:$ FromName和

$ headers =“From:$ from_user \ r \ n”。和

$ headers。='发自:“'。$来自。'”&lt;' 。 $电子邮件。 '&GT;' 。为 “\ r \ n” 个;等...

1 个答案:

答案 0 :(得分:-2)

这对我有用

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));

$from = "$name<$email_address>";