PHP电子邮件表单格式

时间:2014-05-29 06:39:25

标签: php email format

在这里的一些用户的帮助下,我创建了一个表单,可以将数据(名称,复选框闪存)发送到我的电子邮件中。

但是,当我收到电子邮件时,它的格式错误,因此对我来说不是很有用。

我的代码如下:

PHP(mailer.php)

<?php
if(isset($_POST['submit'])) {

$to = "myemail@email.com";
$subject = "This is the subject";
$name_field = $_POST['name'];
$match_name= $_POST['match_name'];

$check_msg ="";
foreach($_POST['check'] as $value) {
$check_msg .= "Selected: $value\n";
}

$body = "From: $name_field\n $check_msg";
$body = "Match: $match_name\n $check_msg"; 

mail($to, $subject, $body);
echo "Data has been submitted to $to!";

} else {
echo "blarg!";
}
?>

HTML

<html>

<head>
<title>World Cup Challenge</title>
<style>
BODY{color:#000000; font-size: 8pt; font-family: Verdana}
.button {background-color: rgb(128,128,128); color:#ffffff; font-size: 8pt;}
.inputc {font-size: 8pt;}
.style3 {font-size: xx-small}
</style>
</head>

<form method="POST" action="mailer.php">
Name: <input type="text" name="name" size=""><br>
<br>

Brazil VS Croatia
<input type="hidden" name="match_name" value="Brazil VS Croatia"> 
<br>
<input type="checkbox" name="check[]" value="Brazil">Brazil<br>
<input type="checkbox" name="check[]" value="Croatia">Croatia<br>
<br>

Mexico VS Cameroon
<input type="hidden" name="match_name" value="Mexico VS Cameroon"> 
<br>
<input type="checkbox" name="check[]" value="Mexico">Mexico<br>
<input type="checkbox" name="check[]" value="Cameroon">Cameroon<br>
<br>

<input type="submit" value="Submit" name="submit">
</form>

有些背景,我正在为自己和几个朋友创建这个表格,以便为即将到来的世界杯选择我们的选秀权。我需要在那里发布50个匹配,他们可以为每个匹配选择。

当我收到电子邮件时,我希望将其格式化为:

**NAME OF USER** 

MATCH NAME 
SELECTED TEAM

NAME NAME #2 
SELECTED TEAM

等等,这个脚本目前有效 - 但只是格式错误。 (请原谅我,如果我格式错误,或者我的新手)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

试试这个:

HTML

<html>
<head>
<title>World Cup Challenge</title>
<style>
BODY{color:#000000; font-size: 8pt; font-family: Verdana}
.button {background-color: rgb(128,128,128); color:#ffffff; font-size: 8pt;}
.inputc {font-size: 8pt;}
.style3 {font-size: xx-small} 
</style>
</head>
<body>
<form method="POST" action="mailer.php">
Name: <input type="text" name="name" size=""><br>
<br>

Brazil VS Croatia
<input type="hidden" name="match_name[]" value="Brazil VS Croatia">
<br>
<input type="radio" name="check0[]" value="Brazil">Brazil<br>
<input type="radio" name="check0[]" value="Croatia">Croatia<br>
<br>

Mexico VS Cameroon
<input type="hidden" name="match_name[]" value="Mexico VS Cameroon">
<br>
<input type="radio" name="check1[]" value="Mexico">Mexico<br>
<input type="radio" name="check1[]" value="Cameroon">Cameroon<br>
<br>   

<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

PHP:

<?php
if(isset($_POST['submit'])) {

    $to         = "myemail@email.com";
    $subject    = "This is the subject";
    $name_field     = $_POST['name'];
    $i      = 0;

    $body       = "From: " . $name_field. "\n";

    foreach($_POST['match_name'] as $match_name) {

        $body .= "Match: " . $match_name . "\n";

        $check = "check".$i;

        foreach($_POST[$check] as $val) {

            $body .= "Selected Team: " . $val . "\n";       
        }

        $i++;
    }


    if (mail($to, $subject, $body)) {

        echo "Mail sent to: $to!";
    } else {

        echo "Sending mail failed";
    }

} else {
    echo "blarg!";
}
?>