PHPMailer 파일첨부 및 dkim 사용하기
dkim 테스트 하면서 php mailer 프로그램으로 하였습니다. 헤더 부분 처리하는데 다른 프로그램 여러개 테스트해 봤지만 제대로 되지 않았는데 이 라이브러리만 잘 되었습니다.
아래 상수로 패스워드 넣는란이 있는데 DKIM 생설 할 때 패스워드 넣는데 그것을 넣어 주는 것입니다.
그런데 PHP Mailer 너무 잘 만들어진 프로그램 입니다. 전에는 직접 만들어 사용하기도 하였는데요. 파일 첨부까지 base64 인코딩 하고 헤더 부분 만들어 주고 그랬는데 이제는 이런 라이브러리 하나 있으면 모든것이 해결이 됩니다.
● BIND 네임서버 DKIM 설정
default._domainkey.pabburi.co.kr. IN TXT "v=DKIM1; k=rsa; p=퍼블릭파일내용;"
● 설정에 문제 없는지 확인해 본다.
dig default._domainkey.pabburi.co.kr txt
● 네임서버 DKIM 키 체크해주는곳
https://www.mail-tester.com/spf-dkim-check
https://dkimcore.org/c/keycheck
#!/opt/remi/php74/root/bin/php
<?php
set_time_limit(0);
ini_set("display_errors", 1);
ini_set('memory_limit','2048M');
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// //Load Composer's autoloader
// require 'vendor/autoload.php';
include 'src/Exception.php';
include 'src/OAuth.php';
include 'src/PHPMailer.php';
include 'src/POP3.php';
include 'src/SMTP.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
define('MAIL_DOMAIN', 'mail.domain.com');
define('MAIL_SELECTOR', 'default');
define('MAIL_IDENTITY', NULL);
define('MAIL_RSA_PASSPHRASE', 'password');
try {
# Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->SMTPDebug = 0; // 2 이면 자세히 보여준다.
$mail->Host = 'smtp.pabburi.co.kr'; //Set the SMTP server to send through
// use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->Port = 25; //TCP port to connect to;
// $mail->SMTPAuth = true; //Enable SMTP authentication
// $mail->Username = 'user@example.com'; //SMTP username
// $mail->Password = 'secret'; //SMTP password
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$fromMail = 'test@'.MAIL_DOMAIN;
$fromName = 'testname';
$toMail = 'test@gmail.com';
$toName = 'testMail';
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->ContentType = "text/html";
//Recipients
$mail->setFrom($fromMail, $fromName);
$mail->addAddress($toMail, $toName); //Add a recipient
$mail->addReplyTo($fromMail, $fromName);
// $mail->addCC('cc@example.com');
# 파일첨부 - Gmail 25MB 한계
$rAddFile = $mail->addAttachment('/PHPMailer-master/composer.json'); //Add attachments
$rAddFile = $mail->addAttachment('/PHPMailer-master/README.md'); //Add attachments
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $mail->Host . ' phpmailer-DKIM - ' . date('Y-m-d H:i:s ');
$mail->Body = 'html mail body';
$mail->AltBody = 'mail body test - 1';
$mail->DKIM_domain = MAIL_DOMAIN;
$mail->DKIM_private = '/PHPMailer/dkim/private_domain.pem';
$mail->DKIM_selector = MAIL_SELECTOR;
$mail->DKIM_passphrase = MAIL_RSA_PASSPHRASE;
$mail->DKIM_identity = $mail->From;
$rSend = $mail->send();
echo date('Y-m-d H:i:s ') . $rSend . ' - ' . $rAddFile . ' - Message has been sent' . "\n\n";
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
* 구글의 메일을 통해서 발송하고자 할때
보안수준이 낮은 앱이 아니면 사용 가능할 수 있는데 정책이 변경 되어 사용이 안될 수 있으니 그러면 다음카카오의 것을 사용하면 된다.
https://mail.daum.net/#setting/POP3IMAP
* 참고하면 좋은것
- [AWS] SES SMTP 메일 발송하기 PHP Mailer > AWS.클라우드