2009-09-13 4 views
0

사람들이 정보를 제출하면 첨부 파일이있는 이메일을 보내는 PHP 스크립트가 있습니다. 나는이 메일을 Gmail의받은 편지함에 문제없이 수신합니다. 그러나 개인 이메일 주소 나 직장 이메일 주소를 사용하면 이메일이 배달되지 않습니다. 이 문제는 내 스크립트 (아래) 또는 서버의 일부 설정에 문제가 있습니까? 나는 그것이 헤더에 문제가있을 수 있다고 생각하지만, 헤더를 변경할 때마다 전자 메일을 중단하고 모든 것이 메시지 본문에 표시됩니다. 누구든지이 문제를 해결하는 방법을 알고 있습니까? 서버는 plesk 제어판이있는 클라이언트 관리 형 Linux 서버이므로 PHP ini 파일에 액세스 할 수 없습니다.php mail() 이메일은 Gmail 계정에 있지만 일반 이메일 계정에는 도착하지 않습니다.

//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Email with Attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$mime_boundary = "<<<--==+X[".md5(time())."]"; 

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/'; 
$fileContent = chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf'))); 


$headers .= "From: [email protected] <[email protected]>\r\n"; 

$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed;\r\n"; 
$headers .= " boundary=\"".$mime_boundary."\""; 




$message = "This is a multi-part message in MIME format.\r\n"; 

$message .= "\r\n"; 
$message .= "--".$mime_boundary."\r\n"; 

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
$message .= "Content-Transfer-Encoding: 7bit\r\n"; 
$message .= "\r\n"; 
$message .= "Email content and what not: \r\n"; 
$message .= "This is the file you asked for! \r\n"; 
$message .= "--".$mime_boundary."" . "\r\n"; 

$message .= "Content-Type: application/octet-stream;\r\n"; 
$message .= " name=\"CTF-brochure.pdf\"" . "\r\n"; 
$message .= "Content-Transfer-Encoding: base64 \r\n"; 
$message .= "Content-Disposition: attachment;\r\n"; 
$message .= " filename=\"CTF_brochure.pdf\"\r\n"; 
$message .= "\r\n"; 
$message .= $fileContent; 
$message .= "\r\n"; 
$message .= "--".$mime_boundary."\r\n"; 

//send the email 
$mail_sent = mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
+0

하이 드류를 저장합니다. 다른 Gmail이 아닌 계정으로 보낼 때 "Mail sent"또는 "Mail failed"가 출력되었습니다. 다른 Gmail 계정으로 보내시겠습니까? – mauris

+0

매번 출력되는 메일은 – Drew

답변

2

다른 대안 버전을 사용해보십시오. 그것은 일반 텍스트와 html 메시지를 모두 받아 들일 수 있으며 메일 클라이언트는 표시 할 메시지를 선택할 수 있습니다.


//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Email with Attachment'; 
//create 2 boundary strings. They must be unique 
$boundary1 = rand(0,9)."-" 
          .rand(10000000000,9999999999)."-" 
          .rand(10000000000,9999999999)."=:" 
          .rand(10000,99999); 
$boundary2 = rand(0,9)."-".rand(10000000000,9999999999)."-" 
          .rand(10000000000,9999999999)."=:" 
          .rand(10000,99999); 

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/'; 
$fileContent = chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf'))); 

$txt_message = "Email content and what not: \r\n"; 
$txt_message .= "This is the file you asked for! \r\n"; 

$html_message = "Email content and what not: <br />"; 
$html_message .= "This is the file you asked for! "; 

$headers  =<<<AKAM 
From: [email protected] <[email protected]> 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
    boundary="$boundary1" 
AKAM; 

$attachment = <<<ATTA 
--$boundary1 
Content-Type: application/octet-stream; 
    name="CTF_brochure.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="CTF_brochure.pdf" 

$fileContent 

ATTA; 

$body = <<<AKAM 
This is a multi-part message in MIME format. 

--$boundary1 
Content-Type: multipart/alternative; 
    boundary="$boundary2" 

--$boundary2 
Content-Type: text/plain; 
    charset=ISO-8859-1; 
Content-Transfer-Encoding: 7bit 

$txt_message 
--$boundary2 
Content-Type: text/html; 
    charset=ISO-8859-1; 
Content-Transfer-Encoding: 7bit 

$html_message 

--$boundary2-- 

$attachment 
--$boundary1-- 
AKAM; 

//send the email 
$mail_sent = @mail($to, $subject, $body, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
+0

감사합니다. 아아, Gmail 계정에 아무런 메시지도 보내지 않고 다른 메일을 통해 아무것도 얻지 못합니다. – Drew

0

다른 계정의 스팸 폴더를 확인하십시오. 때로는 전자 메일 계정 공급자가 사용하는 스팸 필터링 소프트웨어에 대해 너무 많은 경고를 트리거하면 전자 메일이 스팸으로 끝날 수 있습니다.

+0

에게 보내 주신 데 대해 감사 드려요. 아아, 내가 처음 보았습니다! 이것에 대해 – Drew

0

mail() 함수는 헤더를 처리해야하는 경우를 제외하고는 매우 유용합니다. 대부분의 전자 메일은 적절한 헤더없이 스팸 봇에 의해 거부됩니다.

나는 PHPMailer 클래스를 사용하여 모든 작업을 수행하도록 권장합니다. 스스로를 만드는 것은 고통이며 권장되지 않습니다.

https://github.com/PHPMailer/PHPMailer

이 많은 훌륭한 기능을 함께 제공하고 이러한 모든 문제의 삶 :