Skip to content

Instantly share code, notes, and snippets.

@ser1zw
Last active January 17, 2018 18:09
Show Gist options
  • Save ser1zw/4370039 to your computer and use it in GitHub Desktop.
Save ser1zw/4370039 to your computer and use it in GitHub Desktop.
Send E-mail from eml file in Python
#!/usr/bin/env python
# -*- mode: python; coding: utf-8 -*-
import sys
import os.path
import smtplib
if len(sys.argv) <= 2:
print('Usage:')
print(' $ python ' + sys.argv[0] + ' mailfrom rcptto <emlfile>')
print('')
print('Parameter:')
print(' mailfrom: MAIL FROM address.')
print(' rcptto: RCPT TO address.')
print(' emlfile: Message file in eml format. When emlfile is not specified, an empty message will be sent.')
print('')
print('Example:')
print(' $ python ' + sys.argv[0] + ' [email protected] [email protected] mail.eml')
sys.exit(0)
server = 'localhost'
port = 25
mailfrom = sys.argv[1]
rcptto = sys.argv[2].split(',')
message = ''
if len(sys.argv) >= 4:
filename = sys.argv[3]
if not os.path.isfile(filename):
print('File "' + filename + '" not found.')
sys.exit(0)
f = None
try:
f = open(filename)
message = f.read()
finally:
if f != None:
f.close()
smtp = None
try:
try:
smtp = smtplib.SMTP(server, port)
smtp.sendmail(mailfrom, rcptto, message)
except Exception as e:
print('Failed to send mail.')
print(str(e))
else:
print('Succeeded to send mail.')
finally:
if smtp != None:
smtp.close()
@Sulverus
Copy link

i think:
mailfrom = sys.argv[1]
rcptto = sys.argv[2].split(',')

@ser1zw
Copy link
Author

ser1zw commented Dec 27, 2015

Fixed. Thx!

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