메일 발송용 함수 (참조, 숨김참조, 파일첨부 포함)
<?
class Mail {
/*
$senderName = '웹마스터';
$senderEmail = 'webmaster@webmaster.org';
$toName = 'Sender';
$toEmail = 'webmaster@webmaster.org';
$subject = '제목';
$content = '내용';
$filePath = array('file1', 'file2');
$cc = '';
$bcc = '';
Mail::sendMail($senderName, $senderEmail, $toName, $toEmail, $subject, $content, $filePath, $cc, $bcc);
*/
public static function prepareAttachment($path) {
if (file_exists($path)) {
$rn = "\r\n";
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$ftype = finfo_file($finfo, $path);
$file = fopen($path, "r");
$attachment = fread($file, filesize($path));
$attachment = chunk_split(base64_encode($attachment));
fclose($file);
$msg = 'Content-Type: \'' . $ftype . '\'; name="' . basename($path) . '"' . $rn;
$msg .= "Content-Transfer-Encoding: base64" . $rn;
$msg .= 'Content-ID: <' . basename($path) . '>' . $rn;
$msg .= $rn . $attachment . $rn . $rn;
return $msg;
}
return false;
}
public static function sendMail($senderName, $senderEmail, $toName, $toEmail, $subject, $content, $file = '', $cc = '', $bcc = '') {
$rn = "\r\n";
$boundary = md5(rand());
$boundary_content = md5(rand());
$headers = 'From: =?UTF-8?B?'.base64_encode($senderName).'?=<' . $senderEmail . '>' . $rn;
$headers .= 'Reply-To: =?UTF-8?B?'.base64_encode($senderName).'?=<' . $senderEmail . '>' . $rn;
$headers .= 'Mime-Version: 1.0' . $rn;
$headers .= 'Content-Type: multipart/related;boundary=' . $boundary . $rn;
$to = '=?UTF-8?B?'.base64_encode($toName).'?=<' . $toEmail . '>' . $rn;
if ($cc != '') {
$headers .= 'Cc: ' . $cc . $rn;
}
if ($bcc != '') {
$headers .= 'Bcc: ' . $bcc . $rn;
}
$headers .= $rn;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$msg = $rn . '--' . $boundary . $rn;
$msg.= "Content-Type: multipart/alternative;" . $rn;
$msg.= " boundary=\"$boundary_content\"" . $rn;
$msg.= $rn . "--" . $boundary_content . $rn;
$msg .= 'Content-Type: text/plain; charset=UTF-8' . $rn;
$msg .= strip_tags($content) . $rn;
$msg.= $rn . "--" . $boundary_content . $rn;
$msg .= 'Content-Type: text/html; charset=UTF-8' . $rn;
$msg .= 'Content-Transfer-Encoding: quoted-printable' . $rn;
$msg .= $rn . '<div>' . str_replace("=", "=3D", $content) . '</div>' . $rn;
$msg .= $rn . '--' . $boundary_content . '--' . $rn;
if(is_array($file)) {
foreach ($file as $f) {
if ($f != '' && file_exists($f)) {
$conAttached = self::prepareAttachment($f);
if ($conAttached !== false) {
$msg .= $rn . '--' . $boundary . $rn;
$msg .= $conAttached;
}
}
}
} else if ($file != '' && file_exists($file)) {
$conAttached = self::prepareAttachment($file);
if ($conAttached !== false) {
$msg .= $rn . '--' . $boundary . $rn;
$msg .= $conAttached;
}
}
$msg .= $rn . '--' . $boundary . '--' . $rn;
mail($to, $subject, $msg, $headers);
}
}
?>