Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How do you send an email
-
Hi so I tried using the email documentation and it was just crazy confusing can anyone walk me through the process of sending in email in a script.
-
-
Here is another simple example that I used myself, but anonamized. It works when the message text is ASCII.
#!/usr/bin/env python3 # Script to send an email. Runs with Python 3.6 # Import smtplib for the actual sending function # Import the email modules we'll need import smtplib from email.message import EmailMessage ##### CONFIGURATION ##### SMTP_SERVER = 'smtp.example.com' SMTP_PORT = 25 SMTP_LOGIN = None # user name if needed SMTP_PASSWORD = 'secret' # None if not needed ######################### # Info for this message # Adressee name = 'adressee name' email = 'adressee@example.com' sender_name = 'Sender Name' sender_email = 'sender@example.com' subject = 'Test message' message = '''Dear {name}, Thank you for your contribution to Python. With kind regards, {sender_name} {sender_email} ''' ######################### # build and send an email. # message = formatted message with optional {xxx} placeholders # for name, email, sender_name, sender_email. def send_email(message, from_name, from_email, to_name, to_email, subject): body = message.format(name=to_name, email=to_email, sender_name=from_name, sender_email=from_email) msg = EmailMessage() msg.set_content(body) msg['Subject'] = subject msg['From'] = '"{name}" <{address}>'.format(name=from_name, address=from_email) msg['To'] = '{name} <{address}>'.format(name=to_name, address=to_email) # Send the message via the designated SMTP server. s = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) # Login to the SMTP server if necessary if SMTP_LOGIN: s.login(SMTP_LOGIN, SMTP_PASSWORD) s.send_message(msg) s.quit() send_email(message, sender_name, sender_email, name, email, subject) print("Email sent to", name, "<"+email+">")