PHP联系表单 - 使回复 - 具有提交者电子邮件

时间:2017-01-14 11:16:27

标签: php email contact contact-form

我在那里检查了不同的主题,但所有主题都没有使用我的代码。我需要能够通过我的电子邮件直接回复提交者的电子邮件。这是我的代码:

    <?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'];
}

我将非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

答案

像这样重写你的foreach:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fader : MonoBehaviour {

    public float min_fade_speed;
    public float max_fade_speed;

    private SpriteRenderer sprite;
    private float fade_speed;

    // Use this for initialization
    void Start () {
        fade_speed = Random.Range (min_fade_speed, max_fade_speed);
        sprite = GetComponent<SpriteRenderer> ();
        reset_color ();
    }

    void reset_color() {
        //Initially transparent
        sprite.color = new Color(1, 1, 1, 0.0f);
    }

    // Update is called once per frame -- currently unused
    void Update () {
    }

    void FadeInAndOut() {
        sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (0.0f, 1.0f, Mathf.PingPong(Time.time/fade_speed, 1f)));
    }

    IEnumerator FadeOut(float alpha_start) {
        Debug.Log ("Alpha is: " + alpha_start);
        while (sprite.color.a > 0.0f) {
            sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (alpha_start, 0.0f, Time.time / fade_speed));
            yield return null;
        }
    }

    void onMouseEnter() {
        reset_color ();
    }

    void OnMouseOver() {
        FadeInAndOut ();
    }

    void OnMouseExit() {
        float alpha = sprite.color.a;
        Debug.Log ("Alpha is: " + alpha);
        StartCoroutine ("FadeOut", alpha);
        // StartCoroutine(FadeOut(alpha));
    }
}

然后添加标题:

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

所以只需发送邮件。

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

其他答案(忽略它们)

只需使用“回复”设置标题。

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

您将收到用于提交邮件的电子邮件的任何回复。

修改

使用以下内容获取所需的电子邮件:

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

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

然后您就可以使用此var。

发送电子邮件