뉴스 작성하기 라는 기능을 추가하고 싶었다.뉴스 작성하기 기능은 어떤 글을 즐겨찾기한 모든 사람들에게 News message가 담긴 이메일을 보내는 기능이다.google계정을 하나 새로파서 php에서 그 구글계정을 통해서 다른 이메일 주소들로 메일을 보내는 방법을 알아야했다.
메일을 주고받는 두 client가 메일서버(smtp server)를 통해서 어떻게 메일을 주고받는지 보자.

서버에 smtp 서버가 세팅되어있지 않아서 php자체에서 제공하는 mail function은 안썼다.
메일을 받아서 전송해 줄 smtp서버로 google(smtp.gmail.com)을 이용했다. (library 사용. 위의 1번 부분.)
PHPmailer 라는 library를 사용했다.
https://github.com/PHPMailer/PHPMailer/
우선 서버의 document root로 가서 아래 명령어를 통해 PHPmailer를 설치한다.
$ composer.phar require phpmailer/phpmailer
그리고 아래와 같이 send_mail function을 만들 수 있다.
(gmail smtp server를 이용했다.)
<?php
// 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 $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
function send_mail($addresses, $subject, $body) {
    $mail = new PHPMailer(true);
    
    try {
        //Server settings
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = '**id**';                     // SMTP username
        $mail->Password   = '**pw**';                               // SMTP password
        $mail->CharSet = 'utf-8'; 
        $mail->Encoding = "base64";
        $mail->SMTPSecure = 'ssl';          
        $mail->Port       = 465;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
        //Recipients
        $mail->setFrom('eventapp.notify@gmail.com', 'eventapp');
        foreach ($addresses as $address) {
            $mail->addAddress($address);     // Add a recipient
        }
        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $body;
        $mail->send();
        echo 'Message has been sent';
        return true;
    } catch (Exception $e) {
        //echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        return false;
    }
}
발신 gmail계정에서 보안 탭에 보안 수준이 낮은 앱 엑세스 를 사용으로 해줘야
php가 해당 계정에 접근하여 메일을 보낼 수 있다.


뉴스 작성하기를 누르면 텍스트가 지정된 이메일로 보내지도록 만들었다.

뉴스 작성이 완료되고 수신된 메일.
뉴스 작성하기 를 클릭한 뒤에 server가 해당되는 모든사람에게 이메일을 다 보낼때 까지 작성자가 계속 기다려야 한다는 문제가 있었다. (위의 로딩화면으로 계속 기다려야 함.)
또한 메일 전송 로직에서 속도저하가 너무 심한데 이것도 해결 못했다..
PHP-FPM 라는 php extension을 설치후에 사용할 수 있다. (PHP FastCGI Process Manager)