Skip to content

Instantly share code, notes, and snippets.

@SuperZombi
Last active December 31, 2024 17:52
Show Gist options
  • Save SuperZombi/97b0c9453d44701b06c39c5dd77d533a to your computer and use it in GitHub Desktop.
Save SuperZombi/97b0c9453d44701b06c39c5dd77d533a to your computer and use it in GitHub Desktop.
Sending emails
# https://myaccount.google.com/u/0/lesssecureapps
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender_email = '[email protected]'
sender_password = '123456789'
recipients = [
'[email protected]',
'[email protected]',
'[email protected]',
]
subject = 'New letter'
html_content = """
<html>
<body>
<h3>Hello world!</h3>
</body>
</html>
"""
def build_msg(recipient_email):
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(html_content, 'html'))
return msg
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
for index, recipient in enumerate(recipients):
print(index+1, "/", len(recipients))
try:
server.sendmail(sender_email, recipient, build_msg(recipient).as_string())
except Exception as e:
print("[Failed]", recipient, e)
time.sleep(1)
print("[Finished]")
except Exception as e:
print("[Error]", e)
finally:
server.quit()
# https://myaccount.google.com/u/0/lesssecureapps
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender_email = '[email protected]'
sender_password = '123456789'
recipient_email = '[email protected]'
subject = 'New letter'
html_content = """
<html>
<body>
<h3>Hello world!</h3>
</body>
</html>
"""
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(html_content, 'html'))
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
print("Successfully!")
except Exception as e:
print(f"Error: {e}")
finally:
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment