mirror of
https://github.com/janeczku/calibre-web
synced 2025-12-09 09:48:05 +00:00
Merge branch 'master' into cover_thumbnail
# Conflicts: # cps/editbooks.py # cps/helper.py # cps/web.py # test/Calibre-Web TestSummary_Linux.html
This commit is contained in:
@@ -35,6 +35,8 @@ from cps.ub import init_db_thread
|
||||
|
||||
from cps.tasks.mail import TaskEmail
|
||||
from cps import gdriveutils
|
||||
|
||||
|
||||
log = logger.create()
|
||||
|
||||
|
||||
|
||||
88
cps/tasks/mail.py
Normal file → Executable file
88
cps/tasks/mail.py
Normal file → Executable file
@@ -16,24 +16,15 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import sys
|
||||
import os
|
||||
import smtplib
|
||||
import threading
|
||||
import socket
|
||||
import mimetypes
|
||||
|
||||
try:
|
||||
from StringIO import StringIO
|
||||
from email.MIMEBase import MIMEBase
|
||||
from email.MIMEMultipart import MIMEMultipart
|
||||
from email.MIMEText import MIMEText
|
||||
except ImportError:
|
||||
from io import StringIO
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
from io import StringIO
|
||||
from email.message import EmailMessage
|
||||
from email.utils import parseaddr
|
||||
|
||||
|
||||
from email import encoders
|
||||
@@ -45,6 +36,7 @@ from cps.services import gmail
|
||||
from cps import logger, config
|
||||
|
||||
from cps import gdriveutils
|
||||
import uuid
|
||||
|
||||
log = logger.create()
|
||||
|
||||
@@ -130,20 +122,37 @@ class TaskEmail(CalibreTask):
|
||||
self.asyncSMTP = None
|
||||
self.results = dict()
|
||||
|
||||
# from calibre code:
|
||||
# https://github.com/kovidgoyal/calibre/blob/731ccd92a99868de3e2738f65949f19768d9104c/src/calibre/utils/smtp.py#L60
|
||||
def get_msgid_domain(self):
|
||||
try:
|
||||
# Parse out the address from the From line, and then the domain from that
|
||||
from_email = parseaddr(self.settings["mail_from"])[1]
|
||||
msgid_domain = from_email.partition('@')[2].strip()
|
||||
# This can sometimes sneak through parseaddr if the input is malformed
|
||||
msgid_domain = msgid_domain.rstrip('>').strip()
|
||||
except Exception:
|
||||
msgid_domain = ''
|
||||
return msgid_domain or 'calibre-web.com'
|
||||
|
||||
def prepare_message(self):
|
||||
message = MIMEMultipart()
|
||||
message['to'] = self.recipent
|
||||
message['from'] = self.settings["mail_from"]
|
||||
message['subject'] = self.subject
|
||||
message['Message-Id'] = make_msgid('calibre-web')
|
||||
message = EmailMessage()
|
||||
# message = MIMEMultipart()
|
||||
message['From'] = self.settings["mail_from"]
|
||||
message['To'] = self.recipent
|
||||
message['Subject'] = self.subject
|
||||
message['Date'] = formatdate(localtime=True)
|
||||
text = self.text
|
||||
msg = MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8')
|
||||
message.attach(msg)
|
||||
message['Message-Id'] = "{}@{}".format(uuid.uuid4(), self.get_msgid_domain()) # f"<{uuid.uuid4()}@{get_msgid_domain(from_)}>" # make_msgid('calibre-web')
|
||||
message.set_content(self.text.encode('UTF-8'), "text", "plain")
|
||||
if self.attachment:
|
||||
result = self._get_attachment(self.filepath, self.attachment)
|
||||
if result:
|
||||
message.attach(result)
|
||||
data = self._get_attachment(self.filepath, self.attachment)
|
||||
if data:
|
||||
# Set mimetype
|
||||
content_type, encoding = mimetypes.guess_type(self.attachment)
|
||||
if content_type is None or encoding is not None:
|
||||
content_type = 'application/octet-stream'
|
||||
main_type, sub_type = content_type.split('/', 1)
|
||||
message.add_attachment(data, maintype=main_type, subtype=sub_type, filename=self.attachment)
|
||||
else:
|
||||
self._handleError(u"Attachment not found")
|
||||
return
|
||||
@@ -158,10 +167,10 @@ class TaskEmail(CalibreTask):
|
||||
else:
|
||||
self.send_gmail_email(msg)
|
||||
except MemoryError as e:
|
||||
log.debug_or_exception(e, stacklevel=3)
|
||||
log.error_or_exception(e, stacklevel=3)
|
||||
self._handleError(u'MemoryError sending e-mail: {}'.format(str(e)))
|
||||
except (smtplib.SMTPException, smtplib.SMTPAuthenticationError) as e:
|
||||
log.debug_or_exception(e, stacklevel=3)
|
||||
log.error_or_exception(e, stacklevel=3)
|
||||
if hasattr(e, "smtp_error"):
|
||||
text = e.smtp_error.decode('utf-8').replace("\n", '. ')
|
||||
elif hasattr(e, "message"):
|
||||
@@ -172,10 +181,10 @@ class TaskEmail(CalibreTask):
|
||||
text = ''
|
||||
self._handleError(u'Smtplib Error sending e-mail: {}'.format(text))
|
||||
except (socket.error) as e:
|
||||
log.debug_or_exception(e, stacklevel=3)
|
||||
log.error_or_exception(e, stacklevel=3)
|
||||
self._handleError(u'Socket Error sending e-mail: {}'.format(e.strerror))
|
||||
except Exception as ex:
|
||||
log.debug_or_exception(ex, stacklevel=3)
|
||||
log.error_or_exception(ex, stacklevel=3)
|
||||
self._handleError(u'Error sending e-mail: {}'.format(ex))
|
||||
|
||||
def send_standard_email(self, msg):
|
||||
@@ -226,15 +235,15 @@ class TaskEmail(CalibreTask):
|
||||
self._progress = x
|
||||
|
||||
@classmethod
|
||||
def _get_attachment(cls, bookpath, filename):
|
||||
def _get_attachment(cls, book_path, filename):
|
||||
"""Get file as MIMEBase message"""
|
||||
calibre_path = config.config_calibre_dir
|
||||
if config.config_use_google_drive:
|
||||
df = gdriveutils.getFileFromEbooksFolder(bookpath, filename)
|
||||
df = gdriveutils.getFileFromEbooksFolder(book_path, filename)
|
||||
if df:
|
||||
datafile = os.path.join(calibre_path, bookpath, filename)
|
||||
if not os.path.exists(os.path.join(calibre_path, bookpath)):
|
||||
os.makedirs(os.path.join(calibre_path, bookpath))
|
||||
datafile = os.path.join(calibre_path, book_path, filename)
|
||||
if not os.path.exists(os.path.join(calibre_path, book_path)):
|
||||
os.makedirs(os.path.join(calibre_path, book_path))
|
||||
df.GetContentFile(datafile)
|
||||
else:
|
||||
return None
|
||||
@@ -244,23 +253,14 @@ class TaskEmail(CalibreTask):
|
||||
os.remove(datafile)
|
||||
else:
|
||||
try:
|
||||
file_ = open(os.path.join(calibre_path, bookpath, filename), 'rb')
|
||||
file_ = open(os.path.join(calibre_path, book_path, filename), 'rb')
|
||||
data = file_.read()
|
||||
file_.close()
|
||||
except IOError as e:
|
||||
log.debug_or_exception(e, stacklevel=3)
|
||||
log.error_or_exception(e, stacklevel=3)
|
||||
log.error(u'The requested file could not be read. Maybe wrong permissions?')
|
||||
return None
|
||||
# Set mimetype
|
||||
content_type, encoding = mimetypes.guess_type(filename)
|
||||
if content_type is None or encoding is not None:
|
||||
content_type = 'application/octet-stream'
|
||||
main_type, sub_type = content_type.split('/', 1)
|
||||
attachment = MIMEBase(main_type, sub_type)
|
||||
attachment.set_payload(data)
|
||||
encoders.encode_base64(attachment)
|
||||
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
|
||||
return attachment
|
||||
return data
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
||||
Reference in New Issue
Block a user