Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify sender in email notification #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions notification/backends/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ def send(self, message, recipients, *args, **kwargs):
body = message['message.txt']
addresses = self.get_addresses(recipients)
if addresses:
return send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, addresses)
user_sender = kwargs.pop('overwrite_sender', False)
if user_sender:
try:
if user_sender.firstName:
sender_email = '%s <%s>' % (user_sender.firstName, user_sender.email,)
else:
sender_email = user_sender.email
except:
sender_email = settings.DEFAULT_FROM_EMAIL
else:
sender_email = settings.DEFAULT_FROM_EMAIL
return send_mail(subject, body, sender_email, addresses)


class MLStripper(HTMLParser):
Expand Down Expand Up @@ -64,8 +75,20 @@ def send(self, messages, recipients, *args, **kwargs):
if 'notification_type' in kwargs:
notification_type = kwargs.pop('notification_type')

user_sender = kwargs.pop('overwrite_sender', False)
if user_sender:
try:
if user_sender.firstName:
sender_email = '%s <%s>' % (user_sender.firstName, user_sender.email,)
else:
sender_email = user_sender.email
except:
sender_email = settings.DEFAULT_FROM_EMAIL
else:
sender_email = settings.DEFAULT_FROM_EMAIL

email = EmailMultiAlternatives(
subject, body, settings.DEFAULT_FROM_EMAIL, addresses)
subject, body, sender_email, addresses)
if notification_type:
email.extra_headers = {'X-SMTPAPI':'{"category": "%s"}' % notification_type}
email.attach_alternative(body_html, "text/html")
Expand Down
29 changes: 26 additions & 3 deletions notification/backends/mobile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from django.utils.translation import ugettext_lazy as _

from notification.backends.base import NotificationBackend
from push_notifications.models import APNSDevice, GCMDevice

import json
import copy
import json
import logging
from django.core.mail import send_mail
import sys

try:
from myproject.loggers import log_event
except ImportError:
log_event = None
error_logger = logging.getLogger("raven")

def _decode_list(data):
rv = []
Expand Down Expand Up @@ -58,12 +65,28 @@ def send(self, message, recipients, *args, **kwargs):

for dev in devices:
msg_dev = copy.copy(json_msg)
dev_type = ""
if isinstance(dev, APNSDevice):
dev_type = "iOS"
del msg_dev['msg']
else:
dev_type = "Android"
del msg_dev['aps']

print 'Sending notification to device: '+str(dev)
try:
if log_event is not None:
notification_dict = dict(
log_type="push_notification",
level_name="info",
action="sent",
notification_type=kwargs.get('notification_type', None),
device=dev_type
)
log_event(**notification_dict)
except Exception, e:
send_mail("Logging notification failed", str(e), "[email protected]", ["[email protected]", ], fail_silently=True)
error_logger.error(*sys.exc_info())
dev.send_message(msg_dev)
return True

10 changes: 9 additions & 1 deletion notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,15 @@ def send_user_notification(user, notice_type, backend, context):

if recipients:
try:
backend.send(message, recipients, notification_type=notice_type.label)
kwargs = {}
kwargs['notification_type'] = notice_type.label
if 'sender_album_code' in context and context['sender_album_code']:
kwargs['sender_album_code'] = context.get('sender_album_code')
if 'sender_album_title' in context and context['sender_album_title']:
kwargs['sender_album_title'] = context.get('sender_album_title')
if 'reply_to' in context and context['reply_to']:
kwargs['overwrite_reply_to'] = context.get('reply_to')
backend.send(message, recipients, **kwargs)
except TypeError, e:
print u"Tried to send notification to media %s. Send function raised an error." % (backend.title,)
raise e
Expand Down