From 91e14810895cae861421fddb179b5831fa0c1f3d Mon Sep 17 00:00:00 2001 From: Giovane Date: Tue, 13 Oct 2015 16:10:03 -0300 Subject: Fix send welcome email service method #484 Giovane & Jeff --- service/pixelated/adapter/model/mail.py | 21 ++++++++++++--------- service/pixelated/adapter/services/mail_service.py | 13 +++++++++++++ service/pixelated/assets/welcome.mail | 4 +--- 3 files changed, 26 insertions(+), 12 deletions(-) (limited to 'service/pixelated') diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 5a0a1b92..eda9ffb9 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -206,12 +206,15 @@ class InputMail(Mail): mime_multipart = MIMEMultipart() for header in ['To', 'Cc', 'Bcc']: - if self.headers[header]: + if self.headers.get(header): mime_multipart[header] = ", ".join(self.headers[header]) - if self.headers['Subject']: + if self.headers.get('Subject'): mime_multipart['Subject'] = self.headers['Subject'] + if self.headers.get('From'): + mime_multipart['From'] = self.headers['From'] + mime_multipart['Date'] = self.headers['Date'] if type(self.body) is list: for part in self.body: @@ -253,15 +256,15 @@ class InputMail(Mail): @staticmethod def from_python_mail(mail): input_mail = InputMail() - input_mail.headers = {key.capitalize(): value for key, value in mail.items()} - input_mail.headers['Date'] = date.mail_date_now() - input_mail.headers['Subject'] = mail['Subject'] - input_mail.headers['To'] = InputMail.FROM_EMAIL_ADDRESS - input_mail._mime = MIMEMultipart() + input_mail.headers = {unicode(key.capitalize()): unicode(value) for key, value in mail.items()} + input_mail.headers[u'Date'] = unicode(date.mail_date_now()) + input_mail.headers[u'To'] = [InputMail.FROM_EMAIL_ADDRESS] + for payload in mail.get_payload(): - input_mail._mime.attach(payload) + input_mail._mime_multipart.attach(payload) if payload.get_content_type() == 'text/plain': - input_mail.body = payload.as_string() + input_mail.body = unicode(payload.as_string()) + input_mail._mime = input_mail.to_mime_multipart() return input_mail diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index d9cb6517..9fd5589f 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -17,6 +17,8 @@ from twisted.internet import defer from pixelated.adapter.model.mail import InputMail from pixelated.adapter.model.status import Status from pixelated.adapter.services.tag_service import extract_reserved_tags +from email import message_from_file +import os class MailService(object): @@ -127,3 +129,14 @@ class MailService(object): @defer.inlineCallbacks def delete_permanent(self, mail_id): yield self.mail_store.delete_mail(mail_id) + + @defer.inlineCallbacks + def add_welcome_mail_to_inbox(self): + current_path = os.path.dirname(os.path.abspath(__file__)) + with open(os.path.join(current_path, '..', '..', 'assets', 'welcome.mail')) as mail_template_file: + mail_template = message_from_file(mail_template_file) + + input_mail = InputMail.from_python_mail(mail_template) + + mail = yield self.mail_store.add_mail('INBOX', input_mail.raw) + defer.returnValue(mail) diff --git a/service/pixelated/assets/welcome.mail b/service/pixelated/assets/welcome.mail index e85694f1..3f233143 100644 --- a/service/pixelated/assets/welcome.mail +++ b/service/pixelated/assets/welcome.mail @@ -5,9 +5,7 @@ To: Replace Content-Type: multipart/alternative; boundary=000boundary000 --000boundary000 -Content-Type: text/plain; charset=UTF-8 - -Welcome to Pixelated Mail, a modern email with encryption. +Welcome to Pixelated Mail, a modern email with encryption. Pixelated Mail is an open source project that aims to provide secure email on the browser with all the functionality we've come to expect of a modern email client. -- cgit v1.2.3