간단한 알바를 위해서 오랜만에 PHP를 만져보내요…
고객의 의견을 지정된 곳으로 메일을 발송하는 기능이 있는데 서버에 SMTP 서버가 구성되어 있지 않으면 PHP 자체에 구현되어있는 메일 발송기능을 사용할 수 없어서, 모든 개발자들이 좋아하는 GMAIL을 활용해서 메일을 발송하는 함수를 하나 만들어 봤습니다.
먼저 SMTP로 메일을 발송할 수 있게 도와주는 메일발송 관련 라이브러리를 다운받습니다.
phpmailer 라는 라이브러리 입니다.
사이트 주소 : https://code.google.com/a/apache-extras.org/p/phpmailer/
해당 라이브러리 다운 받아서 서버에 올려 놓고 아래의 함수를 작성해서 사용하시면 됩니다.
PHP 메일발송 관련 라이브러리
<?php
/*
* AUTHOR : YOUNGMINJUN
*
* $EMAIL : 보내는 사람 메일 주소
* $NAME : 보내는 사람 이름
* $SUBJECT : 메일 제목
* $CONTENT : 메일 내용
* $MAILTO : 받는 사람 메일 주소
* $MAILTONAME : 받는 사람 이름
*/
function sendMail($EMAIL, $NAME, $SUBJECT, $CONTENT, $MAILTO, $MAILTONAME){
$mail = new PHPMailer();
$body = $CONTENT;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "www.coolio.so"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "사용자 계정"; // GMAIL username
$mail->Password = "비밀번호"; // GMAIL password
$mail->SetFrom($EMAIL, $NAME);
$mail->AddReplyTo($EMAIL, $NAME);
$mail->Subject = $SUBJECT;
$mail->MsgHTML($body);
$address = $MAILTO;
$mail->AddAddress($address, $MAILTONAME);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
사용방법
<?php
sendMail("youngmin.jun@hubtree.co.kr", "HUBTREE-JUN", "메일 제목입니다.", "메일 컨텐츠 입니다.", "youngmin.jun@gmail.com", "COOLIO-JUN");
?>