2017-01-11 68 views
0

這是我的代碼contact.php,這是從網站生成電子郵件的頁面。HTML和PHP使用電子郵件模板的聯繫表格

我找不到爲什麼我的html電子郵件模板沒有呈現,但只顯示爲文本。

我必須在$message_body方缺失一些東西。

<?php 
 
header('Access-Control-Allow-Origin: *'); 
 
header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); 
 
if($_POST) 
 
{ 
 
    $to_email  = "[email protected]"; //Recipient email, Replace with own email here 
 
    
 
    //check if its an ajax request, exit if not 
 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 
 
     
 
     $output = json_encode(array(//create JSON data 
 
      'type'=>'Erreur', 
 
      'text' => 'Sorry Request must be Ajax POST' 
 
     )); 
 
     die($output); //exit script outputting json data 
 
    } 
 
     
 
    //Sanitize input data using PHP filter_var(). 
 
    $user_nom  = filter_var($_POST["user_nom"], FILTER_SANITIZE_STRING); 
 
    $user_prenom = filter_var($_POST["user_prenom"], FILTER_SANITIZE_STRING); 
 
    $user_message  = filter_var($_POST["user_message"], FILTER_SANITIZE_STRING); 
 
    $user_email  = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL); 
 
    $subject  = "Contact site internet"; 
 
    $from   = "www.originsphotography.eu"; 
 
    
 
    //additional php validation 
 
    if(strlen($user_nom)<4){ // If length is less than 4 it will output JSON error. 
 
     $output = json_encode(array('type'=>'error', 'text' => 'Le champ est trop court ou vide')); 
 
     die($output); 
 
    } 
 
    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation 
 
     $output = json_encode(array('type'=>'error', 'text' => 'Veuillez entrer une adresse email valide')); 
 
     die($output); 
 
    } 
 
    
 
    //email body 
 
    $message_body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> 
 
<html xmlns='http://www.w3.org/1999/xhtml'> 
 
\t <head> 
 
\t \t <title>Nouveau message !</title> 
 
\t \t <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
 
\t \t <meta name='viewport' content='width=device-width, initial-scale=1.0' /> 
 
\t \t <style type='text/css'> 
 
\t \t \t * { 
 
\t \t \t \t -ms-text-size-adjust:100%; 
 
\t \t \t \t -webkit-text-size-adjust:none; 
 
\t \t \t \t -webkit-text-resize:100%; 
 
\t \t \t \t text-resize:100%; 
 
\t \t \t } 
 
\t \t \t a{ 
 
\t \t \t \t outline:none; 
 
\t \t \t \t color:#40aceb; 
 
\t \t \t \t text-decoration:underline; 
 
\t \t \t } 
 
\t \t \t a:hover{text-decoration:none !important;} 
 
\t \t \t .nav a:hover{text-decoration:underline !important;} 
 
\t \t \t .title a:hover{text-decoration:underline !important;} 
 
\t \t \t .title-2 a:hover{text-decoration:underline !important;} 
 
\t \t \t .btn:hover{opacity:0.8;} 
 
\t \t \t .btn a:hover{text-decoration:none !important;} 
 
\t \t \t .btn{ 
 
\t \t \t \t -webkit-transition:all 0.3s ease; 
 
\t \t \t \t -moz-transition:all 0.3s ease; 
 
\t \t \t \t -ms-transition:all 0.3s ease; 
 
\t \t \t \t transition:all 0.3s ease; 
 
\t \t \t } 
 
\t \t \t table td {border-collapse: collapse !important;} 
 
\t \t \t .ExternalClass, .ExternalClass a, .ExternalClass span, .ExternalClass b, .ExternalClass br, .ExternalClass p, .ExternalClass div{line-height:inherit;} 
 
\t \t \t @media only screen and (max-width:500px) { 
 
\t \t \t \t table[class='flexible']{width:100% !important;} 
 
\t \t \t \t table[class='center']{ 
 
\t \t \t \t \t float:none !important; 
 
\t \t \t \t \t margin:0 auto !important; 
 
\t \t \t \t } 
 
\t \t \t \t *[class='hide']{ 
 
\t \t \t \t \t display:none !important; 
 
\t \t \t \t \t width:0 !important; 
 
\t \t \t \t \t height:0 !important; 
 
\t \t \t \t \t padding:0 !important; 
 
\t \t \t \t \t font-size:0 !important; 
 
\t \t \t \t \t line-height:0 !important; 
 
\t \t \t \t } 
 
\t \t \t \t td[class='img-flex'] img{ 
 
\t \t \t \t \t width:100% !important; 
 
\t \t \t \t \t height:auto !important; 
 
\t \t \t \t } 
 
\t \t \t \t td[class='aligncenter']{text-align:center !important;} 
 
\t \t \t \t th[class='flex']{ 
 
\t \t \t \t \t display:block !important; 
 
\t \t \t \t \t width:100% !important; 
 
\t \t \t \t } 
 
\t \t \t \t td[class='wrapper']{padding:0 !important;} 
 
\t \t \t \t td[class='holder']{padding:30px 15px 20px !important;} 
 
\t \t \t \t td[class='nav']{ 
 
\t \t \t \t \t padding:20px 0 0 !important; 
 
\t \t \t \t \t text-align:center !important; 
 
\t \t \t \t } 
 
\t \t \t \t td[class='h-auto']{height:auto !important;} 
 
\t \t \t \t td[class='description']{padding:30px 20px !important;} 
 
\t \t \t \t td[class='i-120'] img{ 
 
\t \t \t \t \t width:120px !important; 
 
\t \t \t \t \t height:auto !important; 
 
\t \t \t \t } 
 
\t \t \t \t td[class='footer']{padding:5px 20px 20px !important;} 
 
\t \t \t \t td[class='footer'] td[class='aligncenter']{ 
 
\t \t \t \t \t line-height:25px !important; 
 
\t \t \t \t \t padding:20px 0 0 !important; 
 
\t \t \t \t } 
 
\t \t \t \t tr[class='table-holder']{ 
 
\t \t \t \t \t display:table !important; 
 
\t \t \t \t \t width:100% !important; 
 
\t \t \t \t } 
 
\t \t \t \t th[class='thead']{display:table-header-group !important; width:100% !important;} 
 
\t \t \t \t th[class='tfoot']{display:table-footer-group !important; width:100% !important;} 
 
\t \t \t } 
 
\t \t </style> 
 
\t </head> 
 
\t <body style='margin:0; padding:0;' bgcolor='#eaeced'> 
 
\t \t <table style='min-width:320px;' width='100%' cellspacing='0' cellpadding='0' bgcolor='#eaeced'> 
 
\t \t \t <!-- fix for gmail --> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class='hide'> 
 
\t \t \t \t \t <table width='600' cellpadding='0' cellspacing='0' style='width:600px !important;'> 
 
\t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t <td style='min-width:600px; font-size:0; line-height:0;'>&nbsp;</td> 
 
\t \t \t \t \t \t </tr> 
 
\t \t \t \t \t </table> 
 
\t \t \t \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class='wrapper' style='padding:0 10px;'> 
 
\t \t \t \t \t <!-- module 1 --> 
 
\t \t \t \t \t <table data-module='module-1' data-thumb='thumbnails/01.png' width='100%' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t <td data-bgcolor='bg-module' bgcolor='#eaeced'> 
 
\t \t \t \t \t \t \t \t <table class='flexible' width='600' align='center' style='margin:0 auto;' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t <td style='padding:29px 0 30px;'> 
 
\t \t \t \t \t \t \t \t \t \t \t <table width='100%' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t <th class='flex' width='113' align='left' style='padding:0;'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <table class='center' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t <td style='line-height:0;'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t <a target='_blank' style='text-decoration:none;' href='https://www.psd2html.com/'><img src='https://www.originsphotography.eu/site/img/image414zdzd4.png' border='0' style='font:bold 12px/12px Arial, Helvetica, sans-serif; color:#606060;' align='left' vspace='0' hspace='0' width='113' height='12' alt='PSD2HTML.COM' /></a> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t </table> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t </th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t </table> 
 
\t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t </table> 
 
\t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t </tr> 
 
\t \t \t \t \t </table> 
 
\t \t \t \t \t <!-- module 2 --> 
 
\t \t \t \t \t <table data-module='module-2' data-thumb='thumbnails/02.png' width='100%' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t <td data-bgcolor='bg-module' bgcolor='#eaeced'> 
 
\t \t \t \t \t \t \t \t <table class='flexible' width='600' align='center' style='margin:0 auto;' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t <td class='img-flex'><img src='https://www.originsphotography.eu/site/img/FAIRY.jpg' style='vertical-align:top;' width='600' height='306' alt='' /></td> 
 
\t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t <td data-bgcolor='bg-block' class='holder' style='padding:58px 60px 52px;' bgcolor='#f9f9f9'> 
 
\t \t \t \t \t \t \t \t \t \t \t <table width='100%' cellpadding='0' cellspacing='0'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t <td data-color='title' data-size='size title' data-min='25' data-max='45' data-link-color='link title color' data-link-style='text-decoration:none; color:#292c34;' class='title' align='center' style='font:35px/38px Arial, Helvetica, sans-serif; color:#292c34; padding:0 0 24px;'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t Nouveau message ! 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t <td data-color='title' data-size='size title' data-min='25' data-max='45' data-link-color='link title color' data-link-style='text-decoration:none; color:#292c34;' class='title' align='center' style='font:25px/28px Arial, Helvetica, sans-serif; color:#292c34; padding:0 0 24px;'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t ".$user_prenom." ".$user_nom." 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t <td data-color='text' data-size='size text' data-min='10' data-max='26' data-link-color='link text color' data-link-style='font-weight:bold; text-decoration:underline; color:#40aceb;' align='center' style='font:bold 16px/25px Arial, Helvetica, sans-serif; color:#888; padding:0 0 23px;'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t ".$user_message." 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t <td data-color='title' data-size='size title' data-min='25' data-max='45' data-link-color='link title color' data-link-style='text-decoration:none; color:#292c34;' class='title' align='center' style='font:14px/14px Arial, Helvetica, sans-serif; color:#292c34; padding:0 0 24px;'> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t ".$user_email." 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t </table> 
 
\t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t <tr><td height='28'></td></tr> 
 
\t \t \t \t \t \t \t \t </table> 
 
\t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t </tr> 
 
\t \t \t \t \t </table> 
 
\t \t \t \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <!-- fix for gmail --> 
 
\t \t \t <tr> 
 
\t \t \t \t <td style='line-height:0;'><div style='display:none; white-space:nowrap; font:15px/1px courier;'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></td> 
 
\t \t \t </tr> 
 
\t \t </table> 
 
\t </body> 
 
</html>" ; 
 
    
 
    //proceed with PHP email. 
 
    $headers = 'From: '.$user_email.'' . "\r\n" . 
 
    'Reply-To: '.$user_email.'' . "\r\n" . 
 
    'X-Mailer: PHP/' . phpversion(); 
 
    
 
    $send_mail = mail($to_email, $subject, $message_body, $headers); 
 
    
 
    if(!$send_mail) 
 
    { 
 
     //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens) 
 
     $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.')); 
 
     die($output); 
 
    }else{ 
 
     $output = json_encode(array('type'=>'message', 'text' => 'Merci, '.$user_prenom .' pour votre message. Je vous réponds très vite !')); 
 
     die($output); 
 
    } 
 
} 
 
?>

回答

0

嘗試調整您的標題:

$headers = 'From: '.$user_email.'' . "\r\n"; 
$headers .= 'Reply-To: '.$user_email.'' . "\r\n"; 
$headers .= 'X-Mailer: PHP/' . phpversion(); 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
0

嘗試在頭下面調整此代碼:

$headers = 'From: '.$user_email.'' . "\r\n" . 
'Reply-To: '.$user_email.'' . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
+2

這是如何從對方的回答有什麼區別?或者是讓你的網站得到答案的目標? – Machavity