从php发送html电子邮件

时间:2015-04-20 12:32:00

标签: php html css

我正在尝试从PHP发送一个简单的HTML电子邮件。 所以我创建一个HTML表单和一个PHP脚本发送它,电子邮件发送没有问题,但当我在gmail中检查这封电子邮件我得到这个身体不是一个表所有我所拥有的是带有信息的html标签

我的php脚本:

<?php
    $name = $_POST['txtfullname'];
    $email = $_POST['txtemail'];
    $comments = $_POST['Comments'];
    $phone = $_POST['optmobileno'];
    $phone .= $_POST['txtmobileno'];
    $date = $_POST['birthdate'];
    $country = $_POST['optcountry'];
    $level= $_POST['optprogrammelevel'];
    $programme= $_POST['txtprogrammepreference'];
    $from = "From: subscription@sendmail.com"; 
    $to = "example@sendmail.com"; 
    $subject = 'New Students';

    $message = '<html><body>';
            $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
            $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($name) . "</td></tr>";
            $message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($email) . "</td></tr>";
            $message .= "<tr><td><strong>Phone:</strong> </td><td>" . strip_tags($phone) . "</td></tr>";
            $message .= "<tr><td><strong>Birthdate:</strong> </td><td>" . strip_tags($date) . "</td></tr>";
            $message .= "<tr><td><strong>Contry of residence:</strong> </td><td>" . strip_tags($country) . "</td></tr>";
            $message .= "<tr><td><strong>Programme Level:</strong> </td><td>" . strip_tags($level) . "</td></tr>";
            $message .= "<tr><td><strong>Area of Study:</strong> </td><td>" . strip_tags($programme) . "</td></tr>";
            $message .= "<tr><td><strong>Comments:</strong> </td><td>" . htmlentities($comments) . "</td></tr>";
            $message .= "</table>";
            $message .= "</body></html>";
    $headers = 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();        


      @mail ($to, $subject, $message , $headers)


?>

2 个答案:

答案 0 :(得分:0)

您需要在电子邮件标题中添加content-type html,如下所示:

$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

 $headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

答案 1 :(得分:0)

您需要在代码中添加标题

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

这里使用的是refrence