简体   繁体   English

如何使用PHP发送带有HTML表格的电子邮件?

[英]How to send an email with an HTML table with PHP?

I am sending dynamically creating html table in email body but in the email I receive the html code instead of displaying a table. 我发送动态创建电子邮件正文中的html表但在电子邮件中我收到的是html代码,而不是显示表格。 Please help me. 请帮我。 Here is my code. 这是我的代码。 where my server support only this code for send email $email_message = "name: ".clean_string($name)."\\n"; 我的服务器只支持此代码发送电子邮件$ email_message =“name:”。cleanantring($ name)。“\\ n”; so please help me using this code. 所以请帮我使用这段代码。

  <?php 
require ('class.phpmailer.php');

$name = @$_REQUEST['name'];
//$email = @$_REQUEST['email'];
$contact = @$_REQUEST['contact'];
$day = @$_REQUEST['day'];
$month = @$_REQUEST['month'];
$year = @$_REQUEST['year'];
$date_event = @$_REQUEST['date_event'];
$check = @$_REQUEST['check'];
$check_score = @$_REQUEST['check_score'];
$check_service = @$_REQUEST['check_service'];
$check_product = @$_REQUEST['check_product'];
$check_scheme = @$_REQUEST['check_scheme'];
$check_value = @$_REQUEST['check_value'];
$check_price = @$_REQUEST['check_price'];



$arr = array($year,$month,$day);
$doe_event = join("-",$arr);

$action=@$_REQUEST['action'];

$today=getdate();
$gday="$today[mday]";
$gmonth="$today[mon]";
$gyear="$today[year]";
$ghour="$today[hours]";
$gminutes="$today[minutes]";
$gsecond="$today[seconds]";
$create_date="$gyear-$gmonth-$gday $ghour:$gminutes:$gsecond";

if($action=='add') {

        $email_to = "your email";
        $email_subject = "Message From Website";

        $error_message = "";
        $email_exp = "^[a-z .'-]+$";
        $email_message = "<strong>Contact form</strong><br>";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }   

            //All data In table mail
            $headers = "From: $ContactPerson<info@*******.co.in> \n";
            $headers .= "Reply-To: $to \r\n";
            $headers .= "X-Mailer: PHP/". phpversion();
            $headers .= "X-Priority: 3 \n";
            $headers .= "MIME-version: 1.0\n";
            $headers .= "Content-Type: text/html; charset=UTF-8\n";                 
$email_message='<table cellspacing="0" cellpadding="0" border="1" align="center">  
                 <tr>
                  <td bgcolor="#99CC00">Name &nbsp;';
                      $email_message = "Quick enquiry detail.";
                 $email_message = " ".clean_string($name)."\n";'</td>
                 </tr>  
               </table>';


            //$email_message .= clean_string($email);               
            //$email_message = "Quick enquiry detail.";
            $email_message .= "name: ".clean_string($name)."\n";
            $email_message .= "contact: ".clean_string($contact)."\n";
            $email_message .= "doe: ".clean_string($doe_event)."\n";

  if(isset($_REQUEST['check']) && count($_REQUEST['check'])) 
            {

               $recipient_email_addresses = array 
                (
  );
                  foreach($_REQUEST['check'] as $value){
                $email_message .= "services: ".clean_string($value)."\n";
                };

    }               

            // create email headers
            $email_from="info@*******.co.in";

            $headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$_REQUEST['email']."\r\n" .
            'X-Mailer: PHP/' . phpversion();
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->FromName = $_REQUEST['name'];
            $mail->Host = 'mail.*******.co.in';
            $mail->SMTPAuth = true; 
            $mail->Username = 'info@********.co.in';
            $mail->Password = '********';   

            $mail->AddAddress("your email", "ADMIN");


            $mail->Subject = "Contact Enquiry From www.********.co.in"; 
            $mail->Body    = $email_message;

            if($mail->Send())
            {

    echo "<script>alert('Thank you for quick submit.')    </script>";
    echo "<script>location.replace('index.php?action=msgsent');</script>";                  
                die;
            }
            else
            {
    echo "<script>alert('There was an error sending the message, Please try again.')</script>";
                echo "<script>location.replace('index.php?action=msgerr');</script>";                   
                die;                    
            }

    }

?> ?>

For PHPMailer there is: 对于PHPMailer,有:


$mail->IsHTML(true); //try adding this

Ref: Html Mails 参考: Html邮件

Please refer w3schools.com. 请参考w3schools.com。 In this you can find proper idea with examples. 在这里你可以通过例子找到合适的想法。 Try it 试试吧

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

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