def start_smtp(self, email_username, email_password):
self.server = smtplib.SMTP("smtp.gmail.com", 587)
self.server.ehlo()
self.server.starttls()
try:
self.server.login(email_username, email_password)
print 'LOGIN IS DONE YEYYY, YOU CAN NOW SEND SHIT EMAILS. xD'
return True
except smtplib.SMTPHeloError:
print 'The server responded weird stuff to my login request, please try again'
return False
except smtplib.SMTPAuthenticationError:
print 'Your account name or password is incorrect, please try again using the correct stuff'
return False
except smtplib.SMTPException:
print 'SHIT IS ALL FUCKED THERES NO HOPE THE WORLD IS GOING TO END, HIDEE '
return False
python类SMTPHeloError()的实例源码
def notify(subject, whom, what):
"""Send email notification.
Try to notify the addressee (``whom``) by e-mail, with Subject:
defined by ``subject`` and message body by ``what``.
"""
msg = email.message_from_string(what)
msg.add_header("From", "Certbot renewal agent <root>")
msg.add_header("To", whom)
msg.add_header("Subject", subject)
msg = msg.as_string()
try:
lmtp = smtplib.LMTP()
lmtp.connect()
lmtp.sendmail("root", [whom], msg)
except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused,
smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error):
# We should try using /usr/sbin/sendmail in this case
try:
proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"],
stdin=subprocess.PIPE)
proc.communicate(msg)
except OSError:
return False
return True
def _send_reset_email(self, user):
"""
Send password reset email to given email address
"""
username = user['uid'][0]
token = cherrypy.engine.publish('token-gen', username).pop()
base_url = urljoin(cherrypy.request.base, '/reset')
user['reset_url'] = urljoin(base_url, '?token={0}&username={1}'.format(token, username))
template = cherrypy.config.get('email', {}).get('template', 'email')
html = cherrypy.engine.publish('lookup-template', '{0}.html'.format(template)).pop()
txt = cherrypy.engine.publish('lookup-template', '{0}.txt'.format(template)).pop()
html_body = html.render(user=user)
txt_body = txt.render(user=user)
msg = MIMEMultipart('alternative')
msg['Subject'] = (' ').join([self.subject, 'Password Reset'])
msg['From'] = self.fromaddr
msg['To'] = user['mail'][0]
part1 = MIMEText(txt_body, 'plain')
part2 = MIMEText(html_body, 'html')
msg.attach(part1)
msg.attach(part2)
mailclient = smtplib.SMTP(self.server, self.port)
try:
if self.user and self.password:
mailclient.login(self.user, self.password)
mailclient.sendmail(msg['From'], msg['To'], msg.as_string())
except (smtplib.SMTPHeloError,
smtplib.SMTPAuthenticationError,
smtplib.SMTPException
) as e:
self.bus.log('Unable to send email. Error: {0}'.format(e.message['desc'] if 'desc' in e.message else e), 40) # pylint: disable=C0301
finally:
mailclient.quit()
def _send_username_email(self, user):
"""
Send username reminder email to given email address
"""
username = user['uid'][0]
base_url = urljoin(cherrypy.request.base, '/')
user['login_url'] = urljoin(base_url, '?username={0}'.format(username))
template = cherrypy.config.get('email', {}).get('template', 'email')
html = cherrypy.engine.publish('lookup-template', '{0}.html'.format(template)).pop()
txt = cherrypy.engine.publish('lookup-template', '{0}.txt'.format(template)).pop()
html_body = html.render(user=user)
txt_body = txt.render(user=user)
msg = MIMEMultipart('alternative')
msg['Subject'] = (' ').join([self.subject, 'Username Reminder'])
msg['From'] = self.fromaddr
msg['To'] = user['mail'][0]
part1 = MIMEText(txt_body, 'plain')
part2 = MIMEText(html_body, 'html')
msg.attach(part1)
msg.attach(part2)
mailclient = smtplib.SMTP(self.server, self.port)
try:
if self.user and self.password:
mailclient.login(self.user, self.password)
mailclient.sendmail(msg['From'], msg['To'], msg.as_string())
except (smtplib.SMTPHeloError,
smtplib.SMTPAuthenticationError,
smtplib.SMTPException
) as e:
self.bus.log('Unable to send email. Error: {0}'.format(e.message['desc'] if 'desc' in e.message else e), 40) # pylint: disable=C0301
finally:
mailclient.quit()
def notify(subject, whom, what):
"""Send email notification.
Try to notify the addressee (``whom``) by e-mail, with Subject:
defined by ``subject`` and message body by ``what``.
"""
msg = email.message_from_string(what)
msg.add_header("From", "Certbot renewal agent <root>")
msg.add_header("To", whom)
msg.add_header("Subject", subject)
msg = msg.as_string()
try:
lmtp = smtplib.LMTP()
lmtp.connect()
lmtp.sendmail("root", [whom], msg)
except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused,
smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error):
# We should try using /usr/sbin/sendmail in this case
try:
proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"],
stdin=subprocess.PIPE)
proc.communicate(msg)
except OSError:
return False
return True
def process_notification(notification):
from ..models.notification import (
NotificationDeliveryStateType, UnverifiedEmailException,
MissingEmailException)
import smtplib
import socket
from assembl.lib import config
assert notification
sys.stderr.write(
"process_notification called with notification %d, state was %s" % (
notification.id, notification.delivery_state))
if notification.delivery_state not in \
NotificationDeliveryStateType.getRetryableDeliveryStates():
sys.stderr.write(
"Refusing to process notification %d because its delivery state is: %s" % (
notification.id, notification.delivery_state))
return
try:
email = notification.render_to_message()
# sys.stderr.write(email_str)
recipient = notification.get_to_email_address()
wait_if_necessary(recipient)
notify_process_mailer.send_immediately(email, fail_silently=False)
notification.delivery_state = \
NotificationDeliveryStateType.DELIVERY_IN_PROGRESS
email_was_sent(recipient)
except UnverifiedEmailException as e:
sys.stderr.write("Not sending to unverified email: "+repr(e))
notification.delivery_state = \
NotificationDeliveryStateType.DELIVERY_TEMPORARY_FAILURE
except MissingEmailException as e:
notification.delivery_state = \
NotificationDeliveryStateType.DELIVERY_TEMPORARY_FAILURE
sys.stderr.write("Missing email! :"+repr(e))
except (smtplib.SMTPConnectError,
socket.timeout, socket.error,
smtplib.SMTPHeloError) as e:
sys.stderr.write("Temporary failure: "+repr(e))
notification.delivery_state = \
NotificationDeliveryStateType.DELIVERY_TEMPORARY_FAILURE
except smtplib.SMTPRecipientsRefused as e:
notification.delivery_state = \
NotificationDeliveryStateType.DELIVERY_FAILURE
sys.stderr.write("Recepients refused: "+repr(e))
except smtplib.SMTPSenderRefused as e:
notification.delivery_state = \
NotificationDeliveryStateType.DELIVERY_TEMPORARY_FAILURE
sys.stderr.write("Invalid configuration! :"+repr(e))
mark_changed()
sys.stderr.write(
"process_notification finished processing %d, state is now %s"
% (notification.id, notification.delivery_state))