0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHP: Starttls でメールの送信

0
Last updated at Posted at 2020-07-08

こちらと同じことを PHP で行いました。
Python3: Starttls でメールの送信
hi-ho.ne.jp で試しました。

次のページを参考にしました。
symfony / mailer

ライブラリーのインストール

composer require symfony/mailer
composer require vlucas/phpdotenv
hi-ho.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//	hi-ho.php
//
//					Dec/17/2023
//
// ------------------------------------------------------------------
require_once 'vendor/autoload.php';
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;

fputs (STDERR,"*** 開始 ***\n");

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


$mailer_dsn = $_ENV['MAILER_DSN'];

$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秒" );

$transport = Transport::fromDsn($mailer_dsn);
$mailer = new Mailer($transport);

$email = new Email();
// 4. メッセージの作成
$str_out = "";
$str_out .= 'This is TEST.' . PHP_EOL;
$str_out .= 'こんにちは。' . PHP_EOL;
$str_out .= '晴れています。' . PHP_EOL;
$str_out .= 'Dec/17/2023' . PHP_EOL;
$str_out .= 'PM 12:14' . PHP_EOL;
$str_out .= $today . PHP_EOL;
$str_out .= $now . PHP_EOL;

$email
	->from($from)
	->to($to)
 	->subject('Time for Symfony Mailer!')
	->text($str_out)
       	;

$mailer->send($email);

fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>
.env
MAILER_DSN=smtp://user:password@hi-ho.mose-mail.jp:587
FROM = '****@hi-ho.ne.jp'
TO = 'sample@example.com'

実行結果

$ ./hi-ho.php 
*** 開始 ***
*** 終了 ***
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?