def send_msg_list(msgs, sender=None): if len(msgs): connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT), local_hostname=DNS_NAME.get_fqdn()) try: if (bool(settings.EMAIL_USE_TLS)): connection.ehlo() connection.starttls() connection.ehlo() if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD)) if sender is None: sender = str(settings.DEFAULT_FROM_EMAIL) for email, msg in msgs: try: connection.sendmail(sender, [email], msg) except Exception, e: pass try: connection.quit() except socket.sslerror: connection.close() except Exception, e: pass
def create_and_send_mail_messages(messages): if not settings.EMAIL_HOST: return sender = Header(unicode(settings.APP_SHORT_NAME), 'utf-8') sender.append('<%s>' % unicode(settings.DEFAULT_FROM_EMAIL)) sender = u'%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL)) try: connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT), local_hostname=DNS_NAME.get_fqdn()) if (bool(settings.EMAIL_USE_TLS)): connection.ehlo() connection.starttls() connection.ehlo() if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD)) if sender is None: sender = str(settings.DEFAULT_FROM_EMAIL) for recipient, subject, html, text, media in messages: msgRoot = MIMEMultipart('related') msgRoot['Subject'] = Header(subject, 'utf-8') msgRoot['From'] = sender to = Header(recipient.username, 'utf-8') to.append('<%s>' % recipient.email) msgRoot['To'] = to msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8') msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgAlternative.attach(MIMEText(text.encode('utf-8'), _charset='utf-8')) msgAlternative.attach(MIMEText(html.encode('utf-8'), 'html', _charset='utf-8')) for alias, location in media.items(): fp = open(location, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<'+alias+'>') msgRoot.attach(msgImage) try: connection.sendmail(sender, [recipient.email], msgRoot.as_string()) logging.error("EMAIL_LOG: Email sent to %s" % recipient.email) except Exception, e: logging.error("Couldn't send mail using the sendmail method: %s" % e) try: connection.quit() except socket.sslerror: connection.close()
def create_connection(): connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT), local_hostname=DNS_NAME.get_fqdn()) if bool(settings.EMAIL_USE_TLS): connection.ehlo() connection.starttls() connection.ehlo() if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD)) return connection
def create_connection(): connection = SMTP(str(EMAIL_HOST), str(EMAIL_PORT), local_hostname=DNS_NAME.get_fqdn()) if bool(EMAIL_USE_TLS): connection.ehlo() connection.starttls() connection.ehlo() if EMAIL_HOST_USER and EMAIL_HOST_PASSWORD: connection.login(str(EMAIL_HOST_USER), str(EMAIL_HOST_PASSWORD)) return connection
def open(self): """ Ensures we have a connection to the email server. Returns whether or not a new connection was required (True or False). """ if self.connection: # Nothing to do if the connection is already open. return False try: # If local_hostname is not specified, socket.getfqdn() gets used. # For performance, we use the cached FQDN for local_hostname. self.connection = smtplib.SMTP(self.host, self.port, local_hostname=DNS_NAME.get_fqdn()) if self.use_tls: self.connection.ehlo() self.connection.starttls() self.connection.ehlo() if self.username and self.password: self.connection.login(self.username, self.password) return True except: if not self.fail_silently: raise
def _send_mail_by_smtp(messages): if not settings.EMAIL_HOST: return if len(messages) == 0: return is_invitate=False sender = Header(unicode(settings.APP_SHORT_NAME), 'utf-8') sender.append('<%s>' % unicode(settings.DEFAULT_FROM_EMAIL)) sender = u'%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL)) try: index = 0 connection = None for recipient, subject, html, text, media in messages: # we will reconnect the mail server for ever 100 mails if index % 90 == 0 : print "reconnect" connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT), local_hostname=DNS_NAME.get_fqdn(),timeout=50) if (bool(settings.EMAIL_USE_TLS)): connection.ehlo() connection.starttls() connection.ehlo() if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: try: if messages[0][0].type == "invitation": is_invitate=True connection.login("*****@*****.**", str(settings.EMAIL_HOST_PASSWORD)) except AttributeError: connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD)) if sender is None: sender = str(settings.DEFAULT_FROM_EMAIL) msgRoot = MIMEMultipart('related') msgRoot['Subject'] = Header(subject, 'utf-8') msgRoot['From'] = sender to = Header(recipient.username, 'utf-8') to.append('<%s>' % recipient.email) print ">>>",recipient.email," ",index index += 1 msgRoot['To'] = to msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8') msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgAlternative.attach(MIMEText(text.encode('utf-8'), _charset='utf-8')) msgAlternative.attach(MIMEText(html.encode('utf-8'), 'html', _charset='utf-8')) for alias, location in media.items(): fp = open(location, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<'+alias+'>') msgRoot.attach(msgImage) try: connection.sendmail(sender, [recipient.email], msgRoot.as_string()) #send_mail("hello",msgRoot.as_string(),sender,[recipient.email],auth_user=str(settings.EMAIL_HOST_USER),auth_password=str(settings.EMAIL_HOST_PASSWORD)) except Exception, e: logging.error("Couldn't send mail using the sendmail method: %s" % e) if e.smtp_error.startswith("5.4.5 Daily sending quota exceeded") and is_invitate: exit(index-1) try: connection.quit() except socket.sslerror: connection.close()
def create_and_send_mail_messages(messages): if not settings.EMAIL_HOST: return sender = Header(unicode(settings.APP_SHORT_NAME), 'utf-8') sender.append('<%s>' % unicode(settings.DEFAULT_FROM_EMAIL)) ## fix sender error ##sender = u'%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL)) try: connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT), local_hostname=DNS_NAME.get_fqdn()) if (bool(settings.EMAIL_USE_TLS)): connection.ehlo() connection.starttls() connection.ehlo() if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD)) if sender is None: sender = str(settings.DEFAULT_FROM_EMAIL) for recipient, subject, html, text, media in messages: msgRoot = MIMEMultipart('related') msgRoot['Subject'] = Header(subject, 'utf-8') msgRoot['From'] = sender to = Header(recipient.username, 'utf-8') to.append('<%s>' % recipient.email) msgRoot['To'] = to msgRoot.preamble = 'This is a multi-part message from %s.' % unicode( settings.APP_SHORT_NAME).encode('utf8') msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgAlternative.attach( MIMEText(text.encode('utf-8'), _charset='utf-8')) msgAlternative.attach( MIMEText(html.encode('utf-8'), 'html', _charset='utf-8')) for alias, location in media.items(): fp = open(location, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<' + alias + '>') msgRoot.attach(msgImage) try: connection.sendmail(sender, [recipient.email], msgRoot.as_string()) except Exception, e: pass try: connection.quit() except socket.sslerror: connection.close()