[PHP] xe에서 phpmailer를 이용해 메일 보내기
xe에서 메일 보내기를 시도해 봤다.
우선 xe루트에다가 sendmail 이라는 폴더를 새로 만들고 sendmail.php 파일도 새로 만들어서 아래와 같이 코딩 해봤다.
xe에는 기본적으로 phpmailer가 xe/libs/phpmailer/phpmailer.php 여기에 설치되어 있다.
이것을 활용해서 한번 메일 보내기를 시도해 봤다.
<?
require_once("../libs/phpmailer/phpmailer.php");
$mail = new PHPMailer(true);
//print_r($mail);
$mail->IsSMTP();
try{
$mail->SetFrom('test@gmail.com', 'TESTER'); // 보내는 사람 email 주소와 표시될 이름 (표시될 이름은 생략가능)
$mail->AddAddress("aaa@gmail.com","You");
$mail->Subject = 'Email Subject'; // 메일 제목
$mail->MsgHTML("content");
$mail->Send();
}catch(phpmailerException $e){
echo $e->errorMessage();
}catch(Exception $e){
echo $e->getMessage();
}
?>
예약모듈에서 추가
$event_date_mail = $y."-".$m."-".$d." ".$rTime;
require_once("../libs/phpmailer/phpmailer.php");
$mail = new PHPMailer(true);
//print_r($mail);
$mail->IsSMTP();
try{
$mail->SetFrom('soon1495@naver.com', 'DIU'); // 보내는 사람 email 주소와 표시될 이름 (표시될 이름은 생략가능)
$mail->Subject = '방금 예약하신 내용입니다.'; // 메일 제목
$mail->AddAddress($email_address,"You");
$content=$user_name."님, ".$event_date_mail."로 예약이 완료되었습니다. 혹시 예약을 취소하실 경우 반드시 하루 전에 soon1495@naver.com 이나 전화 070-8881-2805 로 연락부탁드립니다. 그래야 다른 고객님의 예약을 받을 수 있거든요! 감사합니다.^^";
$mail->MsgHTML($content);
$mail->Send();
}catch(phpmailerException $e){
echo $e->errorMessage();
}catch(Exception $e){
echo $e->getMessage();
}
위에처럼 간단하게 해서 테스트해보니 한메일 hanmail.net은 가는데 gmail은 안갔다.
======================================================
세부 사항은 아래 참조하면 된다^^
1. 다음 링크에서 필요한 모듈을 다운로드 받는다. (PHPMailer)
http://code.google.com/a/apache-extras.org/p/phpmailer/
2. 다운로드 받은 파일을 적당한 경로에 압축을 푼다.
3. TEST 해본다.
require_once("inc/PHPMailer/class.phpmailer.php");
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
// $mail->CharSet = "euc-kr";
// $mail->Encoding = "base64";
// $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// $mail->AddReplyTo('name@yourdomain.com', 'First Last');
// $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Host = "smtp.gmail.com"; // email 보낼때 사용할 서버를 지정
$mail->SMTPAuth = true; // SMTP 인증을 사용함
$mail->Port = 465; // email 보낼때 사용할 포트를 지정
$mail->SMTPSecure = "ssl"; // SSL을 사용함
$mail->Username = "test@gmail.com"; // Gmail 계정
$mail->Password = "password"; // 패스워드
$mail->SetFrom('test@gmail.com', 'TESTER'); // 보내는 사람 email 주소와 표시될 이름 (표시될 이름은 생략가능)
$mail->AddAddress('you@mailaddress.com', 'YOU'); // 받을 사람 email 주소와 표시될 이름 (표시될 이름은 생략가능)
$mail->Subject = 'Email Subject'; // 메일 제목
$mail->MsgHTML(file_get_contents('contents.html')); // 메일 내용 (HTML 형식도 되고 그냥 일반 텍스트도 사용 가능함)
$mail->Send();
echo "Message Sent OK\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}참고 사이트 :
- 포스팅 해온 블로그 : http://shineum.tistory.com/56
- PHP 튜토리얼 : http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html
출처: http://88240.tistory.com/76 [shaking blog]