无法通过PHP发送HTML电子邮件

时间:2014-02-12 08:03:21

标签: php html-email

我正在尝试在订单提交时发送HTML电子邮件,因此首先我读了数据库中最后插入的行

if(isset($_POST['submit'])) {

    $sql2 = "SELECT * FROM `$db`.`pre_order` ORDER BY id DESC LIMIT 0,1;";
    $result = mysql_query($sql2, $dbcon);
    $row = mysql_fetch_array($result);
    $id= $row['id'];
    $name=$row['name'];
    $address=$row['delivery_location'];
    $mobile =$row['mobile'];
    $email= $row['email'];
    $to = $email;
    $from = "info@sundoriagro.com"; 
    $subject = "Thanks You message";    

    $message = '

                <html>
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <title>Sundori Honey</title>
                        <link rel="stylesheet" href="css/table.css" type="text/css">
                    </head>
                    <body style="width:600px;">
                        <div class="begining">  
                            <p>
                                Dear <span> <?php echo $name?></span> <br/>

                Thank you for your booking. We are pleased to confirm detail as follows:

                            </p>
                        </div>
                        <table border="1" style="width:80% ;margin: auto;">

                            <tr>
                                <td>Confirmation Number: </td>
                                <td> <?php echo $id;?></td>
                            </tr>
                            <tr>
                                <td>Client’s Name & Contact No:</td>
                                <td><?php echo $name .' and ' .$mobile;?></td>
                            </tr><tr>
                                <td> Contact Address:</td>
                                <td><?php echo $address;?></td>
                            </tr>




                        </table>
                            <div class="begining">  
                            <p>

    Thanks
                            Sincerely,<br/><br/>

                Johm<br/><br/>
                Manager – Marketing & Sales<br/>
                </p>
                        </div>
                    </body>

                </html>
                '; /*---end of HTML msg--*/


    $headers  = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 
    mail($to, $subject, $message, $headers); 

提交表单后,我确实收到了一个消息,但问题是,它只显示类似

的内容

http://i61.tinypic.com/33zbfjl.jpg

邮件没有显示我从数据库中读取的任何值,它只显示表的两行,即使它没有显示位于表下方的页脚文本。

6 个答案:

答案 0 :(得分:4)

变量的值不会在单引号下解析!

你有这个$message变量..

 $message = '....... your content .....';

用这样的双引号括起来..

$message = " ..... your content ......';

否则,您可以使用HEREDOC语法。

HEREDOC变量的 $message 语法。

$message=<<<HTMLMAIL

.... your content..........
// more content....
HTMLMAIL;

将所有内容粘贴到$message变量中,并将其放在HEREDOC

答案 1 :(得分:1)

替换您的$ message值。

$message = '

                <html>
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <title>Sundori Honey</title>
                        <link rel="stylesheet" href="css/table.css" type="text/css">
                    </head>
                    <body style="width:600px;">
                        <div class="begining">  
                            <p>
                                Dear <span>'.$name.'</span> <br/>

                Thank you for your booking. We are pleased to confirm detail as follows:

                            </p>
                        </div>
                        <table border="1" style="width:80% ;margin: auto;">

                            <tr>
                                <td>Confirmation Number: </td>
                                <td>'. $id.'</td>
                            </tr>
                            <tr>
                                <td>Client’s Name & Contact No:</td>
                                <td>'. $name .' and ' .$mobile .'</td>
                            </tr><tr>
                                <td> Contact Address:</td>
                                <td>'. $address .'</td>
                            </tr>




                        </table>
                            <div class="begining">  
                            <p>

    Thanks
                            Sincerely,<br/><br/>

                Johm<br/><br/>
                Manager – Marketing & Sales<br/>
                </p>
                        </div>
                    </body>

                </html>
                '; 

答案 2 :(得分:1)

将此代码用于$ message变量

$message = '<html>
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <title>Sundori Honey</title>
                    <link rel="stylesheet" href="css/table.css" type="text/css">
                </head>
                <body style="width:600px;">
                    <div class="begining">  
                        <p>
                            Dear <span>'.$name.'</span> <br/>

            Thank you for your booking. We are pleased to confirm detail as follows:

                        </p>
                    </div>
                    <table border="1" style="width:80% ;margin: auto;">

                        <tr>
                            <td>Confirmation Number: </td>
                            <td>'.$id.'</td>
                        </tr>
                        <tr>
                            <td>Client’s Name & Contact No:</td>
                            <td>'.$name .' and ' .$mobile.'</td>
                        </tr><tr>
                            <td> Contact Address:</td>
                            <td>'.$address.'</td>
                        </tr>




                    </table>
                        <div class="begining">  
                        <p>

Thanks
                        Sincerely,<br/><br/>

            Johm<br/><br/>
            Manager – Marketing & Sales<br/>
            </p>
                    </div>
                </body>

            </html>'; 

答案 3 :(得分:0)

您的代码中有很多错误。您在不需要的地方使用了<?php echo ?>个标签

你不想再在php中指定内容。您只是将这些html内容存储为字符串。所以将其从$message中删除

更改此

Dear <span> <?php echo $name?></span> <br/>

Dear <span>  $name</span> <br/>

同时使用双打时不要忘记将html属性中的引号更改为单引号

所以将整个$ message更改为此

$message = "

                <html>
                    <head>
                        <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
                        <title>Sundori Honey</title>
                        <link rel='stylesheet' href='css/table.css' type='text/css'>
                    </head>
                    <body style='width:600px;'>
                        <div class='begining'>  
                            <p>
                                Dear <span>$name</span> <br/>

                Thank you for your booking. We are pleased to confirm detail as follows:

                            </p>
                        </div>
                        <table border='1' style='width:80% ;margin: auto;'>

                            <tr>
                                <td>Confirmation Number: </td>
                                <td> $id</td>
                            </tr>
                            <tr>
                                <td>Client’s Name & Contact No:</td>
                                <td> $name  and $mobile </td>
                            </tr><tr>
                                <td> Contact Address:</td>
                                <td> $address </td>
                            </tr>




                        </table>
                            <div class='begining'>  
                            <p>

    Thanks
                            Sincerely,<br/><br/>

                Johm<br/><br/>
                Manager – Marketing & Sales<br/>
                </p>
                        </div>
                    </body>

                </html>
                "; 

答案 4 :(得分:0)

- 不要在php中使用php标签

- 你必须用点(。)

连接“name”和“adresse”

喜欢这个

  <tr>
       <td>Client’s Name & Contact No:</td>
         <td>'.$name .' and ' .$mobile.'</td>
       </tr><tr>
       <td> Contact Address:</td>
       <td>'.$address.'</td>
  </tr>

答案 5 :(得分:0)

试试这个:

$message = '

        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Sundori Honey</title>
                <link rel="stylesheet" href="css/table.css" type="text/css">
            </head>
            <body style="width:600px;">
                <div class="begining">  
                    <p>
                        Dear <span>' . $name . '</span> <br/>

        Thank you for your booking. We are pleased to confirm detail as follows:

                    </p>
                </div>
                <table border="1" style="width:80% ;margin: auto;">

                    <tr>
                        <td>Confirmation Number: </td>
                        <td>' . $id . '</td>
                    </tr>
                    <tr>
                        <td>Client’s Name & Contact No:</td>
                        <td>' . $name .' and ' . $mobile . '</td>
                    </tr><tr>
                        <td> Contact Address:</td>
                        <td>' . $address . '</td>
                    </tr>




                </table>
                    <div class="begining">  
                    <p>Thanks
                    Sincerely,<br/><br/>

        Johm<br/><br/>
        Manager – Marketing & Sales<br/>
        </p>
                </div>
            </body>

        </html>
        ';