0
0

PHP: Swift Mailer の使い方

Last updated at Posted at 2023-12-20

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

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)

送信結果

image.png

確認したバージョン

$ 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
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