php ライブラリ「PHPMailer」を使ってメールを送信する

php ライブラリ「PHPMailer」を使ってメールを送信する



  • OS windows10 pro 64bit
  • Apache 2.4.43
  • PHP 7.4.5
  • Composer 1.10.5




composer require phpmailer/phpmailer

Using version ^6.1 for phpmailer/phpmailer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing phpmailer/phpmailer (v6.1.6): Downloading (100%)
phpmailer/phpmailer suggests installing league/oauth2-google (Needed for Google XOAUTH2 authentication)
phpmailer/phpmailer suggests installing hayageek/oauth2-yahoo (Needed for Yahoo XOAUTH2 authentication)
phpmailer/phpmailer suggests installing stevenmaguire/oauth2-microsoft (Needed for Microsoft XOAUTH2 authentication)
Package mschop/noteephp is abandoned, you should avoid using it. Use mschop/notee instead.
Writing lock file
Generating autoload files
20 packages you are using are looking for funding.
Use the `composer fund` command to find out more!




ini_set('display_errors', "On");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // デバック
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      
    // SMTPの使用
    // smtpサーバー設定
    $mail->Host       = '';
    // SMTP認証                  
    $mail->SMTPAuth   = true;
     // SMTP ユーザー                                
    $mail->Username   = '';
    // SMTP パスワード                   
    $mail->Password   = 'Password';
    // ポート設定                           
    $mail->Port       = 587;                                    

    $mail->setFrom('', 'mebee');
    $mail->addAddress('', 'mebee');

    // htmlメール指定
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';    

    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";