Zend-framework-email-management

提供:Dev Guides
移動先:案内検索

Zend Framework-メール管理

Zend Frameworkは、電子メールメッセージを送信するために zend-mail と呼ばれる別のコンポーネントを提供します。 zend-mailコンポーネントには、テキスト形式とhtml形式の両方の添付ファイル付きの電子メールメッセージを読み書きするオプションもあります。 Zendでのメールの送信は、設定がはるかに簡単で簡単です。

この章では、電子メールの概念、基本設定、SMTPトランスポートなどの詳細設定について説明します。

メールコンポーネントのインストール

メールコンポーネントは、次のComposerコマンドを使用してインストールできます。

composer require zendframework/zend-mail

基本的なメール設定

基本的な電子メールは、1人以上の受信者、件名、本文、および送信者で構成されます。 Zendには、新しいメールメッセージを作成するための Zend \ Mail \ Message クラスが用意されています。 zend-mail を使用して電子メールを送信するには、少なくとも1人の受信者とメッセージ本文を指定する必要があります。

新しいメールメッセージを作成するための部分的なコードは次のとおりです-

use Zend\Mail;
$mail = new Mail\Message();
$mail->setSubject('Zend email sample');
$mail->setBody('This is content of the mail message');
$mail->setFrom('[email protected]', "sender-name");
$mail->addTo('[email protected]', "recipient-name");

Zendは、メールメッセージを送信するためのZend \ Mail \ Sendmailクラスを提供します。 Sendmail はphpのネイティブメール機能 mail を使用してメールメッセージを送信し、php構成ファイルを使用してトランスポート層を構成できます。

Sendmailを使用した部分的なコーディングは次のとおりです-

$transport = new Mail\Transport\Sendmail();
$transport->send($mail);
*zend-mail* は多くのトランスポート層を提供し、それぞれがユーザー名、パスワードなどの多くの追加パラメーターを必要とする場合があります

メール管理方法

注目すべき電子メール管理方法のいくつかは次のとおりです-

  • isValid -「差出人」アドレスのないメッセージは無効です。
isValid() : bool
  • setEncoding -メッセージのエンコードを設定します。
setEncoding(string $encoding) : void
  • getEncoding -メッセージのエンコードを取得します。
getEncoding() : string
  • setHeaders -ヘッダーを作成します。
setHeaders(Zend\Mail\Headers $headers) : void
  • getHeaders -ヘッダーコレクションにアクセスします。
getHeaders() : Zend\Mail\Headers
  • setFrom -送信元アドレスを設定(上書き)します。 キーは人間が読める名前で、値は電子メールアドレスです。
setFrom(
   string|AddressInterface|array|AddressList|Traversable $emailOrAddressList,
      string|null $name
) : void
  • addFrom -「差出人」アドレスを追加します。
addFrom(
   string|AddressInterface|array|AddressList|Traversable $emailOrAddressOrList,
      string|null $name
) : void
  • getFrom -「差出人」送信者のリストを取得します。
getFrom() : AddressList
setTo - Overwrite the address list in the To recipients.
setTo(
   string|AddressInterface|array|AddressList|Traversable $emailOrAddressList,
      null|string $name
) : void
  • setSubject -メッセージの件名ヘッダー値を設定します。
setSubject(string $subject) :void
  • setBody -メッセージ本文を設定します。
setBody(null|string|Zend\Mime\Message|object $body) : void

SMTPトランスポート層

*zend-mail* は、 *Zend \ Mail \ Transport \ Smtpclass* を介してSMTPサーバーを使用して電子メールを送信するオプションを提供します。 SMTPホスト、ポート、ユーザー名、パスワードなどを構成するいくつかの追加オプションがあることを除いて、 *Sendmail* に似ています。

部分的なコードは次のとおりです-

use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
$transport = new SmtpTransport();
$options = new SmtpOptions([
   'name' => 'localhost',
   'host' =>'smtp.gmail.com',
   'port' => 465,
]);
$transport->setOptions($options);

ここに、

  • name -SMTPホストの名前。
  • host -リモートホスト名またはIPアドレス。
  • port -リモートホストがリッスンしているポート。

メールの概念-例

次の点に従って、メールの概念を理解するための簡単なphpコンソールアプリケーションを作成しましょう。

  • フォルダ「mailapp」を作成します。
  • composerツールを使用して zend-mail をインストールします。
  • 「mailapp」フォルダー内にphpファイル Mail.php を作成します。
  • Zend \ Mail \ Message を使用してメッセージを作成します。
$message = new Message();
$message->addTo('[email protected]');
$message->addFrom('[email protected]');
$message->setSubject('Hello!');
$message->setBody("My first Zend-mail application!");
  • SMTPトランスポート層を作成し、必要な構成を追加します。
//Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions([
   'name' => 'localhost',
   'host' => 'smtp.gmail.com',//or any SMTP server
   'port' => 465,//port on which the SMTP server is listening
   'connection_class' => 'login',
   'connection_config' => [
      username' => '<your username>', 'password' => '<your password>',
      'ssl' => 'ssl'],
]);
$transport->setOptions($options);
  • send メソッドを使用してメールを送信します。
$transport->send($message);

完全なリスト、_Mail.php_は次のとおりです-

<?php
require __DIR__ . '/vendor/autoload.php';

use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;

$message = new Message();
$message->addTo('[email protected]');
$message->addFrom('[email protected]');
$message->setSubject('Hello!');
$message->setBody("My first Zend-mail application!");

//Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options = new SmtpOptions([
   'name' => 'localhost',
   'host' => 'smtp.gmail.com',//or any SMTP server
   'port' => 465,//port on which the SMTP server is listening
   'connection_class' => 'login',
   'connection_config' => [
      'username' => '<your username>', 'password' => '<your password>',
      'ssl' => 'ssl'],
]);
$transport->setOptions($options);
$transport->send($message);

ここで、コマンドプロンプト php Mail.php でアプリケーションを実行します。 これにより、アプリケーションで構成されたとおりにメールが送信されます。