doubi7346 2016-07-31 10:21
浏览 25

联系表格名称字符串

I am trying to have it so when the user sends the form the subject line show the user's name like "Inquiry from [Name of user]". How it's setup now it gets to me with the subject "Contact form from site." How can I modify the code to make that happen?

<?php
// configure
$from = 'info@mysite.com'; 
$sendTo = 'info@mysite.com';
$subject = 'Contact form at site';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email
$okMessage = 'Your Message was successfully submitted. Thank you! We will get back to you soon.';
$errorMessage = 'There was an error while submitting the form. Please try again or call us at 970-201-9619';

// let's do the sending

try
{
    $emailText = "You have new message from your Website
=============================
";

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

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

    mail($sendTo, $subject, $emailText, "From: " . $from);

    $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'];
}
  • 写回答

1条回答 默认 最新

  • duanchi3109 2016-07-31 10:36
    关注

    Made change in the subject as per your requirement. I have moved the subject variable right above the mail sending code and concatenate name and surname of user with subject line of mail. Please check below snippet and let me know if you have any query.

    <?php
    // configure
    $from = 'info@mysite.com'; 
    $sendTo = 'info@mysite.com';
    $fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email
    $okMessage = 'Your Message was successfully submitted. Thank you! We will get back to you soon.';
    $errorMessage = 'There was an error while submitting the form. Please try again or call us at 970-201-9619';
    
    // let's do the sending
    
    try
    {
        $emailText = "You have new message from your Website
    =============================
    ";
    
        foreach ($_POST as $key => $value) {
    
            if (isset($fields[$key])) {
                $emailText .= "$fields[$key]: $value
    ";
            }
        }
        $subject = 'Inquiry from '.$_POST['name']." ".$_POST['surname'];
        mail($sendTo, $subject, $emailText, "From: " . $from);
    
        $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'];
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法