홈페이지를 운영하다 보면 회원들에게 상태를 알리는 메일이나 안내 메일을 보낼 필요가 있습니다. 관리하는 홈페이지에서 이메일 전송이 제대로 안되는 문제가 발생해서 이것 저것 테스트 하기 위해 만들어본 샘플 소스 코드입니다. 이 PHP 소스를 이용해 포털 이메일 계정으로 메일을 보내 보니 대체적으로 잘 전송이 되었습니다. 그런데 엠파스나 야후 메일 같은 경우 조금 늦게 도착하는 것 같더군요. 이메일 루틴 관련해서 노하우를 가지고 계신 분이 있다면 관심 있는 분들을 위해 살짝 노출해주시길 바랍니다. UTF-8 기준으로 작성된 테스트 소스이므로 캐릭터셋이 다를 경우 수정이 필요합니다.

웹프로그래머의 홈페이지 정보 블로그 http://hompy.info

<?
$charset = "UTF-8";
$boundary = uniqid(rand(),true);

$mail_array[to_name] = "웹디자이너";
$mail_array[to_email] = "design@hompydesign.com";

$mail_array[from_name] = "웹프로그래머";
$mail_array[from_email] = "program@hompy.info";

$mail_array[title] = "[소식] 홈페이지 정보 블로그" . date("YmdHi");

$mail_array[content] = <<<MSG
<html>
<head>
<title>홈페이지 정보 블로그</title>
</head>
<body>
<a href='http://hompy.info/471' target='_blank'>홈페이지 정보 블로그</a><br>
<a href='http://hompy.info/471' target='_blank'>"http://hompy.info/471"</a><br>
</body>
</html>
MSG;

$mail_array[title] = '=?utf-8?b?'.base64_encode($mail_array[title]).'?=';
$mail_array[from_name] = '=?utf-8?b?'.base64_encode($mail_array[from_name]).'?=';
$mail_array[to_name] = '=?utf-8?b?'.base64_encode($mail_array[to_name]).'?=';
$recipient = $mail_array[to_name] . " <". $mail_array[to_email] . ">";

$headers =
"From: ". $mail_array[from_name] . " <".$mail_array[from_email] . ">\n" .
"Return-Path: ".$mail_array[to_name] . " <" . $mail_array[from_email] . ">\n" .
"Subject: ".$mail_array[title] . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"";

$body =
"--" . $boundary . "\n".
"Content-Type: text/plain; charset=" . $charset . "; format=flowed\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: inline\n\n" .
chunk_split(base64_encode(strip_tags($mail_array[content]))) . "\n\n" .
"--" . $boundary . "\n" .
"Content-Type: text/html; charset=" . $charset . "\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: inline\n\n" .
chunk_split(base64_encode($mail_array[content]))."\n\n" .
"--" . $boundary . "--\n";

mail($recipient , $mail_array[title], $body, $headers);
?>

트랙백 주소 :: http://hompy.info/trackback/471

  1. Subject: [php]

    Tracked from 모기뉨 블로그 2009/01/14 11:25  삭제

    캐릭터셋이 다를 경우 수정이 필요함 웹메일의 화이트도메인을 등록하지 않을 경우, 대형 포털 사이트로 수신이 되지 않을수 있음 단, 작성일 기준으로 네이트 메일은 수신 가능함 ( 네이버 X, 다음 X )

댓글을 달아 주세요