尝试通过HTML电子邮件发送PHP表单数据

时间:2013-05-28 19:41:21

标签: php html forms email

我正在尝试编写表单/作业应用程序,但无法确定如何发送它。我已经知道如何通过电子邮件发送所有信息。

我想要做的是通过电子邮件发送信息,但我希望电子邮件能够显示html编码,以便更好地整理数据。我已经在PHP中声明了所有内容我只是决定消息:

<?php

$to = "jobs@enhstudios.com";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$address = $_POST['address'];
$aptno = $_POST['aptno'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$phonetype = $_POST['phonetype'];
$jdate1 = $_POST['jdate1'];
$employer1 = $_POST['employer1'];
$title1 = $_POST['title1'];
$leaving1 = $_POST['leaving1'];
$jdate2 = $_POST['jdate2'];
$employer2 = $_POST['employer2'];
$title2 = $_POST['title2'];
$leaving2 = $_POST['leaving2'];
$jdate3 = $_POST['jdate3'];
$employer3 = $_POST['employer3'];
$title3 = $_POST['title3'];
$leaving3 = $_POST['leaving3'];
$skills = $_POST['skills'];
$subject = $fname." ".$lname."Application";
$message = 
$from = $email;
mail($to,$subject,$message);

?>

我想在$ message中写一个表,并将数据显示在一个漂亮整洁的表中。

3 个答案:

答案 0 :(得分:0)

是的,建立一个HTML表格,然后适当地设置$headers(参见示例#4:http://php.net/manual/en/function.mail.php)。

答案 1 :(得分:0)

以下是我对PHP背面的整个编码。我确实收到了电子邮件...但是,它全部在HTML代码中,并没有提取任何信息

<?php

$to = "jobs@enhstudios.com";
$email = $_POST['email'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$address = $_POST['address'];
$aptno = $_POST['aptno'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$phonetype = $_POST['phonetype'];
$jobtype1 = $_POST['jobtype1'];
$jobtype2 = $_POST['jobtype2'];
$jdate1 = $_POST['jdate1'];
$edate1 = $_POSt['edate1'];
$employer1 = $_POST['employer1'];
$title1 = $_POST['title1'];
$leaving1 = $_POST['leaving1'];
$jdate2 = $_POST['jdate2'];
$edate2 = $_POSt['edate2'];
$employer2 = $_POST['employer2'];
$title2 = $_POST['title2'];
$leaving2 = $_POST['leaving2'];
$jdate3 = $_POST['jdate3'];
$edate3 = $_POSt['edate3'];
$employer3 = $_POST['employer3'];
$title3 = $_POST['title3'];
$leaving3 = $_POST['leaving3'];
$skills = $_POST['skills'];
$subject = $fname." ".$lname. " Application";
$message = 
'<html>
<head>
</head>
<body>
<table border="2" cellpadding="4" cellspacing="3">
<tr>
<th colspan="5">Personal Information:</th>
</tr>
<tr>
<td colspan="2">First Name:</td>
<td colspan="2">Last Name:</td>
<td>Age:</td>
</tr>
<tr>
<td colspan="2">
    <?php echo $_POST["fname"]; ?>
</td>
<td colspan="2">
    <?php echo $_POST["lname"]; ?>
</td>
<td>
    <?php echo $_POST["age"]; ?>
</td>
</tr>
<tr>
<td>Street:</td>
<td>Apt/Suite</td>
<td>City:</td>
<td>State:</td>
<td>Zip Code:</td>
</tr>
<tr>
<td>
    <?php echo $_POST["address"]; ?>
</td>
<td>
    <?php echo $_POST["aptno"]; ?>
</td>
<td>
    <?php echo $_POST["city"]; ?>
</td>
<td>
    <?php echo $_POST["state"]; ?>
</td>
<td>
    <?php echo $_POST["zip"]; ?>
</td>
</tr>
<tr>
<td colspan="2">Phone Number:</td>
<td>Phone Type:</td>
<td></td>
<td></td>
<tr>
<td colspan="2">
    <?php echo $_POST["phone"]; ?>
</td>
<td>
    <?php echo $_POST["phonetype"]; ?>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="2">Please select job(s) you are applying for:</td>
<td></td>
<td></td>
<td></td>
<tr>
    <td>
          <?php echo $_POST["jobtype1"]; ?>
    </td>
    <td>
          <?php echo $_POST["jobtype2"]; ?>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
</tr>
<tr>
<th colspan="5">Employment History:</th>
</tr>
<tr>
<td>Month/Year Start:</td>
<td>Month/Year End:</td>
<td>Employer:</td>
<td>Title/Position:</td>
<td>Reason For Leaving:</td>
</tr>
<tr>
<td>
    <?php echo $_POST["jdate1"]; ?>
</td>
<td>
    <?php echo $_POST["edate1"]; ?>
</td>
<td>
    <?php echo $_POST["employer1"]; ?>
</td>
<td>
    <?php echo $_POST["title1"]; ?>
</td>
<td>
    <?php echo $_POST["leaving1"]; ?>
</td>
</tr>
<tr>
<td>
    <?php echo $_POST["jdate2"]; ?>
</td>
<td>
    <?php echo $_POST["edate2"]; ?>
</td>
<td>
    <?php echo $_POST["employer2"]; ?>
</td>
<td>
    <?php echo $_POST["title2"]; ?>
</td>
<td>
    <?php echo $_POST["leaving2"]; ?>
</td>
</tr>
<tr>
<td>
    <?php echo $_POST["jdate3"]; ?>
</td>
<td>
    <?php echo $_POST["edate3"]; ?>
</td>
<td>
    <?php echo $_POST["employer3"]; ?>
</td>
<td>
    <?php echo $_POST["title3"]; ?>
</td>
<td>
    <?php echo $_POST["leaving3"]; ?>
</td>
</tr>
<tr>
<th colspan="5">Special Skills:</th>
</tr>
<tr>
<td colspan="5">
    <?php echo $_POST["skills"]; ?>
</td>
</tr>
</table>
</body>
</html>';
$from = $email;
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ENH Studios</title>
<link rel="stylesheet" type="text/css" href="careerstyle.css" media="screen" />
</head>
<body>
<div id="header">
<div id="logo">
</div>

<div id="intro">

    <p><?php echo $_POST["fname"]; ?>&nbsp<?php echo $_POST["lname"]; ?>'s Application</p>
</div>
</div>
<div id="content">
<p>Thank you for your interest in working with ENH Studios, we will review your application and contact you to set up an interview</p>
<p>We interview EVERYONE who applies with us</p>
<table border="2" cellpadding="4" cellspacing="3">
<tr>
<th colspan="5">Personal Information:</th>
</tr>
<tr>
<td colspan="2">First Name:</td>
<td colspan="2">Last Name:</td>
<td>Age:</td>
</tr>
<tr>
<td colspan="2">
    <?php echo $_POST["fname"]; ?>
</td>
<td colspan="2">
    <?php echo $_POST["lname"]; ?>
</td>
<td>
    <?php echo $_POST["age"]; ?>
</td>
</tr>
<tr>
<td>Street:</td>
<td>Apt/Suite</td>
<td>City:</td>
<td>State:</td>
<td>Zip Code:</td>
</tr>
<tr>
<td>
    <?php echo $_POST["address"]; ?>
</td>
<td>
    <?php echo $_POST["aptno"]; ?>
</td>
<td>
    <?php echo $_POST["city"]; ?>
</td>
<td>
    <?php echo $_POST["state"]; ?>
</td>
<td>
    <?php echo $_POST["zip"]; ?>
</td>
</tr>
<tr>
<td colspan="2">Phone Number:</td>
<td>Phone Type:</td>
<td></td>
<td></td>
<tr>
<td colspan="2">
    <?php echo $_POST["phone"]; ?>
</td>
<td>
    <?php echo $_POST["phonetype"]; ?>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="2">Please select job(s) you are applying for:</td>
<td></td>
<td></td>
<td></td>
<tr>
    <td>
          <?php echo $_POST["jobtype1"]; ?>
    </td>
    <td>
          <?php echo $_POST["jobtype2"]; ?>
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
</tr>
<tr>
<th colspan="5">Employment History:</th>
</tr>
<tr>
<td>Month/Year Start:</td>
<td>Month/Year End:</td>
<td>Employer:</td>
<td>Title/Position:</td>
<td>Reason For Leaving:</td>
</tr>
<tr>
<td>
    <?php echo $_POST["jdate1"]; ?>
</td>
<td>
    <?php echo $_POST["edate1"]; ?>
</td>
<td>
    <?php echo $_POST["employer1"]; ?>
</td>
<td>
    <?php echo $_POST["title1"]; ?>
</td>
<td>
    <?php echo $_POST["leaving1"]; ?>
</td>
</tr>
<tr>
<td>
    <?php echo $_POST["jdate2"]; ?>
</td>
<td>
    <?php echo $_POST["edate2"]; ?>
</td>
<td>
    <?php echo $_POST["employer2"]; ?>
</td>
<td>
    <?php echo $_POST["title2"]; ?>
</td>
<td>
    <?php echo $_POST["leaving2"]; ?>
</td>
</tr>
<tr>
<td>
    <?php echo $_POST["jdate3"]; ?>
</td>
<td>
    <?php echo $_POST["edate3"]; ?>
</td>
<td>
    <?php echo $_POST["employer3"]; ?>
</td>
<td>
    <?php echo $_POST["title3"]; ?>
</td>
<td>
    <?php echo $_POST["leaving3"]; ?>
</td>
</tr>
<tr>
<th colspan="5">Special Skills:</th>
</tr>
<tr>
<td colspan="5">
    <?php echo $_POST["skills"]; ?>
</td>
</tr>
</table>
</body>
</html>

答案 2 :(得分:-1)

要发送HTML邮件,必须设置Content-type标头

$ headers ='MIME-Version:1.0'。为 “\ r \ n” 个;

$ headers。='Content-type:text / html; charset = iso-8859-1'。为 “\ r \ n” 个;

 //Here is a example how to use   


<?php
        // to email
        $to  = 'test@example.com';

        // subject
        $subject = 'Birthday Reminders for August';

        // message
        $message = '
        <html>
        <head>
          <title>Birthday Reminders for August</title>
        </head>
        <body>
          <p>Here are the birthdays upcoming in August!</p>
          <table>
            <tr>
              <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
            </tr>
            <tr>
              <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
            </tr>
            <tr>
              <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
            </tr>
          </table>
        </body>
        </html>
        ';

        // To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        // Additional headers
        $headers .= 'To: Mary <test@example.com>' . "\r\n";
        $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";


        // Mail it
        if(mail($to, $subject, $message, $headers))
          {
           echo "mail sent sucessfully";
          }
         else
          {
           echo "Unable to send mail";
          }
?>