Skip to content

Instantly share code, notes, and snippets.

@lnksz
Created October 21, 2021 04:38
Show Gist options
  • Save lnksz/91be8f9872cc39f936c7ea2571313391 to your computer and use it in GitHub Desktop.
Save lnksz/91be8f9872cc39f936c7ea2571313391 to your computer and use it in GitHub Desktop.
round-mail
#!/bin/env python
# -*- encoding: utf-8 -*-
# Send out "personalised" text mails to a list of people,
# without Cc hacks and without sharing the addresses.
# Uses a gmail account, with "less secure apps" enabled.
# Awaits own address in environment var "U" and password in environment var "P"
# $ U=me@gmail.com P=secret ./thisscript.py
# Note: leading space in the terminal should exlude the command from history
import smtplib, ssl
from email.message import EmailMessage
import os
mailtxt = \
"""
Hi {}!
Lorem ipsum and some more text!
BR,
lnksz
"""
recipients = (
{"name": u"Béla", "mail": "bela@email.com"},
{"name": u"Aladár", "mail": "aladar@email.com"})
def main():
u = os.environ.get('U')
p = os.environ.get('P')
ssl_context = ssl.create_default_context()
service = smtplib.SMTP_SSL('smtp.gmail.com', 465, context=ssl_context)
service.login(u, p)
print(f"Sending as {u}")
for r in recipients:
print(f"Mailing {r['name']}")
msg = EmailMessage()
msg['From'] = u
msg['Subject'] = 'roundmail'
msg['To'] = r['mail']
msg.set_content(mailtxt.format(r['name']))
service.send_message(msg)
service.quit()
if __name__ == '__main__':
main()
@lnksz
Copy link
Author

lnksz commented Oct 21, 2021

on my system /bin/env python is py3, you may need to add python3 there on some older Debian/Ubuntu distros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment