Compare commits
5 Commits
7d0b766882
...
5045e947c9
Author | SHA1 | Date |
---|---|---|
Tony Air | 5045e947c9 | |
Tony Air | f7fd70c7f0 | |
Tony Air | 2875a006c8 | |
Tony Air | 914ad595f0 | |
Tony Air | cfdd069de5 |
|
@ -58,6 +58,8 @@ SilverStripe\Core\Injector\Injector:
|
|||
properties:
|
||||
Authenticators:
|
||||
default: '%$A2nt\CMSNiceties\Forms\Authenticator'
|
||||
SilverStripe\Control\Email\Email:
|
||||
class: A2nt\CMSNiceties\MailerFix
|
||||
|
||||
SilverStripe\UserForms\Form\UserForm:
|
||||
extensions:
|
||||
|
|
|
@ -16,7 +16,8 @@
|
|||
"silverstripe/cms": "*",
|
||||
"a2nt/silverstripe-elemental-basics": "*",
|
||||
"silverstripe/widgets": "*",
|
||||
"gorriecoe/silverstripe-linkfield": "*"
|
||||
"gorriecoe/silverstripe-linkfield": "*",
|
||||
"symfony/mailer": "^6"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
|
@ -2,19 +2,21 @@
|
|||
|
||||
namespace A2nt\CMSNiceties\Ajax\Ex;
|
||||
|
||||
use SilverStripe\CMS\Model\RedirectorPage;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Control\HTTPResponse;
|
||||
use SilverStripe\Core\Config\Config;
|
||||
use SilverStripe\Core\Extension;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\ErrorPage\ErrorPage;
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\HiddenField;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\ORM\ValidationResult;
|
||||
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
|
||||
use SilverStripe\Security\Security;
|
||||
use SilverStripe\UserForms\Control\UserDefinedFormController;
|
||||
use SilverStripe\View\SSViewer;
|
||||
|
||||
/**
|
||||
|
@ -270,4 +272,25 @@ class AjaxControllerEx extends Extension
|
|||
|
||||
$response->setBody($body);
|
||||
}
|
||||
|
||||
|
||||
public function isLegacy()
|
||||
{
|
||||
$object = $this->owner;
|
||||
|
||||
if (
|
||||
is_a($object, UserDefinedFormController::class)
|
||||
&& $object->getAction() === 'finished'
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $object->config()->get('legacy') || in_array(
|
||||
$object->ClassName,
|
||||
[
|
||||
RedirectorPage::class,
|
||||
ErrorPage::class,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace A2nt\CMSNiceties;
|
||||
|
||||
use RuntimeException;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\Email\Email;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
use Symfony\Component\Mailer\Transport;
|
||||
use Symfony\Component\Mime\Email as MimeEmail;
|
||||
|
||||
class MailerFix extends Email
|
||||
{
|
||||
private $args;
|
||||
|
||||
public function __construct(
|
||||
$from = null,
|
||||
$to = null,
|
||||
$subject = null,
|
||||
$body = null,
|
||||
$cc = null,
|
||||
$bcc = null,
|
||||
$returnPath = null
|
||||
) {
|
||||
$this->args = func_get_args();
|
||||
parent::__construct($from, $to, $subject, $body, $cc, $bcc, $returnPath);
|
||||
}
|
||||
|
||||
private static function convertVars($mails)
|
||||
{
|
||||
return is_array($mails) ? implode(',', $mails) : $mails;
|
||||
}
|
||||
|
||||
private function loadDetails()
|
||||
{
|
||||
$fields = [
|
||||
'From',
|
||||
'To',
|
||||
'Subject',
|
||||
'Body',
|
||||
'CC',
|
||||
'BCC',
|
||||
'ReturnPath',
|
||||
];
|
||||
|
||||
$i = 0;
|
||||
foreach ($fields as $f) {
|
||||
$func = 'get'.$f;
|
||||
|
||||
$v = $this->$func();
|
||||
|
||||
if ($v) {
|
||||
$this->args[$i] = is_array($v) ? array_keys($v) : $v;
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
$transport = Transport::fromDsn('native://default');//smtp://localhost
|
||||
$mailer = new Mailer($transport);
|
||||
|
||||
$this->loadDetails();
|
||||
$this->render();
|
||||
|
||||
$body = $this->getBody();
|
||||
$to = self::convertVars($this->args[1]);
|
||||
|
||||
$email = (new MimeEmail())
|
||||
->to($to)
|
||||
//->priority(Email::PRIORITY_HIGH)
|
||||
->subject($this->args[2])
|
||||
->text(strip_tags($body, []))
|
||||
->html($body);
|
||||
|
||||
$from = self::convertVars($this->args[0]);
|
||||
$from = $from ? $from : self::getDefaultFrom();
|
||||
if ($from) {
|
||||
$email->from($from);
|
||||
}
|
||||
|
||||
$cc = isset($this->args[4]) ? self::convertVars($this->args[4]) : null;
|
||||
if ($cc) {
|
||||
$email->cc($cc);
|
||||
}
|
||||
|
||||
$bcc = isset($this->args[5]) ? self::convertVars($this->args[5]) : null;
|
||||
if ($bcc) {
|
||||
$email->bcc($bcc);
|
||||
}
|
||||
|
||||
$reply = isset($this->args[6]) ? self::convertVars($this->args[6]) : null;
|
||||
if ($reply) {
|
||||
$email->replyTo($reply);
|
||||
}
|
||||
|
||||
return $mailer->send($email);
|
||||
//parent::send();
|
||||
}
|
||||
|
||||
private function getDefaultFrom(): string
|
||||
{
|
||||
// admin_email can have a string or an array config
|
||||
// https://docs.silverstripe.org/en/4/developer_guides/email/#administrator-emails
|
||||
$adminEmail = Email::config()->get('admin_email');
|
||||
if (is_array($adminEmail) && count($adminEmail ?? []) > 0) {
|
||||
$defaultFrom = array_keys($adminEmail)[0];
|
||||
} else {
|
||||
if (is_string($adminEmail)) {
|
||||
$defaultFrom = $adminEmail;
|
||||
} else {
|
||||
$defaultFrom = '';
|
||||
}
|
||||
}
|
||||
if (empty($defaultFrom)) {
|
||||
$host = Director::host();
|
||||
if (empty($host)) {
|
||||
throw new RuntimeException('Host not defined');
|
||||
}
|
||||
$defaultFrom = sprintf('noreply@%s', $host);
|
||||
}
|
||||
|
||||
return $defaultFrom;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue