PHP邮件表单:用户插入他的电子邮件 - >电子邮件地址应该是发件人,以便于回复

时间:2015-02-09 15:29:23

标签: php html forms email

我需要在以下代码中更改用户在表单中输入其电子邮件地址的内容。在我收到的邮件中,用户的电子邮件应该自动成为发件人 - 这样我就可以轻松回复。 谢谢和问候

<?php

 

$zieladresse = 'IhrName@EXAMPLE.com';

$absenderadresse = 'ihrAndererName@EXAMPLE.com';

// Welcher Absendername soll verwendet werden?
$absendername = 'Formmailer';


$betreff = 'Feedback';


$urlDankeSeite = 'http://www.EXAMPLE.com/IhrName/IhreSeite.htm';


$trenner = ":\t"; // Doppelpunkt + Tabulator


require_once "Swift-4.0.7/lib/swift_required.php"; // Swift initialisieren

if ($_SERVER['REQUEST_METHOD'] === "POST") {

    $message = Swift_Message::newInstance(); // Ein Objekt für die Mailnachricht.

    $message
        ->setFrom(array($absenderadresse => $absendername))
        ->setTo(array($zieladresse)) // alternativ existiert setCc() und setBcc()
        ->setSubject($betreff);

    $mailtext = "";

    foreach ($_POST as $name => $wert) {
        if (is_array($wert)) {
                foreach ($wert as $einzelwert) {
                $mailtext .= $name.$trenner.$einzelwert."\n";
            }
        } else {
            $mailtext .= $name.$trenner.$wert."\n";
        }
    }

    $message->setBody($mailtext, 'text/plain');

    $mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
    $result = $mailer->send($message);

    if ($result == 0) {
        die("Mail konnte nicht versandt werden.");
    }

    header("Location: $urlDankeSeite");
    exit;
}

header("Content-type: text/html; charset=utf-8");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
    <head>
        <title>Einfacher PHP-Formmailer</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <h1>Beispielformular</h1>
        <form action="" method="post">
            <!-- Hier die eigentlichen Formularfelder eintragen. Die folgenden sind Beispielangaben. -->
            <dl>
                <dt>Ihr Name:</dt>
                <dd><input type="text" name="Versender" /></dd>
                <dt>Ihre E-Mail:</dt>
                <dd><input type="text" name="E-Mail" /></dd>
                <dt>Sie können:</dt>
                <dd><input type="checkbox" name="kannwas[]" value="HTML" />HTML <input type="checkbox" name="kannwas[]" value="PHP" />PHP</dd>
                <dt>Sie sind:</dt>
                <dd><input type="radio" name="sexus" value="M" />Mann <input type="radio" name="sexus" value="Frau" />Frau</dd>
                <dt>Sie mögen:</dt>
                <dd><select name="Browser"><option value="Opera">Opera</option><option value="Mozilla">Mozilla</option></select></dd>
                <dt>Bemerkungen:</dt>
                <dd><textarea name="Bemerkungen" rows="3" cols="20">Bemerkungen</textarea></dd>
            </dl>
            <!-- Ende der Beispielangaben -->
            <p>
            <input type="submit" value="Senden" />
            <input type="reset" value="Zurücksetzen" />
            </p>
        </form>
    </body>
</html>

以下代码怎么样,它也有效,似乎不太复杂。这里有什么要调整,以便发件人的电子邮件自动显示在电子邮件程序中作为发件人回复?

的HTML / PHP:

<form method="post" action="Untitled-6.php">

        <label>Name*</label>
        <input name="name" placeholder="Type Here">

        <label>Email*</label>
        <input name="email" type="email" placeholder="Type Here">

        <label>Message</label>
        <textarea name="message" placeholder="Type Here"></textarea>

        <label>*What is 2+2? (Anti-spam)</label>
        <input name="human" placeholder="Type Here">

        <input id="submit" name="submit" type="submit" value="Submit">

    </form>

PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo'; 
$to = 'ceyca82@gmail.com'; 
$subject = 'Hello';
$human = $_POST['human'];

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit'] && $human == '4') {                 
    if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}

&GT;

1 个答案:

答案 0 :(得分:0)

$ absenderadresse = $ _POST [“电子邮件”];

但这里有几个问题:

1)你不应该在堆栈溢出(https://stackoverflow.com/tour

中发布这类问题

2)这里有大量的表单处理教程。 (至少阅读一篇)

3)那个变量......“电子邮件”......您应该考虑将其称为“电子邮件”(按惯例)