カテゴリー
EC-CUBE

【EC-CUBE4】Commandクラス

定期お知らせメールを作成した際に触れたクラスです。
cron→bach→command

コマンドクラスからSwift_Messageでメール送信する

Commandクラス作成

コマンドで作成できます。

bin/console make:command

Commandクラスでメール送信テスト

https://github.com/EC-CUBE/ec-cube/issues/2116
githubから拝借させていただきました。

<?php
/* This file is part of EC-CUBE
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
* http://www.lockon.co.jp/
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/ 
namespace Eccube\Command;
 
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
 
class MailCommand extends Command
{
    protected static $defaultName = 'eccube:mail';

    /**
     * @var SymfonyStyle 
     */
     protected $io;

    public function __construct(\Swift_Mailer $mailer) { 
        parent::__construct();
        $this->mailer = $mailer;
    }

    protected function configure() {
        $this->setDescription('メールのテスト'); 
    }

    protected function initialize(InputInterface $input, OutputInterface $output) {
        $this->io = new SymfonyStyle($input, $output); 
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $message = (new \Swift_Message())
            ->setSubject('メール送信チェック')
            ->setFrom('test01@example.com')
            ->setTo('test02@example.com')
            ->setBcc('test03@example.com')
            ->setReplyTo('test04@example.com')
            ->setReturnPath('test05@example.com');
        $this->mailer->send($message);
        $this->io->success('メール送信テスト完了.'); 
   }
 }

コマンド名になります。

protected static $defaultName = 'eccube:mail';
bin/console eccube:mail

テスト用の叩き台です。

コマンドクラスの挙動確認後

サービスクラスを作成しそこで、メール送るリストなどで選別しメールを送り完了

作成者: kenshi

ゲーム大好きです。
ApexLegends, Arkやっています。
携帯販売員4年(240万)

プログラマー3年(350万)

フリーランスエンジニア1年(500万)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です