def sendmail(self, data, file_type='html'): """send html to kindle""" mail_host = self.get_config('mail', 'host') mail_port = self.get_config('mail', 'port') mail_ssl = self.get_config('mail', 'ssl') mail_from = self.get_config('mail', 'from') mail_to = self.get_config('mail', 'to') mail_username = self.get_config('mail', 'username') mail_password = self.get_config('mail', 'password') if not mail_from: raise Exception("'mail from' is empty") if not mail_to: raise Exception("'mail to' is empty") if not mail_host: raise Exception("'mail host' is empty") if not mail_port: mail_port = 25 print "send mail to %s ... " % mail_to, msg = MIMEMultipart() msg['from'] = mail_from msg['to'] = mail_to msg['subject'] = 'Convert' htmlText = 'google reader delivery.' msg.preamble = htmlText msgText = MIMEText(htmlText, 'html', 'utf-8') msg.attach(msgText) att = MIMEText(data, 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="google-reader-%s.%s"' % ( time.strftime('%H-%M-%S'), file_type) msg.attach(att) try: if mail_ssl in ['1', 1]: mail = smtplib.SMTP_SSL(timeout=60) else: mail = smtplib.SMTP(timeout=60) mail.connect(mail_host, int(mail_port)) mail.ehlo() if mail_username and mail_password: mail.login(mail_username, mail_password) mail.sendmail(msg['from'], msg['to'], msg.as_string()) mail.close() print "done." except Exception, e: print "fail:", e
def sendmail(self, data): """send html to kindle""" if not krconfig.mail_from: raise Exception("'mail from' is empty") if not krconfig.mail_to: raise Exception("'mail to' is empty") if not krconfig.mail_host: raise Exception("'mail host' is empty") logging.info("send mail to {} ... ".format(krconfig.mail_to)) msg = MIMEMultipart() msg['from'] = krconfig.mail_from msg['to'] = krconfig.mail_to msg['subject'] = 'Convert' htmlText = 'kindle reader delivery.' msg.preamble = htmlText msgText = MIMEText(htmlText, 'html', 'utf-8') msg.attach(msgText) att = MIMEText(data, 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="kindle-reader-%s.mobi"' % ( datetime.utcnow() + krconfig.timezone).strftime('%Y%m%d-%H%M%S') msg.attach(att) try: if krconfig.mail_ssl == 1: mail = smtplib.SMTP_SSL(timeout=60) else: mail = smtplib.SMTP(timeout=60) mail.connect(krconfig.mail_host, krconfig.mail_port) mail.ehlo() if krconfig.mail_username and krconfig.mail_password: mail.login(krconfig.mail_username, krconfig.mail_password) mail.sendmail(msg['from'], msg['to'], msg.as_string()) mail.close() except Exception, e: logging.error("fail:%s" % e)