php电子邮件是纯文本而不是HTML

时间:2014-08-19 17:40:45

标签: php html-email

我有一个表单,提交电子邮件并将表单值传递到电子邮件和附件中。电子邮件正在提交,附件正好通过,但它是用纯文本而不是HTML。您实际上可以看到电子邮件中的所有HTML标记,而不是呈现HTML。

到目前为止,我已经完成了相当多的搜索和反复试验而没有运气。关于我在这里做错了什么的想法:

PHP:

<?php

// if there is post
if(isset($_POST) && !empty($_POST) ) {

        // if thre is an attachment
            if(!empty($_FILES['attachment']['name'])) {

        // store some variables
        $file_name = $_FILES['attachment']['name'];
        $temp_name = $_FILES['attachment']['tmp_name'];
        $file_type = $_FILES['attachment']['type'];

        // get the extension of the file
        $base = basename($file_name);
        $extension = substr($base, strlen($base)-4, strlen($base));

// only allow these file types
$allowed_extensions = array(".doc", "docx", ".pdf", ".zip", ".csv", ".xls", "xlsx");

    // check that this file type is allowed
    if(in_array($extension,$allowed_extensions)) {

        // mail essentials
        $from = $_POST['email'];

        // multiple recipients
        $to  = 'email@email.com,'.$_POST['email'];



        // subject
        $today_day=date("d") ;
        $today_month=date("m") ;
        $today_year=date("Y") ;

        $subject = 'Professional Services Confirmation: '." Date and Time:       ".$_POST['ScheduledMonth']."/".$_POST['ScheduledDay']."/". $_POST['ScheduledYear'].' at '. $_POST['ScheduledHour'].":".$_POST['ScheduledMin']." ".$_POST['AMPM']." ".$_POST['TimeZone'];


// message
$message = '
<html>
<head>
<title>Professional Services Confirmation</title>
<style type="text/css">

body {
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    color:#434344;
    line-height:140%;
}

h1 {
    font-size:23px;
    color:#434344;
    font-weight:bold;
    margin-left:10px;
}

h2 {
    font-size:18px;
    color:#434344;
    font-weight:normal;
}

td {padding:5px 5px;}

.bg {background-color:#f1f1f1;}

.item {color:#666666; font-weight:bold;}
.content{
   height:auto; 
   width: 480px; 
   overflow: hidden;
   white-space: nowrap;
}



</style>
</head>
<body>
';
$message=$message.'
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0" align="center" style="border:1px solid #434344;">
  <tr>
  <td style="border-bottom:2px solid #434344;">
  <img src="" />
  </td>
  </tr>

  <tr>
  <td></td>
  </tr>

  <tr>
    <td width="680" height="382">';

$message=$message.'

<h1>Conference Call Confirmation</h1>';

$message=$message.'
<table width="680" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="4" valign="top"><h2>Client Information:</h2></td>
        </tr>


      <tr class="bg">
        <td valign="top" style="width:180px;"><p class="item">Name: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['name'];
$message = $message.'</td>
        </tr>  

                                <tr class="bg">
        <td valign="top" style="width:180px;"><p class="item">Company: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['company'];
$message = $message.'</td>
        </tr>      

      <tr class="bg">
        <td valign="top"><p class="item">Phone #: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['phone'];
$message = $message.'</td>
        </tr>      

      <tr>
        <td valign="top"><p class="item">Email: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['email'];
$message = $message.'</td>
        </tr>      

      <tr>
        <td valign="top"><p class="item">Account #: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['account_num'];
$message = $message.'</td>
        </tr>

      <tr class="bg">
        <td valign="top"><p class="item">Salesperson: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['salesperson'];
$message = $message.'</td>
        </tr>        
<tr>
<td colspan="4">&nbsp;</td>
</tr>

      <tr>
        <td colspan="4" valign="top"><h2>Conference Scheduling:</h2></td>
        </tr>


      <tr class="bg">
        <td valign="top"><p class="item">Event Date: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message.$_POST['ScheduledMonth']."/".$_POST['ScheduledDay']."/".    $_POST['ScheduledYear'];
$message = $message.'</td>
        </tr>
      <tr >
        <td valign="top"><p class="item">Event Time: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['ScheduledHour'].":".$_POST['ScheduledMin']." ".$_POST['AMPM'];
$message = $message.'</td>
        </tr>
      <tr class="bg">
        <td valign="top"><p class="item">Timezone: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['TimeZone'];
$message = $message.'</td>
        </tr>


      <tr>
        <td valign="top"><p class="item">Duration of Call: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['duration'];
$message = $message.'</td>
        </tr>

      <tr class="bg">
        <td valign="top"><p class="item">Lines Reserved: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['lines_reserved'];
$message = $message.'</td>
        </tr>

      <tr>
        <td valign="top"><p class="item">Project ID: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['project_id'];
$message = $message.'</td>
        </tr>

      <tr class="bg">
        <td valign="top"><p class="item">Call Title / Topic: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['call_title_topic'];
$message = $message.'</td>
        </tr>                               

      <tr>
        <td valign="top"><p class="item">Moderators / Host Name(s): </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['moderators'];
$message = $message.'</td>
        </tr>       

      <tr class="bg">
        <td valign="top"><p class="item">Speaker Name(s): </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['speakers'];
$message = $message.'</td>
        </tr>           

      <tr>
        <td valign="top"><p class="item">Fields to be collected <br /> from participants: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['collect_fields'];
$message = $message.'</td>
        </tr>                                       

      <tr class="bg">
        <td valign="top"><p class="item">Seperate Host / Speaker <br /> dial-in # needed: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['seperate_dialin'];
$message = $message.'</td>
        </tr>                                       

      <tr>
        <td valign="top"><p class="item">Special Script Required: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['special_script'];
$message = $message.'</td>
        </tr>       

      <tr>
        <td valign="top"><p class="item">Script: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content">';
$message = $message. $_POST['script'];
$message = $message.'</td>
        </tr>   

      <tr class="bg">
        <td valign="top"><p class="item">Uploads: </p></td>
        <td valign="top"></td>
        <td colspan="2" valign="top" class="content"><p>Please Note that all uploads are located on our Dropbox account under the folder named "Event_Items"</p></td>
        </tr>                                               


      ';

$message = $message.'         
      <tr> 
      <td colspan="4" valign="top"><span class="style5"></span></td>
      </tr>

      '; 
$message = $message.' 
    </table>    

    <br />
    </td>
  </tr>
</table>


 <p>&nbsp;</p>
</body>
</html>

                ';


    // things you need
    $file = $temp_name;
    $content = chunk_split(base64_encode(file_get_contents($file)));
    $uid = md5(uniqid(time()));     

    //standard mail headers
    $header = "From: ".$from."\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";



    // declaring we have multiple parts of email (i.e plain text and attachment)
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";

    // message part
    $header .= "--".$uid."\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    // file attachment part
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment filename=\"".$file_name."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";



    //send the mail
    if (mail($to, $subject, "", $header)) {
    //redirect to the thank you page
    header('Location: http://www.somesite.com/thankyou.php');
        } else {
            echo "Fail";
            }

    } else {
        echo "file type not allowed";
}

 } else {
    echo "no file posted";
  }
 }
  ?>

1 个答案:

答案 0 :(得分:1)

这一行打破了你的标题:

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
       ^

它包含s复数)=&gt; $headers,将其更改为$header

所有其他标头变量都是(单数$header


另外,如果我可以引用迈克尔的评论

&#34;我认为您在第一个MIME边界之前也错过了第二个换行符:
$header .= "This is a multi-part message in MIME format.\r\n";
应以\r\n\r\n&#34;

结尾

如果您仍然遇到问题。但是,根据迈克尔的补充评论,这可能不是必需的。感谢Michael对此进行检查。