How to send html table in email body in php?

后端 未结 4 877
天涯浪人
天涯浪人 2020-12-11 07:09

I am sending dynamically creating html table in email body but in the email I receive the html code instead of displaying a table. Please help me. Here is my code.



        
相关标签:
4条回答
  • 2020-12-11 07:25

    I would suggest Swiftmailer

    http://swiftmailer.org/

    It is used by the Symfony project and has great docs and support.

    Writing email functionality yourself is not advises as there are so many quirks and security considerations.

    Here is the doc for setting the html body content:

    http://swiftmailer.org/docs/message-body

    0 讨论(0)
  • 2020-12-11 07:34

    First you are missing the tag in your HTML code but that should not be an issue.

    Secondly your header is incorrect, you have that:

    $headers = "From Ahmad \r\n";
    //$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
    //$headers .= "CC: susan@example.com\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
    $subject = 'Email report';
    

    instead of:

    //$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
    //$headers .= "CC: susan@example.com\r\n";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
    $headers .= "From: Ahmad <email@email.com>\r\n";
    $subject = 'Email report';
    

    Basically I think you header should start by "Mime-Version".

    Check this code: http://us.php.net/manual/en/function.mail.php#example-2877

    0 讨论(0)
  • 2020-12-11 07:34

    First of all, I would suggest using the Pear Mail extension (it's supplied with the basic build of php), because using the mail() function imo, is not as good, using th pear mail class, you can also set the mime type easily.

    Mail_mime class

    Please keep in mind that alot of mail services have limited (if any) support for HTML messages, I don't know the support level for tables, however.

    0 讨论(0)
  • 2020-12-11 07:42

    http://php.net/manual/en/function.mail.php

    Official example to send HTML-Emails:

    <?php
    // multiple recipients
    $to  = 'aidan@example.com' . ', '; // note the comma
    $to .= 'wez@example.com';
    
    // subject
    $subject = 'Birthday Reminders for August';
    
    // message
    $message = '
    <html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </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";
    
    // Additional headers
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
    
    // Mail it
    mail($to, $subject, $message, $headers);
    ?>
    

    At the end one problem could be that the table tree you created have some render problems in specific email clients. Can you test the code above?

    0 讨论(0)
提交回复
热议问题