简体   繁体   English

如何在PHP中以邮件形式发送HTML动态表?

[英]How to send HTML Dynamic Table as mail in php?

Here i have a code like getting the data from index.php , if user enter the data it will show like table and as well as it should go to email to some predefined mail. 在这里,我有一个类似从index.php获取数据的代码,如果用户输入数据,它将像表一样显示,并且应该将其发送到一些预定义的电子邮件中。 But here i am getting data Dynamically , i tried with $_REQUEST and $_POST both methods to get the data , but in mail function i am trying to change message parameter but by php tags its is not taking and showing some syntax errors. 但是这里我动态地获取数据,我尝试使用$ _REQUEST和$ _POST两种方法来获取数据,但是在邮件功能中,我试图更改消息参数,但是通过php标签,它没有采取并显示一些语法错误。

please go through code here 请在这里检查代码

<?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
';
?> 

Try this method, assign the posted values in variables, outside the $message variable and print the variable inside your $message variable 尝试此方法,在$ 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;
        }

Also add necessary header to send HTML mail as Vinoth Babu, Naveen suggested Naveen建议,还添加必要的标头以发送HTML邮件,如Vinoth Babu一样

$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);

set headers to mime version & type of your message like text/html 将标头设置为mime版本和邮件类型,例如text / html

To send HTML Content/Tables you have to add the following in the header of that mail, 要发送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);

please refer the link below, 请参考下面的链接,

Mail Function 邮件功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM