LoginSignup
0
0

PHP: SMTP over SSL でメールの送信

Last updated at Posted at 2024-01-08

こちらのプログラムを改造しました。
PHP: PHPMailer の使い方

Port は、465 です。

プログラム

phpmailer_smtp_ssl.php
#! /usr/bin/php

<?php
// ------------------------------------------------------------------
//	phpmailer_smtp_env.php
//
//				        Jan/08/2024
//
// ------------------------------------------------------------------
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once '/usr/share/php/libphp-phpmailer/autoload.php';
require_once '/usr/share/php/Dotenv/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$from = $_ENV['FROM'];
$to = $_ENV['TO'];

date_default_timezone_set('Asia/Tokyo');
$today = date ("Y-m-d");
$now = date("Y年m月d日 H時i分s秒");


$mailer = new PHPMailer;

$mailer->IsSMTP();
$mailer->Host = $_ENV['SERVER'];
$mailer->SMTPAuth = true;
$mailer->Username = $_ENV['USR'];
$mailer->Password = $_ENV['PASSWORD'];
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
// $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mailer->Port = $_ENV['PORT'];

$mailer->CharSet = 'utf-8';

$str_out = "";
$str_out .= 'This is TEST.' . PHP_EOL;
$str_out .= 'こんにちは。' . PHP_EOL;
$str_out .= '晴れています。' . PHP_EOL;
$str_out .= $today . PHP_EOL;
$str_out .= $now . PHP_EOL;

$mailer->setFrom($from);
$mailer->addAddress($to);
$mailer->Subject = 'Test Jan/08/2024 ' . date ("H:i:s") ;
$mailer->Body = $str_out;

if(!$mailer->send()) {
	echo 'Email is not sent.\n';
	echo 'Email error: ' . $mailer->ErrorInfo;
} else {
	echo '*** Email has been sent. ***' . PHP_EOL;
}

// ------------------------------------------------------------------
?>
.env
SERVER = 'mail.aaaa.co.jp'
PORT = 465
USR = '****@aaaa.co.jp'
PASSWORD = '****'
FROM = '****@aaaa.co.jp'
TO = 'sample@example.com'
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0