HTML和PHP联系表单与电子邮件模板

时间:2017-01-11 16:50:43

标签: php html email templates

以下是contact.php上的代码,该页面是从网站生成电子邮件的页面。

我无法找到为什么我的html电子邮件模板未呈现但仅显示为文本。

我必须在$message_body方遗漏一些东西。

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
if($_POST)
{
    $to_email       = "originsphotography63@gmail.com"; //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'>
	<head>
		<title>Nouveau message !</title>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		<meta name='viewport' content='width=device-width, initial-scale=1.0' />
		<style type='text/css'>
			* {
				-ms-text-size-adjust:100%;
				-webkit-text-size-adjust:none;
				-webkit-text-resize:100%;
				text-resize:100%;
			}
			a{
				outline:none;
				color:#40aceb;
				text-decoration:underline;
			}
			a:hover{text-decoration:none !important;}
			.nav a:hover{text-decoration:underline !important;}
			.title a:hover{text-decoration:underline !important;}
			.title-2 a:hover{text-decoration:underline !important;}
			.btn:hover{opacity:0.8;}
			.btn a:hover{text-decoration:none !important;}
			.btn{
				-webkit-transition:all 0.3s ease;
				-moz-transition:all 0.3s ease;
				-ms-transition:all 0.3s ease;
				transition:all 0.3s ease;
			}
			table td {border-collapse: collapse !important;}
			.ExternalClass, .ExternalClass a, .ExternalClass span, .ExternalClass b, .ExternalClass br, .ExternalClass p, .ExternalClass div{line-height:inherit;}
			@media only screen and (max-width:500px) {
				table[class='flexible']{width:100% !important;}
				table[class='center']{
					float:none !important;
					margin:0 auto !important;
				}
				*[class='hide']{
					display:none !important;
					width:0 !important;
					height:0 !important;
					padding:0 !important;
					font-size:0 !important;
					line-height:0 !important;
				}
				td[class='img-flex'] img{
					width:100% !important;
					height:auto !important;
				}
				td[class='aligncenter']{text-align:center !important;}
				th[class='flex']{
					display:block !important;
					width:100% !important;
				}
				td[class='wrapper']{padding:0 !important;}
				td[class='holder']{padding:30px 15px 20px !important;}
				td[class='nav']{
					padding:20px 0 0 !important;
					text-align:center !important;
				}
				td[class='h-auto']{height:auto !important;}
				td[class='description']{padding:30px 20px !important;}
				td[class='i-120'] img{
					width:120px !important;
					height:auto !important;
				}
				td[class='footer']{padding:5px 20px 20px !important;}
				td[class='footer'] td[class='aligncenter']{
					line-height:25px !important;
					padding:20px 0 0 !important;
				}
				tr[class='table-holder']{
					display:table !important;
					width:100% !important;
				}
				th[class='thead']{display:table-header-group !important; width:100% !important;}
				th[class='tfoot']{display:table-footer-group !important; width:100% !important;}
			}
		</style>
	</head>
	<body style='margin:0; padding:0;' bgcolor='#eaeced'>
		<table style='min-width:320px;' width='100%' cellspacing='0' cellpadding='0' bgcolor='#eaeced'>
			<!-- fix for gmail -->
			<tr>
				<td class='hide'>
					<table width='600' cellpadding='0' cellspacing='0' style='width:600px !important;'>
						<tr>
							<td style='min-width:600px; font-size:0; line-height:0;'>&nbsp;</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td class='wrapper' style='padding:0 10px;'>
					<!-- module 1 -->
					<table data-module='module-1' data-thumb='thumbnails/01.png' width='100%' cellpadding='0' cellspacing='0'>
						<tr>
							<td data-bgcolor='bg-module' bgcolor='#eaeced'>
								<table class='flexible' width='600' align='center' style='margin:0 auto;' cellpadding='0' cellspacing='0'>
									<tr>
										<td style='padding:29px 0 30px;'>
											<table width='100%' cellpadding='0' cellspacing='0'>
												<tr>
													<th class='flex' width='113' align='left' style='padding:0;'>
														<table class='center' cellpadding='0' cellspacing='0'>
															<tr>
																<td style='line-height:0;'>
																	<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>
																</td>
															</tr>
														</table>
													</th>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
					<!-- module 2 -->
					<table data-module='module-2' data-thumb='thumbnails/02.png' width='100%' cellpadding='0' cellspacing='0'>
						<tr>
							<td data-bgcolor='bg-module' bgcolor='#eaeced'>
								<table class='flexible' width='600' align='center' style='margin:0 auto;' cellpadding='0' cellspacing='0'>
									<tr>
										<td class='img-flex'><img src='https://www.originsphotography.eu/site/img/FAIRY.jpg' style='vertical-align:top;' width='600' height='306' alt='' /></td>
									</tr>
									<tr>
										<td data-bgcolor='bg-block' class='holder' style='padding:58px 60px 52px;' bgcolor='#f9f9f9'>
											<table width='100%' cellpadding='0' cellspacing='0'>
												<tr>
													<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;'>
														Nouveau message !
													</td>
												</tr>
												<tr>
													<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;'>
														".$user_prenom." ".$user_nom."
													</td>
												</tr>
												<tr>
													<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;'>
														".$user_message."
													</td>
												</tr>
												<tr>
													<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;'>
														".$user_email."
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr><td height='28'></td></tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<!-- fix for gmail -->
			<tr>
				<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>
			</tr>
		</table>
	</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);
    }
}
?>

2 个答案:

答案 0 :(得分: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";

答案 1 :(得分:0)

尝试在标题中调整以下代码:

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