src/Eccube/Service/MailService.php line 355

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Service;
  13. use Doctrine\ORM\NonUniqueResultException;
  14. use Eccube\Common\EccubeConfig;
  15. use Eccube\Entity\BaseInfo;
  16. use Eccube\Entity\Customer;
  17. use Eccube\Entity\MailHistory;
  18. use Eccube\Entity\MailTemplate;
  19. use Eccube\Entity\Order;
  20. use Eccube\Entity\OrderItem;
  21. use Eccube\Entity\Shipping;
  22. use Eccube\Event\EccubeEvents;
  23. use Eccube\Event\EventArgs;
  24. use Eccube\Repository\BaseInfoRepository;
  25. use Eccube\Repository\MailHistoryRepository;
  26. use Eccube\Repository\MailTemplateRepository;
  27. use Symfony\Component\DependencyInjection\ContainerInterface;
  28. use Symfony\Component\EventDispatcher\EventDispatcher;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  30. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  31. use Symfony\Component\Mailer\MailerInterface;
  32. use Symfony\Component\Mime\Address;
  33. use Symfony\Component\Mime\Email;
  34. use Twig\Error\LoaderError;
  35. use Twig\Error\RuntimeError;
  36. use Twig\Error\SyntaxError;
  37. class MailService
  38. {
  39.     /**
  40.      * @var MailerInterface
  41.      */
  42.     protected $mailer;
  43.     /**
  44.      * @var MailTemplateRepository
  45.      */
  46.     protected $mailTemplateRepository;
  47.     /**
  48.      * @var MailHistoryRepository
  49.      */
  50.     protected $mailHistoryRepository;
  51.     /**
  52.      * @var EventDispatcher
  53.      */
  54.     protected $eventDispatcher;
  55.     /**
  56.      * @var BaseInfo
  57.      */
  58.     protected $BaseInfo;
  59.     /**
  60.      * @var EccubeConfig
  61.      */
  62.     protected $eccubeConfig;
  63.     /**
  64.      * @var \Twig\Environment
  65.      */
  66.     protected $twig;
  67.     /** @var ContainerInterface */
  68.     protected $container;
  69.     /**
  70.      * MailService constructor.
  71.      *
  72.      * @param MailerInterface $mailer
  73.      * @param MailTemplateRepository $mailTemplateRepository
  74.      * @param MailHistoryRepository $mailHistoryRepository
  75.      * @param BaseInfoRepository $baseInfoRepository
  76.      * @param EventDispatcherInterface $eventDispatcher
  77.      * @param \Twig\Environment $twig
  78.      * @param EccubeConfig $eccubeConfig
  79.      * @param ContainerInterface $container
  80.      */
  81.     public function __construct(
  82.         MailerInterface $mailer,
  83.         MailTemplateRepository $mailTemplateRepository,
  84.         MailHistoryRepository $mailHistoryRepository,
  85.         BaseInfoRepository $baseInfoRepository,
  86.         EventDispatcherInterface $eventDispatcher,
  87.         \Twig\Environment $twig,
  88.         EccubeConfig $eccubeConfig,
  89.         ContainerInterface $container
  90.     ) {
  91.         $this->mailer $mailer;
  92.         $this->mailTemplateRepository $mailTemplateRepository;
  93.         $this->mailHistoryRepository $mailHistoryRepository;
  94.         $this->BaseInfo $baseInfoRepository->get();
  95.         $this->eventDispatcher $eventDispatcher;
  96.         $this->eccubeConfig $eccubeConfig;
  97.         $this->twig $twig;
  98.         $this->container $container;
  99.     }
  100.     /**
  101.      * Send customer confirm mail.
  102.      *
  103.      * @param $Customer 会員情報
  104.      * @param string $activateUrl アクティベート用url
  105.      */
  106.     public function sendCustomerConfirmMail(Customer $Customer$activateUrl)
  107.     {
  108.         log_info('仮会員登録メール送信開始');
  109.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_confirm_mail_template_id']);
  110.         $body $this->twig->render($MailTemplate->getFileName(), [
  111.             'Customer' => $Customer,
  112.             'BaseInfo' => $this->BaseInfo,
  113.             'activateUrl' => $activateUrl,
  114.         ]);
  115.         $message = (new Email())
  116.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  117.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  118.             ->to($this->convertRFCViolatingEmail($Customer->getEmail()))
  119.             ->bcc($this->BaseInfo->getEmail01())
  120.             ->replyTo($this->BaseInfo->getEmail03())
  121.             ->returnPath($this->BaseInfo->getEmail04());
  122.         // HTMLテンプレートが存在する場合
  123.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  124.         if (!is_null($htmlFileName)) {
  125.             $htmlBody $this->twig->render($htmlFileName, [
  126.                 'Customer' => $Customer,
  127.                 'BaseInfo' => $this->BaseInfo,
  128.                 'activateUrl' => $activateUrl,
  129.             ]);
  130.             $message
  131.                 ->text($body)
  132.                 ->html($htmlBody);
  133.         } else {
  134.             $message->text($body);
  135.         }
  136.         $event = new EventArgs(
  137.             [
  138.                 'message' => $message,
  139.                 'Customer' => $Customer,
  140.                 'BaseInfo' => $this->BaseInfo,
  141.                 'activateUrl' => $activateUrl,
  142.             ],
  143.             null
  144.         );
  145.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_CUSTOMER_CONFIRM);
  146.         try {
  147.             $this->mailer->send($message);
  148.             log_info('仮会員登録メール送信完了');
  149.         } catch (TransportExceptionInterface $e) {
  150.             log_critical($e->getMessage());
  151.         }
  152.     }
  153.     /**
  154.      * Send customer complete mail.
  155.      *
  156.      * @param $Customer 会員情報
  157.      */
  158.     public function sendCustomerCompleteMail(Customer $Customer)
  159.     {
  160.         log_info('会員登録完了メール送信開始');
  161.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_complete_mail_template_id']);
  162.         $body $this->twig->render($MailTemplate->getFileName(), [
  163.             'Customer' => $Customer,
  164.             'BaseInfo' => $this->BaseInfo,
  165.         ]);
  166.         $message = (new Email())
  167.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  168.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  169.             ->to($this->convertRFCViolatingEmail($Customer->getEmail()))
  170.             ->bcc($this->BaseInfo->getEmail01())
  171.             ->replyTo($this->BaseInfo->getEmail03())
  172.             ->returnPath($this->BaseInfo->getEmail04());
  173.         // HTMLテンプレートが存在する場合
  174.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  175.         if (!is_null($htmlFileName)) {
  176.             $htmlBody $this->twig->render($htmlFileName, [
  177.                 'Customer' => $Customer,
  178.                 'BaseInfo' => $this->BaseInfo,
  179.             ]);
  180.             $message
  181.                 ->text($body)
  182.                 ->html($htmlBody);
  183.         } else {
  184.             $message->text($body);
  185.         }
  186.         $event = new EventArgs(
  187.             [
  188.                 'message' => $message,
  189.                 'Customer' => $Customer,
  190.                 'BaseInfo' => $this->BaseInfo,
  191.             ],
  192.             null
  193.         );
  194.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_CUSTOMER_COMPLETE);
  195.         try {
  196.             $this->mailer->send($message);
  197.             log_info('会員登録完了メール送信完了');
  198.         } catch (TransportExceptionInterface $e) {
  199.             log_critical($e->getMessage());
  200.         }
  201.     }
  202.     /**
  203.      * Send withdraw mail.
  204.      *
  205.      * @param $Customer Customer
  206.      * @param $email string
  207.      */
  208.     public function sendCustomerWithdrawMail(Customer $Customerstring $email)
  209.     {
  210.         log_info('退会手続き完了メール送信開始');
  211.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_customer_withdraw_mail_template_id']);
  212.         $body $this->twig->render($MailTemplate->getFileName(), [
  213.             'Customer' => $Customer,
  214.             'BaseInfo' => $this->BaseInfo,
  215.         ]);
  216.         $message = (new Email())
  217.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  218.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  219.             ->to($this->convertRFCViolatingEmail($email))
  220.             ->bcc($this->BaseInfo->getEmail01())
  221.             ->replyTo($this->BaseInfo->getEmail03())
  222.             ->returnPath($this->BaseInfo->getEmail04());
  223.         // HTMLテンプレートが存在する場合
  224.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  225.         if (!is_null($htmlFileName)) {
  226.             $htmlBody $this->twig->render($htmlFileName, [
  227.                 'Customer' => $Customer,
  228.                 'BaseInfo' => $this->BaseInfo,
  229.             ]);
  230.             $message
  231.                 ->text($body)
  232.                 ->html($htmlBody);
  233.         } else {
  234.             $message->text($body);
  235.         }
  236.         $event = new EventArgs(
  237.             [
  238.                 'message' => $message,
  239.                 'Customer' => $Customer,
  240.                 'BaseInfo' => $this->BaseInfo,
  241.                 'email' => $email,
  242.             ],
  243.             null
  244.         );
  245.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_CUSTOMER_WITHDRAW);
  246.         try {
  247.             $this->mailer->send($message);
  248.             log_info('退会手続き完了メール送信完了');
  249.         } catch (TransportExceptionInterface $e) {
  250.             log_critical($e->getMessage());
  251.         }
  252.     }
  253.     /**
  254.      * Send contact mail.
  255.      *
  256.      * @param $formData お問い合わせ内容
  257.      */
  258.     public function sendContactMail($formData)
  259.     {
  260.         log_info('お問い合わせ受付メール送信開始');
  261.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_contact_mail_template_id']);
  262.         $body $this->twig->render($MailTemplate->getFileName(), [
  263.             'data' => $formData,
  264.             'BaseInfo' => $this->BaseInfo,
  265.         ]);
  266.         // 問い合わせ者にメール送信
  267.         $message = (new Email())
  268.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  269.             ->from(new Address($this->BaseInfo->getEmail02(), $this->BaseInfo->getShopName()))
  270.             ->to($this->convertRFCViolatingEmail($formData['email']))
  271.             ->bcc($this->BaseInfo->getEmail02())
  272.             ->replyTo($this->BaseInfo->getEmail02())
  273.             ->returnPath($this->BaseInfo->getEmail04());
  274.         // HTMLテンプレートが存在する場合
  275.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  276.         if (!is_null($htmlFileName)) {
  277.             $htmlBody $this->twig->render($htmlFileName, [
  278.                 'data' => $formData,
  279.                 'BaseInfo' => $this->BaseInfo,
  280.             ]);
  281.             $message
  282.                 ->text($body)
  283.                 ->html($htmlBody);
  284.         } else {
  285.             $message->text($body);
  286.         }
  287.         $event = new EventArgs(
  288.             [
  289.                 'message' => $message,
  290.                 'formData' => $formData,
  291.                 'BaseInfo' => $this->BaseInfo,
  292.             ],
  293.             null
  294.         );
  295.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_CONTACT);
  296.         try {
  297.             $this->mailer->send($message);
  298.             log_info('お問い合わせ受付メール送信完了');
  299.         } catch (TransportExceptionInterface $e) {
  300.             log_critical($e->getMessage());
  301.         }
  302.     }
  303.     /**
  304.      * Send order mail.
  305.      *
  306.      * @param \Eccube\Entity\Order $Order 受注情報
  307.      *
  308.      * @return Email
  309.      */
  310.     public function sendOrderMail(Order $Order)
  311.     {
  312.         log_info('受注メール送信開始');
  313.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_order_mail_template_id']);
  314.         $body $this->twig->render($MailTemplate->getFileName(), [
  315.             'Order' => $Order,
  316.         ]);
  317.         $message = (new Email())
  318.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  319.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  320.             ->to($this->convertRFCViolatingEmail($Order->getEmail()))
  321.             ->bcc($this->BaseInfo->getEmail01())
  322.             ->replyTo($this->BaseInfo->getEmail03())
  323.             ->returnPath($this->BaseInfo->getEmail04());
  324.         // HTMLテンプレートが存在する場合
  325.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  326.         if (!is_null($htmlFileName)) {
  327.             $htmlBody $this->twig->render($htmlFileName, [
  328.                 'Order' => $Order,
  329.             ]);
  330.             $message
  331.                 ->text($body)
  332.                 ->html($htmlBody);
  333.         } else {
  334.             $message->text($body);
  335.         }
  336.         $event = new EventArgs(
  337.             [
  338.                 'message' => $message,
  339.                 'Order' => $Order,
  340.                 'MailTemplate' => $MailTemplate,
  341.                 'BaseInfo' => $this->BaseInfo,
  342.             ],
  343.             null
  344.         );
  345.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_ORDER);
  346.         try {
  347.             $this->mailer->send($message);
  348.         } catch (TransportExceptionInterface $e) {
  349.             log_critical($e->getMessage());
  350.         }
  351.         $MailHistory = new MailHistory();
  352.         $MailHistory->setMailSubject($message->getSubject())
  353.             ->setMailBody($message->getTextBody())
  354.             ->setOrder($Order)
  355.             ->setSendDate(new \DateTime());
  356.         // HTML用メールの設定
  357.         $htmlBody $message->getHtmlBody();
  358.         if (!empty($htmlBody)) {
  359.             $MailHistory->setMailHtmlBody($htmlBody);
  360.         }
  361.         $this->mailHistoryRepository->save($MailHistory);
  362.         log_info('受注メール送信完了');
  363.         return $message;
  364.     }
  365.     /**
  366.      * Send admin customer confirm mail.
  367.      *
  368.      * @param $Customer 会員情報
  369.      * @param string $activateUrl アクティベート用url
  370.      */
  371.     public function sendAdminCustomerConfirmMail(Customer $Customer$activateUrl)
  372.     {
  373.         log_info('仮会員登録再送メール送信開始');
  374.         /* @var $MailTemplate \Eccube\Entity\MailTemplate */
  375.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_confirm_mail_template_id']);
  376.         $body $this->twig->render($MailTemplate->getFileName(), [
  377.             'BaseInfo' => $this->BaseInfo,
  378.             'Customer' => $Customer,
  379.             'activateUrl' => $activateUrl,
  380.         ]);
  381.         $message = (new Email())
  382.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  383.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  384.             ->to($this->convertRFCViolatingEmail($Customer->getEmail()))
  385.             ->bcc($this->BaseInfo->getEmail01())
  386.             ->replyTo($this->BaseInfo->getEmail03())
  387.             ->returnPath($this->BaseInfo->getEmail04());
  388.         // HTMLテンプレートが存在する場合
  389.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  390.         if (!is_null($htmlFileName)) {
  391.             $htmlBody $this->twig->render($htmlFileName, [
  392.                 'BaseInfo' => $this->BaseInfo,
  393.                 'Customer' => $Customer,
  394.                 'activateUrl' => $activateUrl,
  395.             ]);
  396.             $message
  397.                 ->text($body)
  398.                 ->html($htmlBody);
  399.         } else {
  400.             $message->text($body);
  401.         }
  402.         $event = new EventArgs(
  403.             [
  404.                 'message' => $message,
  405.                 'Customer' => $Customer,
  406.                 'BaseInfo' => $this->BaseInfo,
  407.                 'activateUrl' => $activateUrl,
  408.             ],
  409.             null
  410.         );
  411.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_ADMIN_CUSTOMER_CONFIRM);
  412.         try {
  413.             $this->mailer->send($message);
  414.             log_info('仮会員登録再送メール送信完了');
  415.         } catch (TransportExceptionInterface $e) {
  416.             log_critical($e->getMessage());
  417.         }
  418.     }
  419.     /**
  420.      * Send admin order mail.
  421.      *
  422.      * @param Order $Order 受注情報
  423.      * @param $formData 入力内容
  424.      *
  425.      * @return Email
  426.      *
  427.      * @throws \Twig_Error_Loader
  428.      * @throws \Twig_Error_Runtime
  429.      * @throws \Twig_Error_Syntax
  430.      */
  431.     public function sendAdminOrderMail(Order $Order$formData)
  432.     {
  433.         log_info('受注管理通知メール送信開始');
  434.         $message = (new Email())
  435.             ->subject('['.$this->BaseInfo->getShopName().'] '.$formData['mail_subject'])
  436.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  437.             ->to($this->convertRFCViolatingEmail($Order->getEmail()))
  438.             ->bcc($this->BaseInfo->getEmail01())
  439.             ->replyTo($this->BaseInfo->getEmail03())
  440.             ->returnPath($this->BaseInfo->getEmail04())
  441.             ->text($formData['tpl_data']);
  442.         $event = new EventArgs(
  443.             [
  444.                 'message' => $message,
  445.                 'Order' => $Order,
  446.                 'formData' => $formData,
  447.                 'BaseInfo' => $this->BaseInfo,
  448.             ],
  449.             null
  450.         );
  451.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_ADMIN_ORDER);
  452.         try {
  453.             $this->mailer->send($message);
  454.             log_info('受注管理通知メール送信完了');
  455.         } catch (TransportExceptionInterface $e) {
  456.             log_critical($e->getMessage());
  457.         }
  458.         return $message;
  459.     }
  460.     /**
  461.      * Send password reset notification mail.
  462.      *
  463.      * @param $Customer 会員情報
  464.      * @param string $reset_url
  465.      */
  466.     public function sendPasswordResetNotificationMail(Customer $Customer$reset_url)
  467.     {
  468.         log_info('パスワード再発行メール送信開始');
  469.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_forgot_mail_template_id']);
  470.         $body $this->twig->render($MailTemplate->getFileName(), [
  471.             'BaseInfo' => $this->BaseInfo,
  472.             'Customer' => $Customer,
  473.             'expire' => $this->eccubeConfig['eccube_customer_reset_expire'],
  474.             'reset_url' => $reset_url,
  475.         ]);
  476.         $message = (new Email())
  477.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  478.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  479.             ->to($this->convertRFCViolatingEmail($Customer->getEmail()))
  480.             ->bcc($this->BaseInfo->getEmail01())
  481.             ->replyTo($this->BaseInfo->getEmail03())
  482.             ->returnPath($this->BaseInfo->getEmail04());
  483.         // HTMLテンプレートが存在する場合
  484.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  485.         if (!is_null($htmlFileName)) {
  486.             $htmlBody $this->twig->render($htmlFileName, [
  487.                 'BaseInfo' => $this->BaseInfo,
  488.                 'Customer' => $Customer,
  489.                 'expire' => $this->eccubeConfig['eccube_customer_reset_expire'],
  490.                 'reset_url' => $reset_url,
  491.             ]);
  492.             $message
  493.                 ->text($body)
  494.                 ->html($htmlBody);
  495.         } else {
  496.             $message->text($body);
  497.         }
  498.         $event = new EventArgs(
  499.             [
  500.                 'message' => $message,
  501.                 'Customer' => $Customer,
  502.                 'BaseInfo' => $this->BaseInfo,
  503.                 'resetUrl' => $reset_url,
  504.             ],
  505.             null
  506.         );
  507.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_PASSWORD_RESET);
  508.         try {
  509.             $this->mailer->send($message);
  510.             log_info('パスワード再発行メール送信完了');
  511.         } catch (TransportExceptionInterface $e) {
  512.             log_critical($e->getMessage());
  513.         }
  514.     }
  515.     /**
  516.      * Send password reset notification mail.
  517.      *
  518.      * @param $Customer 会員情報
  519.      * @param string $password
  520.      */
  521.     public function sendPasswordResetCompleteMail(Customer $Customer$password)
  522.     {
  523.         log_info('パスワード変更完了メール送信開始');
  524.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_reset_complete_mail_template_id']);
  525.         $body $this->twig->render($MailTemplate->getFileName(), [
  526.             'BaseInfo' => $this->BaseInfo,
  527.             'Customer' => $Customer,
  528.             'password' => $password,
  529.         ]);
  530.         $message = (new Email())
  531.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  532.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  533.             ->to($this->convertRFCViolatingEmail($Customer->getEmail()))
  534.             ->bcc($this->BaseInfo->getEmail01())
  535.             ->replyTo($this->BaseInfo->getEmail03())
  536.             ->returnPath($this->BaseInfo->getEmail04());
  537.         // HTMLテンプレートが存在する場合
  538.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  539.         if (!is_null($htmlFileName)) {
  540.             $htmlBody $this->twig->render($htmlFileName, [
  541.                 'BaseInfo' => $this->BaseInfo,
  542.                 'Customer' => $Customer,
  543.                 'password' => $password,
  544.             ]);
  545.             $message
  546.                 ->text($body)
  547.                 ->html($htmlBody);
  548.         } else {
  549.             $message->text($body);
  550.         }
  551.         $event = new EventArgs(
  552.             [
  553.                 'message' => $message,
  554.                 'Customer' => $Customer,
  555.                 'BaseInfo' => $this->BaseInfo,
  556.                 'password' => $password,
  557.             ],
  558.             null
  559.         );
  560.         $this->eventDispatcher->dispatch($eventEccubeEvents::MAIL_PASSWORD_RESET_COMPLETE);
  561.         try {
  562.             $this->mailer->send($message);
  563.             log_info('パスワード変更完了メール送信完了');
  564.         } catch (TransportExceptionInterface $e) {
  565.             log_critical($e->getMessage());
  566.         }
  567.     }
  568.     /**
  569.      * 発送通知メールを送信する.
  570.      * 発送通知メールは受注ごとに送られる
  571.      *
  572.      * @param Shipping $Shipping
  573.      *
  574.      * @throws \Twig_Error
  575.      */
  576.     public function sendShippingNotifyMail(Shipping $Shipping)
  577.     {
  578.         log_info('出荷通知メール送信処理開始', ['id' => $Shipping->getId()]);
  579.         $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']);
  580.         /** @var Order $Order */
  581.         $Order $Shipping->getOrder();
  582.         $body $this->getShippingNotifyMailBody($Shipping$Order$MailTemplate->getFileName());
  583.         $message = (new Email())
  584.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  585.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  586.             ->to($this->convertRFCViolatingEmail($Order->getEmail()))
  587.             ->bcc($this->BaseInfo->getEmail01())
  588.             ->replyTo($this->BaseInfo->getEmail03())
  589.             ->returnPath($this->BaseInfo->getEmail04());
  590.         // HTMLテンプレートが存在する場合
  591.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  592.         if (!is_null($htmlFileName)) {
  593.             $htmlBody $this->getShippingNotifyMailBody($Shipping$Order$htmlFileNametrue);
  594.             $message
  595.                 ->text($body)
  596.                 ->html($htmlBody);
  597.         } else {
  598.             $message->text($body);
  599.         }
  600.         try {
  601.             $this->mailer->send($message);
  602.         } catch (TransportExceptionInterface $e) {
  603.             log_critical($e->getMessage());
  604.         }
  605.         $MailHistory = new MailHistory();
  606.         $MailHistory->setMailSubject($message->getSubject())
  607.                 ->setMailBody($message->getTextBody())
  608.                 ->setOrder($Order)
  609.                 ->setSendDate(new \DateTime());
  610.         // HTML用メールの設定
  611.         $htmlBody $message->getHtmlBody();
  612.         if (!empty($htmlBody)) {
  613.             $MailHistory->setMailHtmlBody($htmlBody);
  614.         }
  615.         $this->mailHistoryRepository->save($MailHistory);
  616.         log_info('出荷通知メール送信処理完了', ['id' => $Shipping->getId()]);
  617.     }
  618.     /**
  619.      * @param Shipping $Shipping
  620.      * @param Order $Order
  621.      * @param string|null $templateName
  622.      * @param boolean $is_html
  623.      *
  624.      * @return string
  625.      *
  626.      * @throws \Twig_Error
  627.      */
  628.     public function getShippingNotifyMailBody(Shipping $ShippingOrder $Order$templateName null$is_html false)
  629.     {
  630.         $ShippingItems array_filter($Shipping->getOrderItems()->toArray(), function (OrderItem $OrderItem) use ($Order) {
  631.             return $OrderItem->getOrderId() === $Order->getId();
  632.         });
  633.         if (is_null($templateName)) {
  634.             /** @var MailTemplate $MailTemplate */
  635.             $MailTemplate $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']);
  636.             $fileName $MailTemplate->getFileName();
  637.         } else {
  638.             $fileName $templateName;
  639.         }
  640.         if ($is_html) {
  641.             $htmlFileName $this->getHtmlTemplate($fileName);
  642.             $fileName = !is_null($htmlFileName) ? $htmlFileName $fileName;
  643.         }
  644.         return $this->twig->render($fileName, [
  645.             'Shipping' => $Shipping,
  646.             'ShippingItems' => $ShippingItems,
  647.             'Order' => $Order,
  648.         ]);
  649.     }
  650.     /**
  651.      * 会員情報変更時にメール通知
  652.      *
  653.      * @param Customer $Customer
  654.      * @param array $userData
  655.      *  - userAgent
  656.      *  - ipAddress
  657.      *  - preEmail
  658.      * @param string $eventName
  659.      *
  660.      * @return void
  661.      *
  662.      * @throws LoaderError
  663.      * @throws NonUniqueResultException
  664.      * @throws RuntimeError
  665.      * @throws SyntaxError
  666.      */
  667.     public function sendCustomerChangeNotifyMail(Customer $Customer, array $userDatastring $eventName)
  668.     {
  669.         log_info('会員情報変更通知メール送信処理開始');
  670.         log_info($eventName);
  671.         // メールテンプレートの取得 IDでの取得は現行環境での差異があるため
  672.         $tpl_name 'Mail/customer_change_notify.twig';
  673.         $MailTemplate $this->mailTemplateRepository->createQueryBuilder('mt')
  674.             ->where('mt.file_name = :file_name')
  675.             ->setParameter('file_name'$tpl_name)
  676.             ->getQuery()
  677.             ->getOneOrNullResult();
  678.         $body $this->twig->render($MailTemplate->getFileName(), [
  679.             'BaseInfo' => $this->BaseInfo,
  680.             'Customer' => $Customer,
  681.             'userAgent' => $userData['userAgent'],
  682.             'eventName' => $eventName,
  683.             'ipAddress' => $userData['ipAddress'],
  684.         ]);
  685.         $message = (new Email())
  686.             ->subject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
  687.             ->from(new Address($this->BaseInfo->getEmail01(), $this->BaseInfo->getShopName()))
  688.             ->to($this->convertRFCViolatingEmail($Customer->getEmail()))
  689.             ->bcc($this->BaseInfo->getEmail01())
  690.             ->replyTo($this->BaseInfo->getEmail03())
  691.             ->returnPath($this->BaseInfo->getEmail04());
  692.         // HTMLテンプレートが存在する場合
  693.         $htmlFileName $this->getHtmlTemplate($MailTemplate->getFileName());
  694.         if (!is_null($htmlFileName)) {
  695.             $htmlBody $this->twig->render($htmlFileName, [
  696.                 'BaseInfo' => $this->BaseInfo,
  697.                 'Customer' => $Customer,
  698.                 'userAgent' => $userData['userAgent'],
  699.                 'eventName' => $eventName,
  700.                 'ipAddress' => $userData['ipAddress'],
  701.             ]);
  702.             $message
  703.                 ->text($body)
  704.                 ->html($htmlBody);
  705.         } else {
  706.             $message->text($body);
  707.         }
  708.         try {
  709.             $this->mailer->send($message);
  710.         } catch (TransportExceptionInterface $e) {
  711.             log_critical($e->getMessage());
  712.         }
  713.         // メールアドレスの変更があった場合、変更前のメールアドレスにも送信
  714.         if (isset($userData['preEmail']) && $Customer->getEmail() != $userData['preEmail']) {
  715.             $message->to($this->convertRFCViolatingEmail($userData['preEmail']));
  716.             // メール送信
  717.             try {
  718.                 $this->mailer->send($message);
  719.             } catch (TransportExceptionInterface $e) {
  720.                 log_critical($e->getMessage());
  721.             }
  722.         }
  723.         log_info('会員情報変更通知メール送信処理完了');
  724.     }
  725.     /**
  726.      * [getHtmlTemplate description]
  727.      *
  728.      * @param  string $templateName  プレーンテキストメールのファイル名
  729.      *
  730.      * @return string|null  存在する場合はファイル名を返す
  731.      */
  732.     public function getHtmlTemplate($templateName)
  733.     {
  734.         // メールテンプレート名からHTMLメール用テンプレート名を生成
  735.         $fileName explode('.'$templateName);
  736.         $suffix '.html';
  737.         $htmlFileName $fileName[0].$suffix.'.'.$fileName[1];
  738.         // HTMLメール用テンプレートの存在チェック
  739.         if ($this->twig->getLoader()->exists($htmlFileName)) {
  740.             return $htmlFileName;
  741.         } else {
  742.             return null;
  743.         }
  744.     }
  745.     /**
  746.      * RFC違反のメールの local part を "" で囲む.
  747.      *
  748.      * パラメータ eccube_rfc_email_check == true の場合は変換しない
  749.      *
  750.      * @param string $email
  751.      *
  752.      * @return Address
  753.      */
  754.     public function convertRFCViolatingEmail(string $email): Address
  755.     {
  756.         if ($this->container->getParameter('eccube_rfc_email_check')) {
  757.             return new Address($email);
  758.         }
  759.         // see https://blog.everqueue.com/chiba/2009/03/22/163/
  760.         $wsp '[\x20\x09]';
  761.         $vchar '[\x21-\x7e]';
  762.         $quoted_pair "\\\\(?:$vchar|$wsp)";
  763.         $qtext '[\x21\x23-\x5b\x5d-\x7e]';
  764.         $qcontent "(?:$qtext|$quoted_pair)";
  765.         $quoted_string "\"$qcontent*\"";
  766.         $atext '[a-zA-Z0-9!#$%&\'*+\-\/\=?^_`{|}~]';
  767.         $dot_atom "$atext+(?:[.]$atext+)*";
  768.         $local_part "(?:$dot_atom|$quoted_string)";
  769.         $domain $dot_atom;
  770.         $addr_spec "{$local_part}[@]$domain";
  771.         $dot_atom_loose "$atext+(?:[.]|$atext)*";
  772.         $local_part_loose "(?:$dot_atom_loose|$quoted_string)";
  773.         $addr_spec_loose "{$local_part_loose}[@]$domain";
  774.         $regexp "/\A{$addr_spec}\z/";
  775.         if (!preg_match($regexp$email)) {
  776.             $email preg_replace('/^(.*)@(.*)$/''"$1"@$2'$email);
  777.         }
  778.         return new Address($email);
  779.     }
  780. }