server.set_debuglevel(0) if CFG_MISCUTIL_SMTP_TLS: server.ehlo() server.starttls() server.ehlo() if CFG_MISCUTIL_SMTP_USER and CFG_MISCUTIL_SMTP_PASS: server.login(CFG_MISCUTIL_SMTP_USER, CFG_MISCUTIL_SMTP_PASS) server.sendmail(fromaddr, toaddr + bccaddr, body) server.quit() sent = True except (smtplib.SMTPException, socket.error): register_exception() if debug_level > 1: try: raise InvenioMiscUtilError(_('Error in connecting to the SMPT server waiting %s seconds. Exception is %s, while sending email from %s to %s with body %s.') % (attempt_sleeptime, sys.exc_info()[0], fromaddr, toaddr, body)) except InvenioMiscUtilError, exc: register_exception() # log('ERR_MISCUTIL_CONNECTION_SMTP', attempt_sleeptime, # sys.exc_info()[0], fromaddr, toaddr, body) if not sent: attempt_times -= 1 if attempt_times > 0: # sleep only if we shall retry again sleep(attempt_sleeptime) if not sent: try: raise InvenioMiscUtilError(_('Error in sending email from %s to %s with body %s.') % (fromaddr, toaddr, body)) except InvenioMiscUtilError, exc: register_exception() # log('ERR_MISCUTIL_SENDING_EMAIL', fromaddr, toaddr, body) return sent
def send_email(fromaddr, toaddr, subject="", content="", html_content='', html_images=None, header=None, footer=None, html_header=None, html_footer=None, copy_to_admin=0, attempt_times=1, attempt_sleeptime=10, debug_level=0, ln=CFG_SITE_LANG, charset=None, replytoaddr="", attachments=None, bccaddr="", ): """Send a forged email to TOADDR from FROMADDR with message created from subjet, content and possibly header and footer. @param fromaddr: [string] sender @param toaddr: [string or list-of-strings] list of receivers (if string, then receivers are separated by ','). BEWARE: If more than once receiptiant is given, the receivers are put in BCC and To will be "Undisclosed.Recipients:". @param subject: [string] subject of the email @param content: [string] content of the email @param html_content: [string] html version of the email @param html_images: [dict] dictionary of image id, image path @param header: [string] header to add, None for the Default @param footer: [string] footer to add, None for the Default @param html_header: [string] header to add to the html part, None for the Default @param html_footer: [string] footer to add to the html part, None for the Default @param copy_to_admin: [int] if 1 add CFG_SITE_ADMIN_EMAIL in receivers @param attempt_times: [int] number of tries @param attempt_sleeptime: [int] seconds in between tries @param debug_level: [int] debug level @param ln: [string] invenio language @param charset: [string] the content charset. By default is None which means to try to encode the email as ascii, then latin1 then utf-8. @param replytoaddr: [string or list-of-strings] to be used for the reply-to header of the email (if string, then receivers are separated by ',') @param attachments: list of paths of files to be attached. Alternatively, every element of the list could be a tuple: (filename, mimetype) @param bccaddr: [string or list-of-strings] to be used for BCC header of the email (if string, then receivers are separated by ',') If sending fails, try to send it ATTEMPT_TIMES, and wait for ATTEMPT_SLEEPTIME seconds in between tries. e.g.: send_email('*****@*****.**', '*****@*****.**', 'Let\'s try!'', 'check 1234', '<strong>check</strong> <em>1234</em><img src="cid:image1">', {'image1': '/tmp/quantum.jpg'}) @return: [bool]: True if email was sent okay, False if it was not. """ if html_images is None: html_images = {} if type(toaddr) is str: toaddr = toaddr.strip().split(',') toaddr = remove_temporary_emails(toaddr) if type(bccaddr) is str: bccaddr = bccaddr.strip().split(',') usebcc = len(toaddr) > 1 # More than one address, let's use Bcc in place of To if copy_to_admin: if CFG_SITE_ADMIN_EMAIL not in toaddr: toaddr.append(CFG_SITE_ADMIN_EMAIL) if CFG_DEVEL_SITE: # if we are on a development site, we don't want to send external e-mails content = """ -------------------------------------------------------------- This message would have been sent to the following recipients: %s -------------------------------------------------------------- %s""" % (toaddr, content) toaddr = CFG_SITE_ADMIN_EMAIL usebcc = False body = forge_email(fromaddr, toaddr, subject, content, html_content, html_images, usebcc, header, footer, html_header, html_footer, ln, charset, replytoaddr, attachments, bccaddr) _ = gettext_set_language(CFG_SITE_LANG) if attempt_times < 1 or not toaddr: try: raise InvenioMiscUtilError(_('The system is not attempting to send an email from %s, to %s, with body %s.') % (fromaddr, toaddr, body)) except InvenioMiscUtilError, exc: register_exception() # log('ERR_MISCUTIL_NOT_ATTEMPTING_SEND_EMAIL', fromaddr, toaddr, body) return False
def send_email(fromaddr, toaddr, subject="", content="", html_content='', html_images=None, header=None, footer=None, html_header=None, html_footer=None, copy_to_admin=0, attempt_times=1, attempt_sleeptime=10, debug_level=0, ln=CFG_SITE_LANG, charset=None, replytoaddr="", attachments=None): """Send a forged email to TOADDR from FROMADDR with message created from subjet, content and possibly header and footer. @param fromaddr: [string] sender @param toaddr: [string or list-of-strings] list of receivers (if string, then receivers are separated by ',') @param subject: [string] subject of the email @param content: [string] content of the email @param html_content: [string] html version of the email @param html_images: [dict] dictionary of image id, image path @param header: [string] header to add, None for the Default @param footer: [string] footer to add, None for the Default @param html_header: [string] header to add to the html part, None for the Default @param html_footer: [string] footer to add to the html part, None for the Default @param copy_to_admin: [int] if 1 add CFG_SITE_ADMIN_EMAIL in receivers @param attempt_times: [int] number of tries @param attempt_sleeptime: [int] seconds in between tries @param debug_level: [int] debug level @param ln: [string] invenio language @param charset: [string] the content charset. By default is None which means to try to encode the email as ascii, then latin1 then utf-8. @param replytoaddr: [string or list-of-strings] to be used for the reply-to header of the email (if string, then receivers are separated by ',') @param attachments: list of paths of files to be attached. Alternatively, every element of the list could be a tuple: (filename, mimetype) If sending fails, try to send it ATTEMPT_TIMES, and wait for ATTEMPT_SLEEPTIME seconds in between tries. e.g.: send_email('*****@*****.**', '*****@*****.**', 'Let\'s try!'', 'check 1234', '<strong>check</strong> <em>1234</em><img src="cid:image1">', {'image1': '/tmp/quantum.jpg'}) @return: [bool]: True if email was sent okay, False if it was not. """ if html_images is None: html_images = {} if type(toaddr) is str: toaddr = toaddr.strip().split(',') toaddr = remove_temporary_emails(toaddr) usebcc = len(toaddr.split( ',')) > 1 # More than one address, let's use Bcc in place of To if copy_to_admin: if CFG_SITE_ADMIN_EMAIL not in toaddr: toaddr.append(CFG_SITE_ADMIN_EMAIL) body = forge_email(fromaddr, toaddr, subject, content, html_content, html_images, usebcc, header, footer, html_header, html_footer, ln, charset, replytoaddr, attachments) if attempt_times < 1 or not toaddr: try: raise InvenioMiscUtilError( g. _('The system is not attempting to send an email from %s, to %s, with body %s.' ) % (fromaddr, toaddr, body)) except InvenioMiscUtilError: register_exception() return False sent = False while not sent and attempt_times > 0: sent = body.send() try: sent = body.send() except Exception: register_exception() if debug_level > 1: try: raise InvenioMiscUtilError( g. _('Error in sending message. Waiting %s seconds. Exception is %s, while sending email from %s to %s with body %s.' ) % (attempt_sleeptime, sys.exc_info()[0], fromaddr, toaddr, body)) except InvenioMiscUtilError: register_exception() if not sent: attempt_times -= 1 if attempt_times > 0: # sleep only if we shall retry again sleep(attempt_sleeptime) if not sent: try: raise InvenioMiscUtilError( g._('Error in sending email from %s to %s with body %s.') % (fromaddr, toaddr, body)) except InvenioMiscUtilError: register_exception() return sent