diff --git a/pm_email_notify/pm_email_notify.admin.inc b/pm_email_notify/pm_email_notify.admin.inc
index a661137..d61efb8 100644
--- a/pm_email_notify/pm_email_notify.admin.inc
+++ b/pm_email_notify/pm_email_notify.admin.inc
@@ -16,6 +16,13 @@ function pm_email_notify_admin_settings_form() {
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
+ $form['pm_email']['privatemsg_setting_show_sender_mail'] = array(
+ '#type' => 'checkbox',
+ '#title' => t("Use sender's email address"),
+ '#description' => t("Use the sender's private email address instead of a generic sender for notification mails. This is the site default setting; users may override it individually."),
+ '#default_value' => variable_get('privatemsg_setting_show_sender_mail', FALSE),
+ '#weight' => 0,
+ );
$form['pm_email']['privatemsg_setting_email_notify_level'] = array(
'#type' => 'radios',
'#title' => t('Default e-mail notification level'),
diff --git a/pm_email_notify/pm_email_notify.module b/pm_email_notify/pm_email_notify.module
index 4d440f9..8c69f61 100644
--- a/pm_email_notify/pm_email_notify.module
+++ b/pm_email_notify/pm_email_notify.module
@@ -86,12 +86,12 @@ function _pm_email_notify_user_level($uid = NULL) {
}
/**
- * Check if a user should only be notified when addressed directly.
+ * Checks if a user should only be notified when addressed directly.
*
* @param $uid
* User ID.
*
- * @param
+ * @return bool
* TRUE if notifications should only be sent for directly addressed
* recipients.
*/
@@ -105,6 +105,25 @@ function _pm_email_notify_only_user($uid) {
}
/**
+ * Checks if a message author wants his email address as a sender.
+ *
+ * @param $uid
+ * The author's user ID.
+ *
+ * @return bool
+ * Whether or not to use the author's email address.
+ */
+function _pm_email_notify_show_sender($uid) {
+ // Either check the setting for this user or the global default.
+ $keys = array(
+ 'user' => array($uid),
+ 'global' => array(0),
+ );
+ return privatemsg_get_setting('show_sender_mail', $keys);
+}
+
+
+/**
* Retrieve notification setting of a user and check if they should receive
* an e-mail notification for a message.
*
@@ -115,6 +134,9 @@ function _pm_email_notify_only_user($uid) {
* User uid
* @param $message
* Message.
+ *
+ * @return bool
+ * Whether or not to notify the user in question.
*/
function _pm_email_notify_send_check($uid, $message) {
static $notifications = array();
@@ -164,8 +186,8 @@ function _pm_email_notify_send_check($uid, $message) {
/**
* Implements hook_privatemsg_message_insert().
*/
- function pm_email_notify_privatemsg_message_insert($message) {
- foreach ($message['recipients'] as $recipient) {
+function pm_email_notify_privatemsg_message_insert($message) {
+ foreach ($message['recipients'] as $recipient) {
pm_email_notify_send_mail($recipient, $message);
}
}
@@ -195,14 +217,25 @@ function pm_email_notify_privatemsg_message_recipient_changed($mid, $thread_id,
/**
* Send a pm notification email to a recipient.
+ *
+ * @param $recipient
+ * Recipient user object.
+ * @param $message
+ * Message object.
*/
function pm_email_notify_send_mail($recipient, $message) {
- // check if recipient enabled email notifications
+ // Check if the recipient enabled email notifications.
if (isset($recipient->uid) && !empty($recipient->mail) && _pm_email_notify_send_check($recipient->uid, $message)) {
- // send them a new pm notification email if they did
+ // Send them a new pm notification email if they did.
$params['recipient'] = $recipient;
$params['message'] = $message;
- $from = variable_get('pm_email_notify_from', '');
+ if (_pm_email_notify_show_sender($message['author']->uid)) {
+ // User author's email as a sender.
+ $from = $message['author']->mail;
+ }
+ else {
+ $from = variable_get('pm_email_notify_from', '');
+ }
drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL);
}
}
@@ -338,38 +371,47 @@ function _pm_email_notify_text($key, $language = NULL, $translate = TRUE) {
function pm_email_notify_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
- if ($category == 'account' && privatemsg_user_access('read privatemsg', $account)) {
- if (privatemsg_user_access('set privatemsg e-mail notification level')) {
- $form['privatemsg']['pm_email_notify_level'] = array(
- '#type' => 'radios',
- '#title' => t('Send me an e-mail notification...'),
- '#options' => array(
- PM_EMAIL_NOTIFY_LEVEL_DISABLED => t('Never.'),
- PM_EMAIL_NOTIFY_LEVEL_THREAD => t('Only for a new conversation'),
- PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE => t("Only once for a conversation until I've read the messages"),
- PM_EMAIL_NOTIFY_LEVEL_ALL => t('Every time I receive a message'),
- ),
- '#default_value' => _pm_email_notify_user_level($account->uid),
- );
- }
- else {
- // If the user does not have permissions to customize the notification
- // level, allow him to opt out of email notifications if they are not
- // disabled by default.
- $is_enabled = _pm_email_notify_user_level();
- $form['privatemsg']['pm_email_notify_level'] = array(
+ if ($category == 'account') {
+ $form['privatemsg']['pm_show_sender_mail'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Show my email address'),
+ '#description' => t('Use my private email address as a sender for notification mails to users I have sent private messages to. If unchecked, the notification will be sent by a generic account.'),
+ '#default_value' => _pm_email_notify_show_sender($account->uid),
+ '#access' => privatemsg_user_access('write privatemsg'),
+ );
+ if (privatemsg_user_access('read privatemsg', $account)) {
+ if (privatemsg_user_access('set privatemsg e-mail notification level')) {
+ $form['privatemsg']['pm_email_notify_level'] = array(
+ '#type' => 'radios',
+ '#title' => t('Send me an e-mail notification...'),
+ '#options' => array(
+ PM_EMAIL_NOTIFY_LEVEL_DISABLED => t('Never.'),
+ PM_EMAIL_NOTIFY_LEVEL_THREAD => t('Only for a new conversation'),
+ PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE => t("Only once for a conversation until I've read the messages"),
+ PM_EMAIL_NOTIFY_LEVEL_ALL => t('Every time I receive a message'),
+ ),
+ '#default_value' => _pm_email_notify_user_level($account->uid),
+ );
+ }
+ else {
+ // If the user does not have permissions to customize the notification
+ // level, allow him to opt out of email notifications if they are not
+ // disabled by default.
+ $is_enabled = _pm_email_notify_user_level();
+ $form['privatemsg']['pm_email_notify_level'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Receive email notification for incoming private messages'),
+ '#default_value' => $is_enabled ? PM_EMAIL_NOTIFY_LEVEL_DEFAULT : PM_EMAIL_NOTIFY_LEVEL_DISABLED,
+ '#access' => $is_enabled,
+ );
+ }
+ $form['privatemsg']['pm_email_only_user'] = array(
'#type' => 'checkbox',
- '#title' => t('Receive email notification for incoming private messages'),
- '#default_value' => $is_enabled ? PM_EMAIL_NOTIFY_LEVEL_DEFAULT : PM_EMAIL_NOTIFY_LEVEL_DISABLED,
- '#access' => $is_enabled,
+ '#title' => t("Don't send me e-mail notifications for mass messages."),
+ '#default_value' => _pm_email_notify_only_user($account->uid),
+ '#access' => privatemsg_user_access('change privatemsg e-mail notification for indirect messages'),
);
}
- $form['privatemsg']['pm_email_only_user'] = array(
- '#type' => 'checkbox',
- '#title' => t("Don't send me e-mail notifications for mass messages."),
- '#default_value' => _pm_email_notify_only_user($account->uid),
- '#access' => privatemsg_user_access('change privatemsg e-mail notification for indirect messages'),
- );
}
return $form;
@@ -379,16 +421,21 @@ function pm_email_notify_user($op, &$edit, &$account, $category = NULL) {
unset($edit['pm_email_notify_level']);
}
-
if (isset($edit['pm_email_only_user'])) {
privatemsg_set_setting('user', $account->uid, 'email_notify_only_user', $edit['pm_email_only_user']);
unset($edit['pm_email_only_user']);
}
+
+ if (isset($edit['pm_show_sender_mail'])) {
+ privatemsg_set_setting('user', $account->uid, 'show_sender_mail', $edit['pm_show_sender_mail']);
+ unset($edit['pm_show_sender_mail']);
+ }
break;
case 'delete':
privatemsg_del_setting('user', $account->uid, 'email_notify_level');
privatemsg_del_setting('user', $account->uid, 'email_notify_only_user');
+ privatemsg_del_setting('user', $account->uid, 'show_sender_mail');
break;
}
}
diff --git a/pm_email_notify/pm_email_notify.test b/pm_email_notify/pm_email_notify.test
index 6493015..e5b2cf2 100644
--- a/pm_email_notify/pm_email_notify.test
+++ b/pm_email_notify/pm_email_notify.test
@@ -28,7 +28,7 @@ class PrivatemsgEMailNotifyTestCase extends DrupalWebTestCase {
$this->author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'write privatemsg to all roles'));
$this->recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'set privatemsg e-mail notification level', 'change privatemsg e-mail notification for indirect messages'));
- $this->recipient2 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
+ $this->recipient2 = $this->drupalCreateUser(array('read privatemsg'));
$this->admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'administer privatemsg settings'));
$this->handledMails = 0;
}
@@ -369,6 +369,80 @@ class PrivatemsgEMailNotifyTestCase extends DrupalWebTestCase {
}
/**
+ * Test the "author's email address as notification sender" feature.
+ */
+ function testShowSenderMail() {
+
+ // Define a value for the "notification mails from" field.
+ $generic_sender = 'generic_sender@example.com';
+
+ // Test #1: Site wide setting is successfully stored (set to "show").
+
+ // Log in as admin and set the default to "show".
+ $this->drupalLogin($this->admin);
+ $edit = array(
+ 'privatemsg_setting_show_sender_mail' => TRUE,
+ // Also set the generic sender address while we're here.
+ 'pm_email_notify_from' => $generic_sender,
+ );
+ $this->drupalPost('admin/settings/messages/notify', $edit, t('Save configuration'));
+ $this->drupalGet('admin/settings/messages/notify');
+ // Verify correct defaults.
+ $this->assertFieldByName('privatemsg_setting_show_sender_mail', 1, "Use sender's email address value is now enabled in the form.");
+
+
+ // Test #2: User defaults match site-wide setting (currently "show").
+
+ // Log in as author and check for the custom field default.
+ $this->drupalLogin($this->author);
+ $this->drupalGet('user/' . $this->author->uid . '/edit');
+ $this->assertFieldByName('pm_show_sender_mail', 1, "User's default setting for show my email address is also enabled.");
+
+
+ // Test #3: User can override the site-wide setting (back to "hide")
+ // and his private mail address will be hidden.
+ $edit = array(
+ 'pm_show_sender_mail' => FALSE,
+ );
+ $this->drupalPost(NULL, $edit, t('Save'));
+ $this->drupalGet('user/' . $this->author->uid . '/edit');
+ $this->assertFieldByName('pm_show_sender_mail', 0, "User successfully disabled his show my email address setting.");
+
+ // Let author send a test mail.
+ $message = array(
+ 'recipient' => $this->recipient->name,
+ 'subject' => $this->randomName(),
+ 'body' => $this->randomName(20),
+ );
+ $this->drupalPost('messages/new', $message, t('Send message'));
+ $message['thread_id'] = $this->getCurrentThreadId();
+
+ $recipients = array($this->recipient);
+ $this->verifyMails($recipients, $this->author, $message, $generic_sender);
+
+
+ // Test #4: User can change his setting to "show" and his private
+ // mail address will be used.
+ $this->drupalGet('user/' . $this->author->uid . '/edit');
+ $edit = array(
+ 'pm_show_sender_mail' => TRUE,
+ );
+ $this->drupalPost(NULL, $edit, t('Save'));
+
+ // Let author send a test mail.
+ $message = array(
+ 'recipient' => $this->recipient->name,
+ 'subject' => $this->randomName(),
+ 'body' => $this->randomName(20),
+ );
+ $this->drupalPost('messages/new', $message, t('Send message'));
+ $message['thread_id'] = $this->getCurrentThreadId();
+
+ $recipients = array($this->recipient);
+ $this->verifyMails($recipients, $this->author, $message, $this->author->mail);
+ }
+
+ /**
* Extracts the current thread_id from the URl.
*
* @return
@@ -412,11 +486,13 @@ class PrivatemsgEMailNotifyTestCase extends DrupalWebTestCase {
* Author user object.
* @param $message
* Message array, containing the keys thread_id, body and subject.
+ * @param $expectedFrom
+ * The mail address expected to be the notification origin (optional).
*/
- function verifyMails($recipients, $author, $message) {
+ function verifyMails($recipients, $author, $message, $expectedFrom = NULL) {
$mails = $this->getNewMails(count($recipients));
foreach ($recipients as $id => $recipient) {
- $this->verifyMail($mails[$id], $recipient, $author, $message);
+ $this->verifyMail($mails[$id], $recipient, $author, $message, $expectedFrom);
}
}
@@ -431,8 +507,11 @@ class PrivatemsgEMailNotifyTestCase extends DrupalWebTestCase {
* Author user object.
* @param $message
* Message array, containing the keys thread_id, body and subject.
+ * @param $expectedFrom
+ * The mail address expected to be the notification origin (optional).
+ *
*/
- function verifyMail($mail, $recipient, $author, $message) {
+ function verifyMail($mail, $recipient, $author, $message, $expectedFrom = NULL) {
$this->assertEqual($mail['id'], 'pm_email_notify_notice', t('Correct notification id.'));
$this->assertEqual($mail['to'], $recipient->mail, t('Correct recipient e-mail address.'));
$params = array(
@@ -451,5 +530,10 @@ class PrivatemsgEMailNotifyTestCase extends DrupalWebTestCase {
$body = drupal_wrap_mail(strtr(_pm_email_notify_text('body'), $params));
$this->assertEqual($mail['subject'], $subject, t('Correct subject'));
$this->assertEqual($mail['body'], $body, t('Correct body'));
+
+ // Optionally verify the message's from address.
+ if (isset($expectedFrom)) {
+ $this->assertEqual($mail['from'], $expectedFrom, sprintf( 'Mail sender was %s, expected sender was %s', $mail['from'], $expectedFrom));
+ }
}
}
\ No newline at end of file