【问题标题】:How to send HTML Dynamic Table as mail in php?如何在 php 中将 HTML 动态表作为邮件发送?
【发布时间】:2013-01-20 07:44:39
【问题描述】:

这里我有一个代码,比如从 index.php 获取数据,如果用户输入数据,它将显示为表格,并且它应该通过电子邮件发送到一些预定义的邮件。但是在这里我动态地获取数据,我尝试使用 $_REQUEST 和 $_POST 两种方法来获取数据,但是在邮件功能中我试图更改消息参数但是通过 php 标签它没有采用并显示一些语法错误。

请在此处查看代码

<?php
$to="xxxxxxx@gmail.com";
$fn="Fisrt Name";
$ln="Last Name";
$name=$fn.' '.$ln;
$from="xxxxx@xxx.com";
$subject = "Welcome to Website";

include('newsmtp/smtpwork.php');

 ?>
 <?php

 $message = 'Dear $firstName, 


 Your Welcome Message.'.'
 <table border=1>

    <tr>
        <td>First Name:</td>
        <td><?php $firstName=$_POST['firstname'];
        echo $firstName;?></td>
    </tr>

    <tr>
        <td>Last Name:</td>
        <td><?php $lastname=$_POST['lastname'];
        echo $lastname;?></td>

    </tr>

    <tr>

        <td>Title:</td>
        <td><?php $title=$_POST['title'];
        echo $title;?></td>
    </tr>

    <tr>

        <td>Address:</td>
        <td><?php $address=$_POST['address'];
        echo $address;?></td>
    </tr>

    <tr>

        <td>Phone Number:</td>
        <td><?php $phone=$_POST['phone'];
        echo $phone;?></td>
    </tr>


    <tr>

        <td>Course Name:</td>
        <td><?php $course=$_POST['coursename'];
        echo $course;?></td>

    </tr>

    <tr>

        <td>Website:</td>
        <td><?php $website=$_POST['website'];
        echo $website;?></td>

    </tr>

</table>

Thanks
xxxxxxxxxxxx
';
?> 

【问题讨论】:

标签: php javascript jquery html email


【解决方案1】:

试试这个方法,在 $message 变量之外的变量中分配发布的值,并在 $message 变量中打印变量

$firstname  = $_POST['firstname'];
$lastname   = $_POST['lastname'];
$title      = $_POST['title'];
$address    = $_POST['address'];
$phone      = $_POST['phone'];
$course     = $_POST['course'];
$website    = $_POST['website'];


    $message = 'Dear '.$firstName.', 
     Your Welcome Message.'.'
     <table border=1>
        <tr>
            <td>First Name:</td>
            <td>'.$firstname.' </td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td>'.$lastname.'</td>
        </tr>
     <tr>
        <td>Title:</td>
        <td>'.$title.'</td>
    </tr>
    <tr>
        <td>Address:</td>
        <td>'.$address.'</td>
    </tr>
    <tr>
        <td>Phone Number:</td>
        <td>'.$phone.'</td>
    </tr>
    <tr>
        <td>Course Name:</td>
        <td>'.$course.'</td>
    </tr>
    <tr>
        <td>Website:</td>
        <td>'.$website.'</td>
    </tr>
</table>
Thanks
xxxxxxxxxxxx
';
?> 




$to       ="xxxxxxx@gmail.com";
$subject  = "Welcome to Website";
$headers  = 'From: xxxxxx <noreply@xxxxxxx.com>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n";

$sent   = @mail($to,$subject,$message,$headers); 

        if ($sent) {
            return true;
        } else {
            return false;
        }

还添加必要的标头以作为 Vinoth Babu 发送 HTML 邮件,Naveen 建议

【讨论】:

  • 但它在邮件中显示总表,表示亲爱的 $firstname,您的欢迎信息。
    名字: .$firstName.
    谢谢xxxxxx
  • 只有你需要添加标题,跟随其他两个ans
  • @user1696874- 添加标题,检查我的答案
【解决方案2】:
$to = "naveen@gmail.com";
$subject = $name ." is contact ";

 $headers = "MIME-Version: 1.0" . "\n";
 $headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";
@mail($to,$subject,$message,$headers);

将标题设置为您的消息的 mime 版本和类型,例如 text/html

【讨论】:

    【解决方案3】:

    要发送 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";
    
    mail($to, $subject, $message, $headers);
    

    请参考以下链接,

    Mail Function

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 2011-06-21
      • 1970-01-01
      • 2014-06-23
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多