Hii there,
I’m trying to send an email through the CakePHP (3.9) SMTP class, however, this fails with an error (see title).
When I dd()
my email, this is the output (obviously I’ve blurred some sensitive information):
this is the code I’m using:
<?php
namespace Admiral\Admiral;
use Cake\Mailer\Email as CakeMail;
use Admiral\Admiral\Settings;
use Cake\Core\Configure;
class Email {
private $email = [];
private $cakeMail;
public function __construct() {
$this->cakeMail = new CakeMail();
$this->email['transport'] = 'default';
$this->email['template'] = 'default';
$this->email['from'] = $this->cakeMail->getConfigTransport('default')['from'];
}
public function set(string $option = '', $value) {
$this->email[$option] = $value;
}
public function get(string $option = null) {
if(!$option) {
return $this->email;
}
return $this->email[$option];
}
public function send() {
// Create the mail
$this->cakeMail->transport($this->email['transport']);
$this->cakeMail->setTo($this->email['to']);
$this->cakeMail->setFrom($this->email['from']['mail'], $this->email['from']['name']);
if(!empty($this->email['subject'])) $this->cakeMail->setSubject($this->email['subject']);
if(!empty($this->email['replyTo'])) $this->cakeMail->setReplyTo($this->email['replyTo']);
if(!empty($this->email['template'])) $this->cakeMail->viewBuilder()->setTemplate($this->email['template']);
if(!empty($this->email['format'])) $this->cakeMail->emailFormat($this->email['format']);
if(!empty($this->email['viewVars'])) $this->cakeMail->setViewVars($this->email['viewVars']);
$this->cakeMail->setDomain('finlaydag33k.nl');
$this->cakeMail->setProfile([
'transport' => 'default',
'from' => $this->email['from']['mail']
]);
// Send the email and return the result
try {
return $this->cakeMail->send();
} catch (\Exception $e) {
return $e;
}
}
}
Which gets called using this code:
// Build our email
$email = new Email();
$email->set('to', $user->email);
$email->set('subject', 'Your login code for ' . env('APP_NAME'));
$email->set('viewVars', [
'code' => $code,
]);
// Send the email
$status = $email->send();
Even using a smaller piece of code doesn’t seem to work (same error):
$email = new \Cake\Mailer\Email('default');
$email->from(['example@finlaydag33k.nl' => 'FinlayDaG33k'])
->to('me@finlaydag33k.nl')
->subject('Test Email')
->send('Test Email message');
// Send the email
$status = $email->send();