Skip to content

Instantly share code, notes, and snippets.

@luerhard
Last active July 3, 2017 12:47
Show Gist options
  • Save luerhard/27faa93a1ba89378a98d98b1ee2c4514 to your computer and use it in GitHub Desktop.
Save luerhard/27faa93a1ba89378a98d98b1ee2c4514 to your computer and use it in GitHub Desktop.
Send email from within a python script from one email-adress to another
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def send_mail(message, *subject,**kwargs):
smtp_host = 'smtp.xxx.de'
login, password = 'login-email', 'password'
recv = 'receiving-email'
try:
if subject:
subject = Header(subject[0])
else:
subject = 'Kein Betreff'
message = MIMEText(message, 'plain', 'utf-8')
message['Subject'] = subject
message['From'] = f'Python Script <{login}>'
message['To'] = recv
except Exception as e:
print(e)
return
s = smtplib.SMTP(smtp_host, 587, timeout=10)
#s.set_debuglevel(1)
try:
s.starttls()
s.login(login, password)
s.sendmail(message['From'], message['To'], message.as_string())
finally:
s.quit()
#message = "empty message"
#subject = "empty subject"
#send_mail(message, subject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment