Fix detached instance issue in sender
This commit is contained in:
parent
595d87a76c
commit
daffe1eb22
|
|
@ -5,6 +5,10 @@ from email.mime.multipart import MIMEMultipart
|
|||
from email.mime.text import MIMEText
|
||||
from email.utils import formatdate
|
||||
|
||||
from logbook import Logger
|
||||
|
||||
logger = Logger(__name__)
|
||||
|
||||
|
||||
class Sender:
|
||||
def __init__(self, loop, from_addr, smtp_server, smtp_port, smtp_user=None, smtp_passwd=None):
|
||||
|
|
@ -15,17 +19,17 @@ class Sender:
|
|||
self.user = smtp_user
|
||||
self.passwd = smtp_passwd
|
||||
|
||||
def _send_mail(self, job, data):
|
||||
def _send_mail(self, title, article, format, email, data):
|
||||
msg = MIMEMultipart()
|
||||
msg['Subject'] = job.title
|
||||
msg['Subject'] = title
|
||||
msg['From'] = self.from_addr
|
||||
msg['To'] = job.user.kindle_mail
|
||||
msg['To'] = email
|
||||
msg['Date'] = formatdate(localtime=True)
|
||||
|
||||
mobi = MIMEApplication(data)
|
||||
encode_base64(mobi)
|
||||
mobi.add_header('Content-Disposition',
|
||||
'attachment; filename={id}.{format}'.format(id=job.article, format=job.format))
|
||||
'attachment; filename={id}.{format}'.format(id=article, format=format))
|
||||
|
||||
msg.attach(mobi)
|
||||
|
||||
|
|
@ -33,17 +37,21 @@ class Sender:
|
|||
smtp.starttls()
|
||||
if self.user is not None:
|
||||
smtp.login(self.user, self.passwd)
|
||||
smtp.sendmail(self.from_addr, job.user.kindle_mail, msg.as_string())
|
||||
smtp.sendmail(self.from_addr, [email], msg.as_string())
|
||||
smtp.quit()
|
||||
logger.info("Mail with article {article} in format {format} sent to {email}".format(article=article,
|
||||
format=format,
|
||||
email=email))
|
||||
|
||||
async def send_mail(self, job, data):
|
||||
return self.loop.run_in_executor(None, self._send_mail, job, data)
|
||||
return self.loop.run_in_executor(None, self._send_mail, job.title, job.article, job.format,
|
||||
job.user.kindle_mail, data)
|
||||
|
||||
def _send_warning(self, user, config):
|
||||
def _send_warning(self, email, config):
|
||||
msg = MIMEMultipart()
|
||||
msg['Subject'] = "Wallabag-Kindle-Consumer Notice"
|
||||
msg['From'] = self.from_addr
|
||||
msg['To'] = user.email
|
||||
msg['To'] = email
|
||||
msg['Date'] = formatdate(localtime=True)
|
||||
|
||||
txt = MIMEText(("the Wallabag-Kindle-Consumer for your Wallabag "
|
||||
|
|
@ -58,8 +66,9 @@ class Sender:
|
|||
smtp.starttls()
|
||||
if self.user is not None:
|
||||
smtp.login(self.user, self.passwd)
|
||||
smtp.sendmail(self.from_addr, user.email, msg.as_string())
|
||||
smtp.sendmail(self.from_addr, [email], msg.as_string())
|
||||
smtp.quit()
|
||||
logger.info("Notify mail sent to {user}", user=email)
|
||||
|
||||
async def send_warning(self, user, config):
|
||||
return self.loop.run_in_executor(None, self._send_warning, user, config)
|
||||
return self.loop.run_in_executor(None, self._send_warning, user.email, config)
|
||||
|
|
|
|||
Loading…
Reference in New Issue