php smtp 구글 이메일 보내기

최준호·2023년 4월 27일
0

-- php naver.com 메일보내기.

https://github.com/PHPMailer/PHPMailer

-- 위 깃허브 사이트를 참고하세요.

먼저 필요한 php 파일은

src 폴더 내,
phpMail.php, SMTP.php, Exception.php 파일을 복사한다.

php 파일 내 namespace를 모두 지워준다.

깃허브 내 example 폴더

gmail.phps 파일을 이용한다.

내용 변경 후 .

<?php

/**
 * This example shows settings to use when sending via Google's Gmail servers.
 * This uses traditional id & password authentication - look at the gmail_xoauth.phps
 * example to see how to use XOAUTH2.
 * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
 */

//Import PHPMailer classes into the global namespace

include "PHPMailer.php";
include "SMTP.php";

//Create a new PHPMailer instance
$mail = new PHPMailer();

try {
    $mail->CharSet    = "UTF-8";
    $mail->Encoding   = "base64";
    //한글 깨짐 해결
 } catch (Exception $e) {
    echo $e->getMessage();

 }

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// smtp 서버명
//Use `$mail->Host = gethostbyname('smtp.gmail.com');`
//if your network does not support SMTP over IPv6,
//though this may cause issues with TLS

//Set the SMTP port number:
// - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or
// - 587 for SMTP+STARTTLS
$mail->Port = 465;
// 포트

//Set the encryption mechanism to use:
// - SMTPS (implicit TLS on port 465) or
// - STARTTLS (explicit TLS on port 587)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'cosmos3407';
// 아이디

//Password to use for SMTP authentication
$mail->Password = 'zjfvncsmfopvjjla';

//Set who the message is to be sent from
//Note that with gmail you can only use your account address (same as `Username`)
//or predefined aliases that you have configured within your account.
//Do not use user-submitted addresses in here
$mail->setFrom('cosmos3407@gmail.com', 'First Last');
// 누가 보낸건지 

//Set an alternative reply-to address
//This is a good place to put user-submitted addresses
$mail->addReplyTo('cosmos3407@gmail.com', 'First Last');
// 답변시 누구한테 보낼건지

//Set who the message is to be sent to
$mail->addAddress('lveco3407@naver.com', 'John Doe');
// 누구한테 보낼건지

//Set the subject line
$mail->Subject = '메일 테스트입니다!!!!!.';
// 메일 제목

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
// contents.html를 포함 시켜 보내준다

//Replace the plain text body with one created manually
$mail->AltBody = '이메일 테스트 ';
// 이메일 내용 만약 보내는 게 따로 있을경우 안보내짐

//Attach an image file
$mail->addAttachment('images/card.jpg');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
    //Section 2: IMAP
    //Uncomment these to save your message in the 'Sent Mail' folder.
    #if (save_mail($mail)) {
    #    echo "Message saved!";
    #}
}

//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl', '*' ) to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
    //You can change 'Sent Mail' to any other folder or tag
    $path = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail';

    //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
    $imapStream = imap_open($path, $mail->Username, $mail->Password);

    $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
    imap_close($imapStream);

    return $result;
}

실행시

php 구조

profile
변화를 두려워하는 사람이 가장 불행한 사람이다.

0개의 댓글