|
| 1 | +import asyncio |
| 2 | +import tornado.ioloop, tornado.web |
| 3 | +import smtplib |
| 4 | + |
| 5 | +# Function for sending mail |
| 6 | +def sendEmail(recipient_email, app_name): |
| 7 | + sender_email = "" |
| 8 | + app_password = "" |
| 9 | + # SMTP config |
| 10 | + smtp_server = "smtp.gmail.com" |
| 11 | + smtp_port = 587 |
| 12 | + |
| 13 | + subject = "Android App Created: %s" % app_name |
| 14 | + body = '''Your Android app %s has been successfully created.''' % app_name |
| 15 | + |
| 16 | + email_message = "Subject: %s\n\n%s" % (subject, body) |
| 17 | + |
| 18 | + try: |
| 19 | + server = smtplib.SMTP(smtp_server, smtp_port) |
| 20 | + server.starttls() |
| 21 | + server.login(sender_email, app_password) |
| 22 | + server.sendmail(sender_email, recipient_email, email_message) |
| 23 | + print("Email sent successfully") |
| 24 | + email_sent = True |
| 25 | + |
| 26 | + except smtplib.SMTPAuthenticationError as e: |
| 27 | + print("SMTP Authentication Error: %s" % e) |
| 28 | + except Exception as e: |
| 29 | + print("Error: %s", e) |
| 30 | + |
| 31 | + # raise exception for outer try except |
| 32 | + if not email_sent: |
| 33 | + raise Exception("Email sending failed.") |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +# Async testing function |
| 38 | +async def asyncFunc(i): |
| 39 | + await asyncio.sleep(1) |
| 40 | + print("I'm Running %s" % i) |
| 41 | + |
| 42 | + |
| 43 | +# main function |
| 44 | +async def main(): |
| 45 | + print("Code started") |
| 46 | + |
| 47 | + # Code for sending mail |
| 48 | + try: |
| 49 | + recipient_email = "[email protected]" |
| 50 | + app_name = "myApp" |
| 51 | + tornado.ioloop.IOLoop.current().run_in_executor(None, sendEmail, recipient_email, app_name) |
| 52 | + # You can directly call the function for async |
| 53 | + # sendEmail(recipient_email, app_name) |
| 54 | + except Exception as e: |
| 55 | + print(e) |
| 56 | + |
| 57 | + for i in range(0, 10): |
| 58 | + await asyncFunc(i) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + loop = asyncio.get_event_loop() |
| 63 | + loop.run_until_complete(main()) |
0 commit comments