def make_mobi(book_id, calibrepath, user_id, kindle_mail): book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id).filter( db.Data.format == 'EPUB').first() if not data: error_message = _(u"epub format not found for book id: %(book)d", book=book_id) app.logger.error("make_mobi: " + error_message) return error_message if ub.config.config_use_google_drive: df = gd.getFileFromEbooksFolder(book.path, data.name + u".epub") if df: datafile = os.path.join(calibrepath, book.path, data.name + u".epub") if not os.path.exists(os.path.join(calibrepath, book.path)): os.makedirs(os.path.join(calibrepath, book.path)) df.GetContentFile(datafile) else: error_message = (u"make_mobi: epub not found on gdrive: %s.epub" % data.name) return error_message file_path = os.path.join(calibrepath, book.path, data.name) if os.path.exists(file_path + u".epub"): # append converter to queue global_WorkerThread.add_convert(file_path, book.id, user_id, _(u"Convert: %s" % book.title), ub.get_mail_settings(), kindle_mail) return None else: error_message = (u"make_mobi: epub not found: %s.epub" % file_path) return error_message
def send_mail(book_id, kindle_mail, calibrepath, user_id): """Send email with attachments""" book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id).all() formats = {} for entry in data: if entry.format == "MOBI": formats["mobi"] = entry.name + ".mobi" if entry.format == "EPUB": formats["epub"] = entry.name + ".epub" if entry.format == "PDF": formats["pdf"] = entry.name + ".pdf" if len(formats) == 0: return _(u"Could not find any formats suitable for sending by e-mail") if 'mobi' in formats: result = formats['mobi'] elif 'epub' in formats: # returns None if sucess, otherwise errormessage return convert_book_format(book_id, calibrepath, u'epub', u'mobi', user_id, kindle_mail) elif 'pdf' in formats: result = formats['pdf'] # worker.get_attachment() else: return _(u"Could not find any formats suitable for sending by e-mail") if result: global_WorkerThread.add_email(_(u"Send to Kindle"), book.path, result, ub.get_mail_settings(), kindle_mail, user_id, _(u"E-mail: %(book)s", book=book.title)) else: return _( u"The requested file could not be read. Maybe wrong permissions?")
def convert_book_format(book_id, calibrepath, old_book_format, new_book_format, user_id, kindle_mail=None): book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == old_book_format).first() if not data: error_message = _(u"%(format)s format not found for book id: %(book)d", format=old_book_format, book=book_id) app.logger.error("convert_book_format: " + error_message) return error_message if ub.config.config_use_google_drive: df = gd.getFileFromEbooksFolder(book.path, data.name + "." + old_book_format.lower()) if df: datafile = os.path.join(calibrepath, book.path, data.name + u"." + old_book_format.lower()) if not os.path.exists(os.path.join(calibrepath, book.path)): os.makedirs(os.path.join(calibrepath, book.path)) df.GetContentFile(datafile) else: error_message = _(u"%(format)s not found on Google Drive: %(fn)s", format=old_book_format, fn=data.name + "." + old_book_format.lower()) return error_message file_path = os.path.join(calibrepath, book.path, data.name) if os.path.exists(file_path + "." + old_book_format.lower()): # read settings and append converter task to queue if kindle_mail: settings = ub.get_mail_settings() text = _(u"Convert: %(book)s" , book=book.title) else: settings = dict() text = _(u"Convert to %(format)s: %(book)s", format=new_book_format, book=book.title) settings['old_book_format'] = old_book_format settings['new_book_format'] = new_book_format global_WorkerThread.add_convert(file_path, book.id, user_id, text, settings, kindle_mail) return None else: error_message = _(u"%(format)s not found: %(fn)s", format=old_book_format, fn=data.name + "." + old_book_format.lower()) return error_message
def send_test_mail(kindle_mail, user_name): msg = MIMEMultipart() msg['Subject'] = _(u'Calibre-web test email') text = _(u'This email has been sent via calibre web.') msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8')) global_eMailThread.add_email(msg, ub.get_mail_settings(), kindle_mail, user_name, _('Test E-Mail')) return # send_raw_email(kindle_mail, msg)
def send_mail(book_id, kindle_mail, calibrepath, user_id): """Send email with attachments""" # create MIME message result = None msg = MIMEMultipart() msg['Subject'] = _(u'Send to Kindle') msg['Message-Id'] = make_msgid('calibre-web') msg['Date'] = formatdate(localtime=True) text = _(u'This email has been sent via calibre web.') msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8')) book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id).all() formats = {} index = 0 for indx, entry in enumerate(data): if entry.format == "MOBI": formats["mobi"] = entry.name + ".mobi" if entry.format == "EPUB": formats["epub"] = entry.name + ".epub" index = indx if entry.format == "PDF": formats["pdf"] = entry.name + ".pdf" if len(formats) == 0: return _("Could not find any formats suitable for sending by email") if 'mobi' in formats: result = get_attachment(calibrepath, book.path, formats['mobi']) if result: msg.attach(result) elif 'epub' in formats: # returns filename if sucess, otherwise errormessage data, resultCode = make_mobi(book.id, calibrepath) if resultCode == RET_SUCCESS: result = get_attachment(calibrepath, book.path, os.path.basename(data)) if result: msg.attach(result) else: app.logger.error(data) return data elif 'pdf' in formats: result = get_attachment(calibrepath, book.path, formats['pdf']) if result: msg.attach(result) else: return _("Could not find any formats suitable for sending by email") if result: global_eMailThread.add_email(msg, ub.get_mail_settings(), kindle_mail, user_id, _(u"E-Mail: %s" % book.title)) return None # send_raw_email(kindle_mail, msg) else: return _( 'The requested file could not be read. Maybe wrong permissions?')
def send_registration_mail(e_mail, user_name, default_password, resend=False): text = "Hello %s!\r\n" % user_name if not resend: text += "Your new account at Calibre-Web has been created. Thanks for joining us!\r\n" text += "Please log in to your account using the following informations:\r\n" text += "User name: %s\n" % user_name text += "Password: %s\r\n" % default_password text += "Don't forget to change your password after first login.\r\n" text += "Sincerely\r\n\r\n" text += "Your Calibre-Web team" global_WorkerThread.add_email(_(u'Get Started with Calibre-Web'),None, None, ub.get_mail_settings(), e_mail, user_name, _(u"Registration e-mail for user: %(name)s", name=user_name), text) return
def send_raw_email(kindle_mail, msg): settings = ub.get_mail_settings() msg['From'] = settings["mail_from"] msg['To'] = kindle_mail use_ssl = int(settings.get('mail_use_ssl', 0)) # convert MIME message to string fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) msg = fp.getvalue() # send email try: timeout = 600 # set timeout to 5mins org_stderr = sys.stderr sys.stderr = StderrLogger() if use_ssl == 2: mailserver = smtplib.SMTP_SSL(settings["mail_server"], settings["mail_port"], timeout) else: mailserver = smtplib.SMTP(settings["mail_server"], settings["mail_port"], timeout) mailserver.set_debuglevel(1) if use_ssl == 1: mailserver.starttls() if settings["mail_password"]: mailserver.login(str(settings["mail_login"]), str(settings["mail_password"])) mailserver.sendmail(settings["mail_from"], kindle_mail, msg) mailserver.quit() smtplib.stderr = org_stderr except (socket.error, smtplib.SMTPRecipientsRefused, smtplib.SMTPException) as ex: app.logger.error(traceback.print_exc()) return _("Failed to send mail: %s" % str(ex)) return None
def send_mail(book_id, kindle_mail, calibrepath, user_id): """Send email with attachments""" # create MIME message msg = MIMEMultipart() msg['Subject'] = _(u'Send to Kindle') msg['Message-Id'] = make_msgid('calibre-web') msg['Date'] = formatdate(localtime=True) text = _(u'This email has been sent via calibre web.') msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8')) book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id) formats = {} for entry in data: if entry.format == "MOBI": formats["mobi"] = os.path.join(calibrepath, book.path, entry.name + ".mobi") if entry.format == "EPUB": formats["epub"] = os.path.join(calibrepath, book.path, entry.name + ".epub") if entry.format == "PDF": formats["pdf"] = os.path.join(calibrepath, book.path, entry.name + ".pdf") if len(formats) == 0: return _("Could not find any formats suitable for sending by email") if 'mobi' in formats: msg.attach(get_attachment(formats['mobi'])) elif 'epub' in formats: data, resultCode = make_mobi(book.id, calibrepath) if resultCode == RET_SUCCESS: msg.attach(get_attachment(data)) else: app.logger.error = data return data # _("Could not convert epub to mobi") elif 'pdf' in formats: msg.attach(get_attachment(formats['pdf'])) else: return _("Could not find any formats suitable for sending by email") global_eMailThread.add_email(msg, ub.get_mail_settings(), kindle_mail, user_id) return None # send_raw_email(kindle_mail, msg)
def send_test_mail(kindle_mail): settings = ub.get_mail_settings() msg = MIMEMultipart() msg['From'] = settings["mail_from"] msg['To'] = kindle_mail msg['Subject'] = _('Calibre-web test email') text = _('This email has been sent via calibre web.') use_ssl = settings.get('mail_use_ssl', 0) # convert MIME message to string fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) msg = fp.getvalue() # send email try: timeout = 600 # set timeout to 5mins org_stderr = smtplib.stderr smtplib.stderr = StderrLogger() mailserver = smtplib.SMTP(settings["mail_server"], settings["mail_port"], timeout) mailserver.set_debuglevel(1) if int(use_ssl) == 1: mailserver.ehlo() mailserver.starttls() mailserver.ehlo() if settings["mail_password"]: mailserver.login(settings["mail_login"], settings["mail_password"]) mailserver.sendmail(settings["mail_login"], kindle_mail, msg) mailserver.quit() smtplib.stderr = org_stderr except (socket.error, smtplib.SMTPRecipientsRefused, smtplib.SMTPException), e: app.logger.error(traceback.print_exc()) return _("Failed to send mail: %s" % str(e))
def send_mail(book_id, book_format, convert, kindle_mail, calibrepath, user_id): """Send email with attachments""" book = db.session.query(db.Books).filter(db.Books.id == book_id).first() if convert: # returns None if success, otherwise errormessage return convert_book_format(book_id, calibrepath, u'epub', book_format.lower(), user_id, kindle_mail) else: for entry in iter(book.data): if entry.format.upper() == book_format.upper(): result = entry.name + '.' + book_format.lower() global_WorkerThread.add_email( _(u"Send to Kindle"), book.path, result, ub.get_mail_settings(), kindle_mail, user_id, _(u"E-mail: %(book)s", book=book.title), _(u'This e-mail has been sent via Calibre-Web.')) return return _( u"The requested file could not be read. Maybe wrong permissions?")
def send_test_mail(kindle_mail, user_name): global_WorkerThread.add_email(_(u'Calibre-Web test e-mail'),None, None, ub.get_mail_settings(), kindle_mail, user_name, _(u"Test e-mail"), _(u'This e-mail has been sent via Calibre-Web.')) return
def send_test_mail(kindle_mail, user_name): global_WorkerThread.add_email(_(u'Calibre-web test email'), None, None, ub.get_mail_settings(), kindle_mail, user_name, _(u"Test E-Mail")) return
def send_mail(book_id, kindle_mail): '''Send email with attachments''' is_mobi = False is_azw = False is_azw3 = False is_epub = False is_pdf = False file_path = None settings = ub.get_mail_settings() # create MIME message msg = MIMEMultipart() msg['From'] = settings["mail_from"] msg['To'] = kindle_mail msg['Subject'] = 'Send to Kindle' text = 'This email has been sent via calibre web.' msg.attach(MIMEText(text)) use_ssl = settings.get('mail_use_ssl', 0) # attach files #msg.attach(self.get_attachment(file_path)) book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id) formats = {} for entry in data: if entry.format == "MOBI": formats["mobi"] = os.path.join(config.DB_ROOT, book.path, entry.name + ".mobi") if entry.format == "EPUB": formats["epub"] = os.path.join(config.DB_ROOT, book.path, entry.name + ".epub") if entry.format == "PDF": formats["pdf"] = os.path.join(config.DB_ROOT, book.path, entry.name + ".pdf") if len(formats) == 0: return "Could not find any formats suitable for sending by email" if 'mobi' in formats: msg.attach(get_attachment(formats['mobi'])) elif 'epub' in formats: filepath = make_mobi(book.id) if filepath is not None: msg.attach(get_attachment(filepath)) elif filepath is None: return "Could not convert epub to mobi" elif 'pdf' in formats: msg.attach(get_attachment(formats['pdf'])) elif 'pdf' in formats: msg.attach(get_attachment(formats['pdf'])) else: return "Could not find any formats suitable for sending by email" # convert MIME message to string fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) msg = fp.getvalue() # send email try: mailserver = smtplib.SMTP(settings["mail_server"], settings["mail_port"]) mailserver.set_debuglevel(0) if int(use_ssl) == 1: mailserver.ehlo() mailserver.starttls() mailserver.ehlo() if settings["mail_password"]: mailserver.login(settings["mail_login"], settings["mail_password"]) mailserver.sendmail(settings["mail_login"], kindle_mail, msg) mailserver.quit() except (socket.error, smtplib.SMTPRecipientsRefused, smtplib.SMTPException), e: app.logger.error(traceback.print_exc()) return "Failed to send mail: %s" % str(e)
def send_mail(book_id, kindle_mail): '''Send email with attachments''' is_mobi = False is_azw = False is_azw3 = False is_epub = False is_pdf = False file_path = None settings = ub.get_mail_settings() # create MIME message msg = MIMEMultipart() msg['From'] = settings["mail_from"] msg['To'] = kindle_mail msg['Subject'] = 'Send to Kindle' text = 'This email has been sent via calibre web.' msg.attach(MIMEText(text)) use_ssl = settings.get('mail_use_ssl', 0) # attach files #msg.attach(self.get_attachment(file_path)) book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id) formats = {} for entry in data: if entry.format == "MOBI": formats["mobi"] = os.path.join(config.DB_ROOT, book.path, entry.name + ".mobi") if entry.format == "EPUB": formats["epub"] = os.path.join(config.DB_ROOT, book.path, entry.name + ".epub") if entry.format == "PDF": formats["pdf"] = os.path.join(config.DB_ROOT, book.path, entry.name + ".pdf") if len(formats) == 0: return "Could not find any formats suitable for sending by email" if 'mobi' in formats: msg.attach(get_attachment(formats['mobi'])) elif 'epub' in formats: filepath = make_mobi(book.id) if filepath is not None: msg.attach(get_attachment(filepath)) elif 'pdf' in formats: msg.attach(get_attachment(formats['pdf'])) elif 'pdf' in formats: msg.attach(get_attachment(formats['pdf'])) else: return "Could not find any formats suitable for sending by email" # convert MIME message to string fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) msg = fp.getvalue() # send email try: mailserver = smtplib.SMTP(settings["mail_server"],settings["mail_port"]) mailserver.set_debuglevel(0) if int(use_ssl) == 1: mailserver.ehlo() mailserver.starttls() mailserver.ehlo() if settings["mail_password"]: mailserver.login(settings["mail_login"], settings["mail_password"]) mailserver.sendmail(settings["mail_login"], kindle_mail, msg) mailserver.quit() except (socket.error, smtplib.SMTPRecipientsRefused, smtplib.SMTPException), e: app.logger.error(traceback.print_exc()) return "Failed to send mail: %s" % str(e)