SW/ 개발팁 / 추천프로그램, 꿀팁 공유
  • 돌아가기
  • 아래로
  • 위로
  • 목록
  • 댓글
정보

Php-Mailer를 이용해서 메일 발송하기(Mail 서버사용X)

달소 달소 843

0

1

@PHPMailer

원래 서버에서 메일을 전송하려고 하면 senmail을 설치하고 메일을 발송하는 방법이 기본적이다. 하지만 sendmail 데몬이 죽거나 하면 메일이 안가기 때문에.. 능력자가 만들어놓은 php-mailer를 사용해서 메일 보내는 방법을 기술해보겠다.

준비물 : 서버 or pc ,gmail ID, naver.com

참고 : https://github.com/PHPMailer/PHPMailer

git clone을 사용해서 php-mailer 다운로드.

git clone 명령어가 안되면 apt install git 설치

git clone https://github.com/PHPMailer/PHPMailer

php 설치하기

php 기반이니까.. 당연히 php 설치!
apt install php

설치확인
php -v

testmail.php 작성하기

먼저 PHPMailer 위치 확인. 나는 /home/admin-sv/PHPMailer/ 이다

vi testmail.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require '/home/admin-sv/PHPMailer/src/Exception.php';
require '/home/admin-sv/PHPMailer/src/PHPMailer.php';
require '/home/admin-sv/PHPMailer/src/SMTP.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
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->CharSet = "utf-8"; //한글이 안깨지게 CharSet 설정
$mail->Encoding = "base64";
$mail->Host = "smtp.gmail.com"; // email 보낼때 사용할 서버를 지정
$mail->SMTPAuth = true; // SMTP 인증을 사용함
$mail->Port = 465; // email 보낼때 사용할 포트를 지정
$mail->SMTPSecure = "ssl"; // SSL을 사용함
$mail->Username = "메일주소@gmail.com"; // Gmail 계정
$mail->Password = "메일 PW"; // 패스워드
$mail->SetFrom('보내는 사람 메일 주소', '표시될이름'); // 보내는 사람 email 주소와 표시될 이름 (표시될 이름은 생략가능)
$mail->AddAddress('받는사람@gmail.com'); // 받을 사람 email 주소와 표시될 이름 (표시될 이름은 생략가능)
$mail->Subject = 'Test email'; // 메일 제목
$mail->Body =
"Test email 발송"; // 내용
$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!
}
?>

발송 Test

php testmail.php

gmail 확인

PHP로 짜기때문에 db연동이나 함수 활용해서 추가적으로 활용이 가능해 보인다.

*PS

한글 깨진거 메일보냈을때

신고공유스크랩
1

댓글 쓰기 권한이 없습니다. 로그인

취소 댓글 등록

cmt alert

신고

"님의 댓글"

이 댓글을 신고하시겠습니까?

댓글 삭제

"님의 댓글"

삭제하시겠습니까?


목록

공유

facebooktwitterpinterestbandkakao story