ライブラリーのインストール
composer require swiftmailer/swiftmailer
次の警告が出ますが、使えます。
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
プログラム
ツリー構造
$ tree -L 2
.
├── composer.json
├── composer.lock
├── swiftmailer.php
└── vendor
├── autoload.php
├── composer
├── doctrine
├── egulias
├── swiftmailer
└── symfony
swiftmailer.php
<?php
require_once 'vendor/autoload.php';
// 送信設定
$transport = new Swift_SmtpTransport('hi-ho.mose-mail.jp', 587);
$transport->setUsername('user@hi-ho.ne.jp');
$transport->setPassword('password');
$mailer = new Swift_Mailer($transport);
$str_out = 'Hello,Everybody
Test
';
$str_out .= 'Dec/20/2023
';
$str_out .= 'Dec/20/2023
';
$str_out .= 'Dec/20/2023
';
// メール作成
$message = new Swift_Message('Test Message PM 16:45');
$message->setFrom(['user@hi-ho.ne.jp' => 'Uchida']);
$message->setTo(['user@example.com']);
$message->setBody(
$str_out, 'text/plain'
);
// メール送信
$result = $mailer->send($message);
var_dump($result);
送信
php swiftmailer.php
$ php swiftmailer.php
int(1)
送信結果
確認したバージョン
$ php --version
PHP 8.2.10-2ubuntu1 (cli) (built: Sep 5 2023 14:37:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
with Zend OPcache v8.2.10-2ubuntu1, Copyright (c), by Zend Technologies