Skip to content

Commit

Permalink
Add cc and bcc to email_users
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarans committed Nov 14, 2019
1 parent 14ef420 commit 7aabd69
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,5 +1,5 @@
ckanapi==4.3
hdx-python-country==2.1.6
hdx-python-country==2.1.7
ndg-httpsclient==0.5.1
pyasn1==0.4.7
pyOpenSSL==19.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -6,7 +6,7 @@
from setuptools import setup, find_packages

requirements = ['ckanapi>=4.3',
'hdx-python-country>=2.1.6',
'hdx-python-country>=2.1.7',
'ndg-httpsclient',
'pyasn1',
'pyOpenSSL',
Expand Down
20 changes: 15 additions & 5 deletions src/hdx/data/user.py
Expand Up @@ -2,7 +2,7 @@
"""User class containing all logic for creating, checking, and updating users."""
import logging
from os.path import join
from typing import Optional, List
from typing import Optional, List, Any

import hdx.data.organization
from hdx.data.hdxobject import HDXObject
Expand Down Expand Up @@ -180,16 +180,19 @@ def get_all_users(configuration=None, **kwargs):
return users

@staticmethod
def email_users(users, subject, text_body, html_body=None, sender=None, configuration=None, **kwargs):
# type: (List['User'], str, str, Optional[str], Optional[str], Optional[Configuration], Any) -> None
def email_users(users, subject, text_body, html_body=None, sender=None, cc=None, bcc=None, configuration=None,
**kwargs):
# type: (List['User'], str, str, Optional[str], Optional[str], Optional[List[User]], Optional[List[User]], Optional[Configuration], Any) -> None
"""Email a list of users
Args:
users (List[User]): List of users
users (List[User]): List of users in To address
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
cc (Optional[List[User]]: List of users to cc. Defaults to None.
bcc (Optional[List[User]]: List of users to bcc. Defaults to None.
configuration (Optional[Configuration]): HDX configuration. Defaults to configuration of first user in list.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
Expand All @@ -203,9 +206,16 @@ def email_users(users, subject, text_body, html_body=None, sender=None, configur
recipients = list()
for user in users:
recipients.append(user.data['email'])
ccemails = list()
for user in cc:
ccemails.append(user.data['email'])
bccemails = list()
for user in bcc:
bccemails.append(user.data['email'])
if configuration is None:
configuration = users[0].configuration
configuration.emailer().send(recipients, subject, text_body, html_body=html_body, sender=sender, **kwargs)
configuration.emailer().send(recipients, subject, text_body, html_body=html_body, sender=sender, cc=ccemails,
bcc=bccemails, **kwargs)

def get_organizations(self, permission='read'):
# type: (str) -> List['Organization']
Expand Down
2 changes: 1 addition & 1 deletion src/hdx/version.txt
@@ -1 +1 @@
3.8.9
3.9.0
9 changes: 6 additions & 3 deletions tests/hdx/data/test_user.py
Expand Up @@ -374,20 +374,23 @@ def test_get_all_users(self, configuration, post_list, mocksmtp):
config = Configuration.read()
config.setup_emailer(email_config_dict=TestUser.email_config_dict)
User.email_users(users, TestUser.subject, TestUser.text_body, html_body=TestUser.html_body,
sender=TestUser.sender, mail_options=TestUser.mail_options, rcpt_options=TestUser.rcpt_options)
sender=TestUser.sender, cc=users, bcc=users, mail_options=TestUser.mail_options,
rcpt_options=TestUser.rcpt_options)
email = config.emailer()
assert email.server.type == 'smtpssl'
assert email.server.initargs == TestUser.smtp_initargs
assert email.server.username == TestUser.username
assert email.server.password == TestUser.password
assert email.server.sender == TestUser.sender
assert email.server.recipients == ['xxx@yyy.com', 'aaa@bbb.com']
assert email.server.recipients == ['xxx@yyy.com', 'aaa@bbb.com', 'xxx@yyy.com', 'aaa@bbb.com', 'xxx@yyy.com',
'aaa@bbb.com']
assert 'Content-Type: multipart/alternative;' in email.server.msg
assert '''\
MIME-Version: 1.0
Subject: hello
From: me@gmail.com
To: xxx@yyy.com, aaa@bbb.com''' in email.server.msg
To: xxx@yyy.com, aaa@bbb.com
Cc: xxx@yyy.com, aaa@bbb.com''' in email.server.msg
assert '''\
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Expand Down

0 comments on commit 7aabd69

Please sign in to comment.