Aus html-Formular Daten an Email senden

4 Antworten

Vom Fragesteller als hilfreich ausgezeichnet

Hier der komplette Code (ist getestet und funktioniert):

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Form Style 8</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
<script type="text/javascript">
//auto expand textarea
function adjust_textarea(h) {
    h.style.height = "45px";
    h.style.height = (h.scrollHeight)+"px";
}
</script>
<style>
body{

}
.form-style-8{
    font-family: 'Open Sans Condensed', arial, sans;
    width: 500px;
    padding: 30px;
    background: #FFFFFF;
    margin: 50px auto;
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
    -moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
    -webkit-box-shadow:  0px 0px 15px rgba(0, 0, 0, 0.22);

}
.form-style-8 h2{
    background: #4D4D4D;
    text-transform: uppercase;
    font-family: 'Open Sans Condensed', sans-serif;
    color: #797979;
    font-size: 18px;
    font-weight: 100;
    padding: 20px;
    margin: -30px -30px 30px -30px;
}
.form-style-8 input[type="text"],
.form-style-8 input[type="date"],
.form-style-8 input[type="datetime"],
.form-style-8 input[type="email"],
.form-style-8 input[type="number"],
.form-style-8 input[type="search"],
.form-style-8 input[type="time"],
.form-style-8 input[type="url"],
.form-style-8 input[type="password"],
.form-style-8 textarea,
.form-style-8 select 
{
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    outline: none;
    display: block;
    width: 100%;
    padding: 7px;
    border: none;
    border-bottom: 1px solid #ddd;
    background: transparent;
    margin-bottom: 10px;
    font: 16px Arial, Helvetica, sans-serif;
    height: 45px;
}
.form-style-8 textarea{
    resize:none;
    overflow: hidden;
}
.form-style-8 input[type="button"], 
.form-style-8 input[type="submit"]{
    -moz-box-shadow: inset 0px 1px 0px 0px #45D6D6;
    -webkit-box-shadow: inset 0px 1px 0px 0px #45D6D6;
    box-shadow: inset 0px 1px 0px 0px #45D6D6;
    background-color: #2CBBBB;
    border: 1px solid #27A0A0;
    display: inline-block;
    cursor: pointer;
    color: #FFFFFF;
    font-family: 'Open Sans Condensed', sans-serif;
    font-size: 14px;
    padding: 8px 18px;
    text-decoration: none;
    text-transform: uppercase;
}
.form-style-8 input[type="button"]:hover, 
.form-style-8 input[type="submit"]:hover {
    background:linear-gradient(to bottom, #34CACA 5%, #30C9C9 100%);
    background-color:#34CACA;
}
</style>
</head>

<body>

<?php
    if (isset($_POST[send])) {
        $senderName = "Zyogen";
        $senderAddress = "zyogen@example.com";
        $recipient = "destination@example.com";
        $subject = "Neue Nachricht";
        $message = "Name: " . $_POST[field1] . "\nEmail: " . $_POST[field2] . "\nKlasse: " . $_POST[field3] . "\nText: " . $_POST[field4];
        # Composing the header
        $header = "From: " . $senderName . "<" . $senderAddress . ">" . "\n";
        $header .= "Content-Type: text/plain; charset=utf-8" . "\n";
        # Sending the mail
        if (mail($recipient, $subject, $message, $header, "-f" . $senderAddress)) {
            echo "Mail gesendet.";
        } else {
            echo "Fehler beim Senden der Mail.";
        }
    }
?>

<div class="form-style-8">
    <h2>Kontakt</h2>
    <form action="email.php" method="post">
        <input type="text" name="field1" placeholder="Name" />
        <input type="email" name="field2" placeholder="Email-Adresse" />
        <input type="text" name="field3" placeholder="Klasse" />
        <textarea placeholder="Nachricht" onkeyup="adjust_textarea(this)" name="field4"></textarea>
        <input type="submit" name="send" value="Senden" />
    </form>
</div>

</body>
</html>

Im Formular muss statt GET die POST-Methode verwendet werden und die Textarea und der Submit-Button benötigen einen Namen. Mit dem PHP-Code wird abgefragt, ob Submit geklickt wurde und wenn ja, wird die Mail zusammengesetzt und verschickt. Der Server muss dazu in der Lage sein, Mails zu verschicken, z.B. per Postfix.


DennisBc 
Fragesteller
 16.02.2015, 17:26

Vielen Dank erst mal :) meine Email muss dann recipient sein oder? Was kommt dann bei senderAdress rein?

0
acdxx  16.02.2015, 17:28
@DennisBc

Ja richtig. Die Absenderadresse ist im Prinzip frei wählbar, aber ich würde z.B. info@deinedomain.de o.ä. empfehlen

0
DennisBc 
Fragesteller
 16.02.2015, 17:56
@acdxx

Danke schön. Dann werd ich mir jetzt als erstes eine Email bei meinem Hoster anlegen.

0
acdxx  16.02.2015, 18:11
@DennisBc

Die Adresse muss nicht unbedingt funktionsfähig/existent sein. Ich weiß jetzt allerdings nicht, ob das auf einfachen Hosting-Servern überhaupt funktioniert, da die PHP-Mailfunktion standardmäßig das lokal installierte Programm sendmail verwendet. Ich habe es auf einem selbstverwalteten Rootserver getestet - da geht das natürlich. Im Zweifel mal beim Support des Hosters nachfragen, ob die PHP-Mailfunktion genutzt werden kann.

0
DennisBc 
Fragesteller
 16.02.2015, 18:20
@acdxx

Habe jetzt alles fertig gemacht und hochgeladen, allerding kommt keine Email an (auch nicht in Spam/Junk), zweitens ist über dem Formular jetzt Text, bekommt man den noch weg? Und wenn ich auf Senden klicke komme ich auf die Homepage meines Hostingservice.

0
acdxx  16.02.2015, 20:31
@DennisBc

Die Echo Ausgaben (gesendet bzw. Fehler) kann man weglassen, das war nur zu Testzwecken. Und bei der Form Action muss wieder der Name deiner PHP-Datei eingetragen werden.

Aber wie gesagt, ich könnte mir vorstellen, dass das direkte Mailen bei einfachen Hosting-Services nicht funktioniert. Frag das mal beim Provider nach. Wenn der mail Befehl gesperrt ist, müsste man die Mail nicht über den Server selbst verschicken, sondern per SMTP über ein externes Mailkonto. Da kann ich allerdings nicht helfen.

0

Das ist das Formular

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Form Style 8</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
<script type="text/javascript">
//auto expand textarea
function adjust_textarea(h) {
    h.style.height = "45px";
    h.style.height = (h.scrollHeight)+"px";
}
</script>
<style>
body{

}
.form-style-8{
    font-family: 'Open Sans Condensed', arial, sans;
    width: 500px;
    padding: 30px;
    background: #FFFFFF;
    margin: 50px auto;
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
    -moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
    -webkit-box-shadow:  0px 0px 15px rgba(0, 0, 0, 0.22);

}
.form-style-8 h2{
    background: #4D4D4D;
    text-transform: uppercase;
    font-family: 'Open Sans Condensed', sans-serif;
    color: #797979;
    font-size: 18px;
    font-weight: 100;
    padding: 20px;
    margin: -30px -30px 30px -30px;
}
.form-style-8 input[type="text"],
.form-style-8 input[type="date"],
.form-style-8 input[type="datetime"],
.form-style-8 input[type="email"],
.form-style-8 input[type="number"],
.form-style-8 input[type="search"],
.form-style-8 input[type="time"],
.form-style-8 input[type="url"],
.form-style-8 input[type="password"],
.form-style-8 textarea,
.form-style-8 select 
{
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    outline: none;
    display: block;
    width: 100%;
    padding: 7px;
    border: none;
    border-bottom: 1px solid #ddd;
    background: transparent;
    margin-bottom: 10px;
    font: 16px Arial, Helvetica, sans-serif;
    height: 45px;
}
.form-style-8 textarea{
    resize:none;
    overflow: hidden;
}
.form-style-8 input[type="button"], 
.form-style-8 input[type="submit"]{
    -moz-box-shadow: inset 0px 1px 0px 0px #45D6D6;
    -webkit-box-shadow: inset 0px 1px 0px 0px #45D6D6;
    box-shadow: inset 0px 1px 0px 0px #45D6D6;
    background-color: #2CBBBB;
    border: 1px solid #27A0A0;
    display: inline-block;
    cursor: pointer;
    color: #FFFFFF;
    font-family: 'Open Sans Condensed', sans-serif;
    font-size: 14px;
    padding: 8px 18px;
    text-decoration: none;
    text-transform: uppercase;
}
.form-style-8 input[type="button"]:hover, 
.form-style-8 input[type="submit"]:hover {
    background:linear-gradient(to bottom, #34CACA 5%, #30C9C9 100%);
    background-color:#34CACA;
}
</style>
</head>

<body>

<div class="form-style-8">
  <h2>Kontakt</h2>
 <form action="hier stand meine php Datei" method="get">
    <input type="text" name="field1" placeholder="Name" />
    <input type="email" name="field2" placeholder="Email-Adresse" />
    <input type="text" name="field3" placeholder="Klasse" />
    <textarea placeholder="Nachricht" onkeyup="adjust_textarea(this)"></textarea>
    <input type="submit" value="Senden" />
  </form>
</div>

</body>
</html>

unabhängig vom eigentlichen Problem ein kleiner Hinweis. Das php-eigene mail() sollte nicht verwendet werden, da dort durchaus verschiedene Probleme auftauchen könnten (Email wird als Spam erkannt, es gibt Darstellungsprobleme oder die Mail kommt auch mal gar nicht an, weil die Header nicht korrekt sind). Günstiger ist es eine Mailer-Klasse zu verwenden.

Lies dazu bitte mal hinter folgendem Link:

http://www.robo47.net/text/38-Mail-ist-tot-es-lebe-mail

Dort findest Du auch gleich Downloads zu verschiedenen Mailer-Klassen und entsprechende Beispiele

<form action="hier stand meine php Datei"...

Kannst Du mit Deinem Webspace überhaupt php nutzen?


DennisBc 
Fragesteller
 16.02.2015, 16:51

Laut dem Anbieter kann ich nutzen: PHP (VERSIONEN 5.2, 5.3, 5.4)

0
Zyogen  16.02.2015, 16:54
@DennisBc

Dann prüfe einmal folgendes: Erstelle eine Datei

<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
phpinfo( );
?>

Lade sie hoch, rufe sie im Browser auf und schau Dir das Ergebnis an.

1
DennisBc 
Fragesteller
 16.02.2015, 17:13
@Zyogen

Dann sehe ich den Inhalt der Datei

0
acdxx  16.02.2015, 20:36
@DennisBc

Da darf kein Leerzeichen zwischen den Klammern sein. Hier der korrekte Code:

<html>

<head>
    <title>PHP Test</title>
</head>

<body>
    <?php
        phpinfo();
    ?>
</body>

</html>
0
SiedlerProfi  17.03.2015, 16:47

Wieso antwrtest du auf fragen, wenn du dich gar nicht auskennst?

1