$errors = static::checkSendable($mailing);
if (!empty($errors)) {
$fields = implode(',', array_keys($errors));
- throw new CRM_Core_Exception("Mailing cannot be sent. There are missing fields ($fields).", 'cannot-send', $errors);
+ throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors);
}
}
if (empty($mailing->body_html) && empty($mailing->body_text)) {
$errors['body'] = ts('Field "body_html" or "body_text" is required.');
}
+
+ if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'disable_mandatory_tokens_check')) {
+ $header = $mailing->header_id && $mailing->header_id != 'null' ? CRM_Mailing_BAO_Component::findById($mailing->header_id) : NULL;
+ $footer = $mailing->footer_id && $mailing->footer_id != 'null' ? CRM_Mailing_BAO_Component::findById($mailing->footer_id) : NULL;
+ foreach (array('body_html', 'body_text') as $field) {
+ if (empty($mailing->{$field})) {
+ continue;
+ }
+ $str = ($header ? $header->{$field} : '') . $mailing->{$field} . ($footer ? $footer->{$field} : '');
+ $err = CRM_Utils_Token::requiredTokens($str);
+ if ($err !== TRUE) {
+ foreach ($err as $token => $desc) {
+ $errors["{$field}:{$token}"] = ts('This message is missing a required token - {%1}: %2',
+ array(1 => $token, 2 => $desc)
+ );
+ }
+ }
+ }
+ }
+
return $errors;
}
$this->_email = 'test@test.test';
$this->_params = array(
'subject' => 'Hello {contact.display_name}',
- 'body_text' => "This is {contact.display_name}",
- 'body_html' => "<p>This is {contact.display_name}</p>",
+ 'body_text' => "This is {contact.display_name}.\n{domain.address}{action.optOutUrl}",
+ 'body_html' => "<p>This is {contact.display_name}.</p><p>{domain.address}{action.optOutUrl}</p>",
'name' => 'mailing name',
'created_id' => $this->_contactID,
'header_id' => '',
$previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
$this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
- $this->assertEquals("This is $displayName", $previewResult['values']['body_text']);
- $this->assertContains("<p>This is $displayName</p>", $previewResult['values']['body_html']);
+ $this->assertContains("This is $displayName", $previewResult['values']['body_text']);
+ $this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
}
public function testMailerPreviewRecipients() {
TRUE, //useLogin
array('name' => ''), // createParams
array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
- "/Mailing cannot be sent. There are missing fields \\(name\\)./", // expectedFailure
+ "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./", // expectedFailure
0, // expectedJobCount
);
+ $cases[] = array(
+ TRUE, //useLogin
+ array('body_html' => '', 'body_text' => ''), // createParams
+ array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
+ "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./", // expectedFailure
+ 0, // expectedJobCount
+ );
+ $cases[] = array(
+ TRUE, //useLogin
+ array('body_html' => 'Oops, did I leave my tokens at home?'), // createParams
+ array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
+ "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", // expectedFailure
+ 0, // expectedJobCount
+ );
+ $cases[] = array(
+ TRUE, //useLogin
+ array('body_text' => 'Oops, did I leave my tokens at home?'), // createParams
+ array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
+ "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure
+ 0, // expectedJobCount
+ );
+
return $cases;
}