1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-19 13:22:42 +01:00

Add a setting to disable all notification email

Summary: Ref T5861. Adds an option to opt out of all notification email. We'll still send you password resets, email verifications, etc.

Test Plan:
{F189484}

  - Added unit tests.
  - With preference set to different things, tried to send myself mail. Mail respected preferences.
  - Sent password reset email, which got through the preference.

Reviewers: btrahan, chad

Reviewed By: chad

Subscribers: rush898, epriestley

Maniphest Tasks: T5861

Differential Revision: https://secure.phabricator.com/D10237
This commit is contained in:
epriestley 2014-08-12 12:28:29 -07:00
parent 0196f53f9d
commit d011f8fdc6
10 changed files with 109 additions and 11 deletions

View file

@ -108,12 +108,9 @@ Phabricator
EOBODY; EOBODY;
} }
// NOTE: Don't set the user as 'from', or they may not receive the
// mail if they have the "don't send me email about my own actions"
// preference set.
$mail = id(new PhabricatorMetaMTAMail()) $mail = id(new PhabricatorMetaMTAMail())
->setSubject(pht('[Phabricator] Password Reset')) ->setSubject(pht('[Phabricator] Password Reset'))
->setForceDelivery(true)
->addRawTos(array($target_email->getAddress())) ->addRawTos(array($target_email->getAddress()))
->setBody($body) ->setBody($body)
->saveAndSend(); ->saveAndSend();

View file

@ -631,6 +631,7 @@ EOBODY;
id(new PhabricatorMetaMTAMail()) id(new PhabricatorMetaMTAMail())
->addRawTos(array($email->getAddress())) ->addRawTos(array($email->getAddress()))
->setSubject(pht('[Legalpad] Signature Verification')) ->setSubject(pht('[Legalpad] Signature Verification'))
->setForceDelivery(true)
->setBody($body) ->setBody($body)
->setRelatedPHID($signature->getDocumentPHID()) ->setRelatedPHID($signature->getDocumentPHID())
->saveAndSend(); ->saveAndSend();

View file

@ -242,6 +242,25 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
return $this->getParam('cc', array()); return $this->getParam('cc', array());
} }
/**
* Force delivery of a message, even if recipients have preferences which
* would otherwise drop the message.
*
* This is primarily intended to let users who don't want any email still
* receive things like password resets.
*
* @param bool True to force delivery despite user preferences.
* @return this
*/
public function setForceDelivery($force) {
$this->setParam('force', $force);
return $this;
}
public function getForceDelivery() {
return $this->getParam('force', false);
}
/** /**
* Flag that this is an auto-generated bulk message and should have bulk * Flag that this is an auto-generated bulk message and should have bulk
* headers added to it if appropriate. Broadly, this means some flavor of * headers added to it if appropriate. Broadly, this means some flavor of
@ -794,6 +813,11 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
return array(); return array();
} }
if ($this->getForceDelivery()) {
// If we're forcing delivery, skip all the opt-out checks.
return $actors;
}
// Exclude explicit recipients. // Exclude explicit recipients.
foreach ($this->getExcludeMailRecipientPHIDs() as $phid) { foreach ($this->getExcludeMailRecipientPHIDs() as $phid) {
$actor = idx($actors, $phid); $actor = idx($actors, $phid);
@ -834,17 +858,29 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
} }
} }
$all_prefs = id(new PhabricatorUserPreferences())->loadAllWhere(
'userPHID in (%Ls)',
$actor_phids);
$all_prefs = mpull($all_prefs, null, 'getUserPHID');
// Exclude recipients who don't want any mail.
foreach ($all_prefs as $phid => $prefs) {
$exclude = $prefs->getPreference(
PhabricatorUserPreferences::PREFERENCE_NO_MAIL,
false);
if ($exclude) {
$actors[$phid]->setUndeliverable(
pht(
'This recipient has disabled all email notifications '.
'(Settings > Email Preferences > Email Notifications).'));
}
}
// Exclude all recipients who have set preferences to not receive this type // Exclude all recipients who have set preferences to not receive this type
// of email (for example, a user who says they don't want emails about task // of email (for example, a user who says they don't want emails about task
// CC changes). // CC changes).
$tags = $this->getParam('mailtags'); $tags = $this->getParam('mailtags');
if ($tags) { if ($tags) {
$all_prefs = id(new PhabricatorUserPreferences())->loadAllWhere(
'userPHID in (%Ls)',
$actor_phids);
$all_prefs = mpull($all_prefs, null, 'getUserPHID');
foreach ($all_prefs as $phid => $prefs) { foreach ($all_prefs as $phid => $prefs) {
$user_mailtags = $prefs->getPreference( $user_mailtags = $prefs->getPreference(
PhabricatorUserPreferences::PREFERENCE_MAILTAGS, PhabricatorUserPreferences::PREFERENCE_MAILTAGS,

View file

@ -329,6 +329,7 @@ EOBODY
$mail = id(new PhabricatorMetaMTAMail()) $mail = id(new PhabricatorMetaMTAMail())
->setIsErrorEmail(true) ->setIsErrorEmail(true)
->setForceDelivery(true)
->setSubject($title) ->setSubject($title)
->addRawTos(array($from)) ->addRawTos(array($from))
->setBody($body) ->setBody($body)

View file

@ -72,7 +72,7 @@ final class PhabricatorMetaMTAMailTestCase extends PhabricatorTestCase {
'"To" is a recipient.'); '"To" is a recipient.');
// Test that the "No Self Mail" preference works correctly. // Test that the "No Self Mail" and "No Mail" preferences work correctly.
$mail->setFrom($phid); $mail->setFrom($phid);
$this->assertTrue( $this->assertTrue(
@ -92,6 +92,31 @@ final class PhabricatorMetaMTAMailTestCase extends PhabricatorTestCase {
PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL); PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL);
$prefs->save(); $prefs->save();
$this->assertTrue(
in_array($phid, $mail->buildRecipientList()),
'"From" does not exclude recipients by default.');
$prefs->setPreference(
PhabricatorUserPreferences::PREFERENCE_NO_MAIL,
true);
$prefs->save();
$this->assertFalse(
in_array($phid, $mail->buildRecipientList()),
'"From" excludes recipients with no-mail set.');
$mail->setForceDelivery(true);
$this->assertTrue(
in_array($phid, $mail->buildRecipientList()),
'"From" includes no-mail recipients when forced.');
$mail->setForceDelivery(false);
$prefs->unsetPreference(
PhabricatorUserPreferences::PREFERENCE_NO_MAIL);
$prefs->save();
$this->assertTrue( $this->assertTrue(
in_array($phid, $mail->buildRecipientList()), in_array($phid, $mail->buildRecipientList()),
'"From" does not exclude recipients by default.'); '"From" does not exclude recipients by default.');

View file

@ -45,6 +45,7 @@ final class PhabricatorPeopleApproveController
->addTos(array($user->getPHID())) ->addTos(array($user->getPHID()))
->addCCs(array($admin->getPHID())) ->addCCs(array($admin->getPHID()))
->setSubject('[Phabricator] '.$title) ->setSubject('[Phabricator] '.$title)
->setForceDelivery(true)
->setBody($body) ->setBody($body)
->saveAndSend(); ->saveAndSend();

View file

@ -540,6 +540,7 @@ EOBODY;
$mail = id(new PhabricatorMetaMTAMail()) $mail = id(new PhabricatorMetaMTAMail())
->addTos(array($this->getPHID())) ->addTos(array($this->getPHID()))
->setForceDelivery(true)
->setSubject('[Phabricator] Welcome to Phabricator') ->setSubject('[Phabricator] Welcome to Phabricator')
->setBody($body) ->setBody($body)
->saveAndSend(); ->saveAndSend();
@ -583,6 +584,7 @@ EOBODY;
$mail = id(new PhabricatorMetaMTAMail()) $mail = id(new PhabricatorMetaMTAMail())
->addTos(array($this->getPHID())) ->addTos(array($this->getPHID()))
->setForceDelivery(true)
->setSubject('[Phabricator] Username Changed') ->setSubject('[Phabricator] Username Changed')
->setBody($body) ->setBody($body)
->saveAndSend(); ->saveAndSend();

View file

@ -173,6 +173,7 @@ EOBODY;
id(new PhabricatorMetaMTAMail()) id(new PhabricatorMetaMTAMail())
->addRawTos(array($address)) ->addRawTos(array($address))
->setForceDelivery(true)
->setSubject('[Phabricator] Email Verification') ->setSubject('[Phabricator] Email Verification')
->setBody($body) ->setBody($body)
->setRelatedPHID($user->getPHID()) ->setRelatedPHID($user->getPHID())
@ -210,6 +211,7 @@ EOBODY;
id(new PhabricatorMetaMTAMail()) id(new PhabricatorMetaMTAMail())
->addRawTos(array($old_address)) ->addRawTos(array($old_address))
->setForceDelivery(true)
->setSubject('[Phabricator] Primary Address Changed') ->setSubject('[Phabricator] Primary Address Changed')
->setBody($body) ->setBody($body)
->setFrom($user->getPHID()) ->setFrom($user->getPHID())
@ -241,6 +243,7 @@ EOBODY;
id(new PhabricatorMetaMTAMail()) id(new PhabricatorMetaMTAMail())
->addRawTos(array($new_address)) ->addRawTos(array($new_address))
->setForceDelivery(true)
->setSubject('[Phabricator] Primary Address Changed') ->setSubject('[Phabricator] Primary Address Changed')
->setBody($body) ->setBody($body)
->setFrom($user->getPHID()) ->setFrom($user->getPHID())

View file

@ -20,10 +20,15 @@ final class PhabricatorSettingsPanelEmailPreferences
$preferences = $user->loadPreferences(); $preferences = $user->loadPreferences();
$pref_no_mail = PhabricatorUserPreferences::PREFERENCE_NO_MAIL;
$pref_no_self_mail = PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL; $pref_no_self_mail = PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL;
$errors = array(); $errors = array();
if ($request->isFormPost()) { if ($request->isFormPost()) {
$preferences->setPreference(
$pref_no_mail,
$request->getStr($pref_no_mail));
$preferences->setPreference( $preferences->setPreference(
$pref_no_self_mail, $pref_no_self_mail,
$request->getStr($pref_no_self_mail)); $request->getStr($pref_no_self_mail));
@ -56,6 +61,33 @@ final class PhabricatorSettingsPanelEmailPreferences
$form = new AphrontFormView(); $form = new AphrontFormView();
$form $form
->setUser($user) ->setUser($user)
->appendRemarkupInstructions(
pht(
'These settings let you control how Phabricator notifies you about '.
'events. You can configure Phabricator to send you an email, '.
'just send a web notification, or not notify you at all.'))
->appendRemarkupInstructions(
pht(
'If you disable **Email Notifications**, Phabricator will never '.
'send email to notify you about events. This preference overrides '.
'all your other settings.'.
"\n\n".
"//You may still receive some administrative email, like password ".
"reset email.//"))
->appendChild(
id(new AphrontFormSelectControl())
->setLabel(pht('Email Notifications'))
->setName($pref_no_mail)
->setOptions(
array(
'0' => pht('Send me email notifications'),
'1' => pht('Never send email notifications'),
))
->setValue($preferences->getPreference($pref_no_mail, 0)))
->appendRemarkupInstructions(
pht(
'If you disable **Self Actions**, Phabricator will not notify '.
'you about actions you take.'))
->appendChild( ->appendChild(
id(new AphrontFormSelectControl()) id(new AphrontFormSelectControl())
->setLabel(pht('Self Actions')) ->setLabel(pht('Self Actions'))
@ -65,7 +97,6 @@ final class PhabricatorSettingsPanelEmailPreferences
'0' => pht('Send me an email when I take an action'), '0' => pht('Send me an email when I take an action'),
'1' => pht('Do not send me an email when I take an action'), '1' => pht('Do not send me an email when I take an action'),
)) ))
->setCaption(pht('You can disable email about your own actions.'))
->setValue($preferences->getPreference($pref_no_self_mail, 0))); ->setValue($preferences->getPreference($pref_no_self_mail, 0)));
$mailtags = $preferences->getPreference('mailtags', array()); $mailtags = $preferences->getPreference('mailtags', array());

View file

@ -12,6 +12,7 @@ final class PhabricatorUserPreferences extends PhabricatorUserDAO {
const PREFERENCE_RE_PREFIX = 're-prefix'; const PREFERENCE_RE_PREFIX = 're-prefix';
const PREFERENCE_NO_SELF_MAIL = 'self-mail'; const PREFERENCE_NO_SELF_MAIL = 'self-mail';
const PREFERENCE_NO_MAIL = 'no-mail';
const PREFERENCE_MAILTAGS = 'mailtags'; const PREFERENCE_MAILTAGS = 'mailtags';
const PREFERENCE_VARY_SUBJECT = 'vary-subject'; const PREFERENCE_VARY_SUBJECT = 'vary-subject';