2012-10-17 3 views
0

전자 메일이 자동 생성 첨부 파일 (.txt)과 함께 전송되는 연락처 양식을 작성하고 싶습니다. 첨부 파일에는 연락처 양식에있는 입력 필드의 모든 값 (비어 있는지 여부)이 포함되어야합니다. PHP mail()을 사용하여이 작업을 수행 할 수 있습니까? 내 코드는 이제 전자 메일 본문으로 문의 양식 내용을 보낼 수 있습니다연락처 양식의 컨텐츠 자동 첨부 및 이메일로 보내기

<?php 
define("WEBMASTER_EMAIL", '[email protected]'); 
error_reporting (E_ALL); 


if(!empty($_POST)) 
{ 
$_POST = array_map('trim', $_POST); 
$name = htmlspecialchars($_POST['name']); 
$email = $_POST['email']; 
$subject = htmlspecialchars($_POST['subject']); 
$message = htmlspecialchars($_POST['message']); 

$error = array(); 


if(empty($name)) 
{ 

$error[] = 'Please enter your name'; 

} 


if(empty($email)) 
{ 

$error[] = 'Please enter your e-mail'; 


} 

elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
{ 

$error[] = 'e-mail is incorrect'; 

} 


if(empty($message) || empty($message{15})) 
{ 

$error[] = "Please enter message more than 15 characters"; 

} 

if(empty($error)) 
{ 

$body = ' 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<table> 
<tr><td>Name: </td><td>&nbsp;</td><td>'.$name.'</td><tr> 
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr> 
<tr><td>Email: </td><td>&nbsp;</td><td>'.$email.'</td><tr> 
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr> 
<tr><td>Subject: </td><td>&nbsp;</td><td>'.$subject.'</td><tr> 
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr> 
<tr><td>Message: </td><td>&nbsp;</td><td>'.stripslashes($message).'</td><tr> 
</table> 
</body> 
</html> 
'; 


$mail = mail(WEBMASTER_EMAIL, 'Message sent from website', $body, 
    "MIME-Version: 1.0" . "\r\n" 
."Content-type: text/html; charset=UTF-8" . "\r\n" 
."From: ".$email." \r\n" 
    ."Reply-To: ".$email."\r\n" 
    ."X-Mailer: PHP/" . phpversion()); 

if($mail) 
{ 
echo 'OK'; 
} 

} 
else 
{ 
echo '<div class="notification_error">'.implode('<br />', $error).'</div>'; 
} 
} 
?> 

답변

0

양식 값을 사용하여 파일을 생성하려면이 example를 참조하십시오.

텍스트 및 첨부 파일이 포함 된 php mail()을 사용하여 전자 메일을 보내려면 헤더를 약간 엉망으로 처리해야합니다. 헤더에 Content-Type: multipart/mixed;을 추가하십시오 (example 참조).

관련 문제