Skip to content

Commit

Permalink
Default recipient handling when a Cc or Bcc is specified without a To…
Browse files Browse the repository at this point in the history
… parameter
  • Loading branch information
JamesDPC committed Feb 5, 2018
1 parent 1ae8abb commit 8f2d0a6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ The message will be sent via a Queued Job depending on this setting and the mess
+ 'when-attachments' = Only when attachments are present

With a value of 'when-attachments' set, message delivery attempts without attachments will not use the queued job.
### default_recipient
Mailgun requires a 'to' parameter. If your system sends messages with Bcc/Cc but no 'To' then you will need to specify a default_recipient (one that you control).

## Sending
Sending of messages occurs via ```NSWDPC\SilverstripeMailgunSync\Connector\Message``` class using API configuration from YAML.
Expand Down
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NSWDPC\SilverstripeMailgunSync\Connector\Base:
workaround_testmode: false
# yes|no|when-attachments
send_via_job : 'when-attachments'
default_recipient : ''
# Event config
MailgunEvent:
secure_folder_name : 'SecureUploads'
Expand Down
18 changes: 17 additions & 1 deletion src/connector/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ abstract class Base {
private static $api_testmode = false;// when true ALL emails are sent with o:testmode = 'yes'
private static $sync_local_mime = false;// download messages when failed
private static $resubmit_failures = 2;// number of resubmit failures before the message is stored (when sync_local_mime is true)
private static $always_set_sender = true;
private static $track_userform = false;// track userform submissions (userform module support)

private static $workaround_testmode = false;// this works around an oddity where testmode is 'yes', the recipient is in the supression list but messages are 'delivered' in testmode

private static $send_via_job = 'when-attachments';
private static $default_recipient = '';

/**
* Returns an RFC2822 datetime in the format accepted by Mailgun
* @param string $relative a strtotime compatible format e.g 'now -4 weeks'
Expand Down Expand Up @@ -96,9 +100,21 @@ final protected function alwaysSetSender() {
final protected function applyTestMode(&$parameters) {
$mailgun_testmode = \Config::inst()->get(__CLASS__,'api_testmode');
if($mailgun_testmode) {
\SS_Log::log("applyTestMode - yes", \SS_Log::NOTICE);
//\SS_Log::log("applyTestMode - yes", \SS_Log::NOTICE);
$parameters['o:testmode'] = 'yes';
}
}

/**
* When Bcc/Cc is provided with no 'To', mailgun rejects the request (400 Bad Request), this method applies the configured default_recipient
*/
final public function applyDefaultRecipient(&$parameters) {
if(empty($parameters['to'])
&& (!empty($parameters['cc']) || !empty($parameters['bcc']))
&& ($default_recipient = \Config::inst()->get(__CLASS__,'default_recipient'))) {
//\SS_Log::log("applyDefaultRecipient - {$default_recipient}", \SS_Log::NOTICE);
$parameters['to'] = $default_recipient;
}
}

}
5 changes: 4 additions & 1 deletion src/connector/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function send($parameters) {
// apply Mailgun testmode if Config is set
$this->applyTestMode($parameters);

// if required, apply the default recipient
$this->applyDefaultRecipient($parameters);

$send_via_job = $this->sendViaJob();
//\SS_Log::log("send_via_job={$send_via_job}", \SS_Log::DEBUG);

Expand Down Expand Up @@ -373,5 +376,5 @@ protected function messageFileName() {
$filename = hash("md5", $time . $rand) . ".txt";
return $filename;
}

}
3 changes: 3 additions & 0 deletions src/jobs/SendJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function process() {
//\SS_Log::log("SendJob::process using domain {$domain}", \SS_Log::DEBUG);
//\SS_Log::log("SendJob::process to '{$parameters['to']}', from '{$parameters['from']}', subject '{$parameters['subject']}'", \SS_Log::DEBUG);

// if required, apply the default recipient
$connector->applyDefaultRecipient($parameters);
// decode all attachments
$connector->decodeAttachments($parameters);
$response = $client->messages()->send($domain, $parameters);

Expand Down

0 comments on commit 8f2d0a6

Please sign in to comment.