|
|
|
|
@ -19,7 +19,6 @@ from logging import getLogger |
|
|
|
|
log = getLogger('mail') |
|
|
|
|
from itertools import chain |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
from email.mime.multipart import MIMEMultipart |
|
|
|
|
from email.mime.text import MIMEText |
|
|
|
|
@ -84,6 +83,7 @@ else: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LINK_RE = re.compile(r"https?://([^ \n]+\n)+[^ \n]+", re.MULTILINE) |
|
|
|
|
cid_rx = re.compile(r'^<(?P<id>.*)>$') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def html2text(html): |
|
|
|
|
@ -153,6 +153,7 @@ class NewsLetterSender(object): |
|
|
|
|
|
|
|
|
|
# |
|
|
|
|
self.announce = self.newsletter.dailymail |
|
|
|
|
self.local_dev = getattr(settings, 'LOCAL_DEV', False) |
|
|
|
|
|
|
|
|
|
def build_message(self, contact, announce_context=None): |
|
|
|
|
""" |
|
|
|
|
@ -190,6 +191,8 @@ class NewsLetterSender(object): |
|
|
|
|
message.attach(message_alt) |
|
|
|
|
|
|
|
|
|
for attachment in self.attachments: |
|
|
|
|
if self.newsletter.dailymail and hasattr(attachment, 'cid') and not attachment.cid in content_html: |
|
|
|
|
continue |
|
|
|
|
message.attach(attachment) |
|
|
|
|
|
|
|
|
|
if self.announce and announce_context: |
|
|
|
|
@ -267,7 +270,9 @@ class NewsLetterSender(object): |
|
|
|
|
def build_daily_attachments(self): |
|
|
|
|
attachments = [] |
|
|
|
|
for cid, path in dailymail_attahcments.iteritems(): |
|
|
|
|
attachments.append(self.gen_attachment_by_path(path, cid)) |
|
|
|
|
atchm = self.gen_attachment_by_path(path, cid) |
|
|
|
|
atchm.cid = 'cid:' + cid |
|
|
|
|
attachments.append(atchm) |
|
|
|
|
return attachments |
|
|
|
|
|
|
|
|
|
def build_announce_attachments(self, context): |
|
|
|
|
@ -439,15 +444,13 @@ class NewsLetterSender(object): |
|
|
|
|
|
|
|
|
|
if self.newsletter.status == Newsletter.WAITING: |
|
|
|
|
self.newsletter.status = Newsletter.SENDING |
|
|
|
|
if self.newsletter.status == Newsletter.SENDING and \ |
|
|
|
|
self.newsletter.mails_sent() >= \ |
|
|
|
|
self.newsletter.mailing_list.expedition_set().count(): |
|
|
|
|
if self.newsletter.status == Newsletter.SENDING and not self.expedition_list: |
|
|
|
|
self.newsletter.status = Newsletter.SENT |
|
|
|
|
if self.newsletter.ab_testing: |
|
|
|
|
self.newsletter.ab_final_stage = True |
|
|
|
|
|
|
|
|
|
if self.announce and not self.expedition_list: |
|
|
|
|
self.newsletter.status = Newsletter.SENT |
|
|
|
|
# if self.announce and not self.expedition_list: |
|
|
|
|
# self.newsletter.status = Newsletter.SENT |
|
|
|
|
|
|
|
|
|
self.newsletter.save() |
|
|
|
|
|
|
|
|
|
@ -521,6 +524,9 @@ class NewsLetterSender(object): |
|
|
|
|
print >>sys.stderr, 'smtp connection raises %s' % exception |
|
|
|
|
status = ContactMailingStatus.ERROR |
|
|
|
|
|
|
|
|
|
if self.newsletter.dailymail and status in [ContactMailingStatus.SENT_TEST, ContactMailingStatus.SENT]: |
|
|
|
|
contact.last_mailing_date = datetime.now().date() |
|
|
|
|
contact.save() |
|
|
|
|
params = { |
|
|
|
|
'newsletter': self.newsletter, |
|
|
|
|
'contact': contact, |
|
|
|
|
@ -551,7 +557,7 @@ class Mailer(NewsLetterSender): |
|
|
|
|
if not self.can_send: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
if not self.smtp: |
|
|
|
|
if not self.smtp and not self.local_dev: |
|
|
|
|
self.smtp_connect() |
|
|
|
|
|
|
|
|
|
if self.newsletter.dailymail: |
|
|
|
|
@ -583,9 +589,11 @@ class Mailer(NewsLetterSender): |
|
|
|
|
# pass |
|
|
|
|
if send: |
|
|
|
|
message = self.build_message(contact, announce_context) |
|
|
|
|
self.smtp.sendmail(self.newsletter.header_sender, |
|
|
|
|
if not self.local_dev: |
|
|
|
|
self.smtp.sendmail(self.newsletter.header_sender, |
|
|
|
|
contact.email, |
|
|
|
|
message.as_string()) |
|
|
|
|
# print(message) |
|
|
|
|
except (Exception, ) as e: |
|
|
|
|
exception = e |
|
|
|
|
print(exception) |
|
|
|
|
@ -615,7 +623,8 @@ class Mailer(NewsLetterSender): |
|
|
|
|
# маркируем оставшиеся контакты на второй этап |
|
|
|
|
self.mark_contacts_for_second_stage() |
|
|
|
|
|
|
|
|
|
self.smtp.quit() |
|
|
|
|
if not self.local_dev: |
|
|
|
|
self.smtp.quit() |
|
|
|
|
self.update_newsletter_status() |
|
|
|
|
|
|
|
|
|
def smtp_connect(self): |
|
|
|
|
@ -625,6 +634,8 @@ class Mailer(NewsLetterSender): |
|
|
|
|
@property |
|
|
|
|
def expedition_list(self): |
|
|
|
|
"""Build the expedition list""" |
|
|
|
|
self.second_stage_expedition_list_ids = [] |
|
|
|
|
|
|
|
|
|
self.credits = self.newsletter.server.credits() |
|
|
|
|
if self.credits <= 0: |
|
|
|
|
return [] |
|
|
|
|
@ -636,8 +647,6 @@ class Mailer(NewsLetterSender): |
|
|
|
|
|
|
|
|
|
qs = qs[:self.credits] |
|
|
|
|
|
|
|
|
|
self.second_stage_expedition_list_ids = [] |
|
|
|
|
|
|
|
|
|
if self.test: |
|
|
|
|
return qs |
|
|
|
|
|
|
|
|
|
|