dtwbp26022 2017-02-25 16:20
浏览 81
已采纳

PHP(邮件)联系表 - “回复发件人的电子邮件”问题[重复]

This question already has an answer here:

Sorry if this may seem stupid but my PHP knowledge is somewhere near the "0" mark. I've built a website and am using a rather simple PHP contact form as per needs. However I still have some issues which I am not able to really solve.

For instance, when I receive the email I would like to be able to reply to the person who sent the email, basically be able to reply to his/her added email address. Right now the PHP is configured to have a "from" and "to" containing my server side email(s). I will paste below the code. Whenever I try to hit "Reply" I am basically replying to my server side email address.

Is there a way I can do that through the code I pasted below?

And the other question is related to Gmail. When I get an email everything is put inline so I get something like: "Name - Name, Surname - Surname, Email - Email, Message - Message". I have a redirection put on the server side email so I can get the email on a gmail address as well. However, if I am viewing the email on the server side, through webmail, is properly formatted, each line on its separate row.

Can I achieve that on Gmail?

Here is my PHP code

<?php

// configure
$from = 'contact@mydomain.com';
$sendTo = 'contact@mydomain.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
=============================
";

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

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

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

    mail($sendTo, $subject, $emailText, implode("
", $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'];
}
</div>
  • 写回答

1条回答 默认 最新

  • dongrong7883 2017-02-25 16:28
    关注

    Instead using hard-coded "From" you should receive user's email and set it as 'from'.

    I guess that in your form you have email field with name email. So change from

    'From: ' . $from,
    

    to

    'From: ' . $_POST['email'],
    

    Now header From: should contain provided user's email. In my opinion your code is weird but this fix your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效