To change the usage mailer without changing the flow of calls in MailComponent, etc. as much as possible
We received a request to change the Mailer to PHPMailer so that we can also send emails to RFC violation email addresses.
I was able to install PHPMailer, create a file directly, operate it alone, and send an email.
However, I would like to change the mailer that I use without changing the code to one that does not default, but is it possible to do so?
Current Processing Flow
Currently invoking mail components from the controller
src/Controller/Component/MailComponent.php
public function sendMailConfirm($club,$change){
if($this->_isSend&!empty($change->mail))$this->getMailer('Customer')->send('mail_confirm',[$club,$change]);
}
Subsequently invoking and sending templates from the specified mailer
src/Mailer/CustomerMailer.php
public function mail_confirm($club,$change){
$from = Configure::read('Mailer.From');
$to = $change->mail;
$this
->from($from)
->to($to)
->subject('PIN Code Sent')
->template('mail_confirm', 'default')
->set(['pin_code'=>$change->pin_code]);
}
What I didn't understand
On the official website, they said they would create their own transport and use other mailers.
https://book.cakephp.org/3/ja/core-libraries/email.html#id9
However, I don't have enough understanding and I can't imagine how to use it at all
It is also not clear how to invoke the added proprietary transport.I was wondering if I could call the mailer I wanted to change and use it, so I briefly tried the following to see if I could connect it, but I got an error without the transportClass method.
src/Mailer/CustomerMailer.php
<?php
namespace App\Mailer;
use Cake\Core\Configure;
use Cake\Mailer\Mailer;
use Cake\Mailer\Email;
class CustomerMailer extensions Mailer {
・・・・
public function mail_confirm($club,$change){
$yourInstance=$this->getTransport()->transportClass();
$test=$yourInstance->myCustomMethod();
$this->log('Customer Mailer'.var_export($test,true));
$from = Configure::read('Mailer.From');
$to = $change->mail;
$this
->from($from)
->to($to)
->subject('PIN Code Sent')
->template('mail_confirm', 'default')
->set(['pin_code'=>$change->pin_code]);
}
Proprietary transport as a test
src/Mailer/Transport/PhpmailerTransport.php
<?php
namespace App\Mailer\Transport;
use Cake\Mailer\AbstractTransport;
use Cake\Mailer\Email;
class PhpmailerTransport extensions AbstractTransport
{
public function test(){
return 'from PhpmailerTransport message';
}
}
Integration is similar when changing this part
Or could you please let me know if you know any samples of Cakephp that use a mailer other than the default?
We only find a way to use PHPMailer directly, and we don't see an efficient way to switch.
Please let me know if there is anything wrong with the way of thinking itself.
Thank you for your cooperation
version
cakephp3.5
php7
PHPMailer v6.1.5
Use Email::setEmailPattern
to change the allowed email address pattern
Cake\Mailer\Email::emailPattern($pattern)|CakePHP Cookbook
Email class methods can also be used from the Mailer class, so
class CustomerMailer extensions Mailer
{
// ...snip
public function mail_confirm($club,$change)
{
$this->setEmailPattern('/^(?:[\p{L}0-9-._]+@[\p{L}0-9-._]+)$/ui');
// ...snip
}
Provide a regular expression of the email address you want to allow, as shown in .
If you want to create your own mail transport class, but not for this purpose, create a class that inherits Cake\Mailer\AbstractTransport
.AbstractTransport expects to implement the send(Email$mail)
method, so it creates a send method and creates a send process based on the email class given.
Create Your Own Transport|Email-3.8
class PhpmailerTransport extensions AbstractTransport
{
public function send (Email$email)
{
$phpmailer = new PHPMailer();
// TODO:setting up PHPMailer
// TODO: Get headers and message bodies from $email and set them to PHPMailer before sending them
}
Implement a class similar to , then add it to EmailTransport
in config/app.php
.
'EmailTransport'=>[
'default' = > [...],
'phpmailer' = > [
'className' = > 'Phpmailer',
// The value set here is passed to the constructor of the transport class.
// It will be available in the transport class `getConfig()`.
],
],
Finally, use setTransport()
to switch transport classes in the Mailer or Email classes.
class CustomerMailer extensions Mailer
{
// ...snip
public function mail_confirm($club,$change)
{
$this->setTransport('phpmailer');
// ...snip
}
© 2024 OneMinuteCode. All rights reserved.