Created
June 15, 2017 20:34
-
-
Save lukele/e53a1489335710ab2804590338290620 to your computer and use it in GitHub Desktop.
Resend a .eml file (useful for testing message loading in Mail.app)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- mode: python; coding: utf-8-unix -*- | |
import sys | |
import os.path | |
import smtplib | |
def remove_headers(raw_message, headers=[]): | |
message = [] | |
i = 0 | |
for line in raw_message: | |
if line.strip() == "": | |
break | |
i += 1 | |
skip = False | |
for h in headers: | |
if line.lower().startswith(h.lower()): | |
skip = True | |
break | |
if skip: | |
continue | |
message.append(line) | |
message += raw_message[i:] | |
return message | |
def send_message(filename): | |
global smtpserver | |
if not os.path.isfile(filename): | |
print('File "' + filename + '" not found.') | |
sys.exit(0) | |
with open(filename) as f: | |
message = f.readlines() | |
message = remove_headers(message, ["From:", "To:", "CC:", "BCC:", "Date:", "Message-ID:", "X-"]) | |
message = "".join(message) | |
print "From: %s" % (mailfrom) | |
print "To: %s" % (rcptto) | |
try: | |
smtpserver.sendmail(mailfrom, rcptto, message) | |
except Exception as e: | |
print('Failed to send mail.') | |
print(str(e)) | |
else: | |
print('Succeeded to send mail.') | |
if len(sys.argv) <= 2: | |
print('Usage:') | |
print(' $ python ' + sys.argv[0] + ' mailfrom rcptto <emlfile>') | |
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 send.') | |
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: | |
filenames = sys.argv[3:] | |
# if not os.path.isfile(filename): | |
# print('File "' + filename + '" not found.') | |
# sys.exit(0) | |
# with open(filename) as f: | |
# message = f.readlines() | |
try: | |
smtpserver = smtplib.SMTP("smtp.gmail.com", 587) | |
smtpserver.set_debuglevel(1) | |
smtpserver.ehlo() | |
smtpserver.starttls() | |
smtpserver.ehlo() | |
smtpserver.login('user', 'pass') | |
for filename in filenames: | |
send_message(filename) | |
except Exception as e: | |
print('Failed to send mail.') | |
print(str(e)) | |
else: | |
print('Succeeded to send mail.') | |
finally: | |
if smtpserver != None: | |
smtpserver.close() |
Thx for sharing. Most useful for me... Works like a charm...
This also works
swaks -f [email protected] -t [email protected] -d /path/to/emlfile
Found it later here
https://stackoverflow.com/questions/11328836/tools-for-sending-email
Thanks for your solution with curl ... today it helped me out big time! :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. Also found a way to do it with CURL in Linux
`# !/bin/bash
MAIL_FROM=[email protected]
MAIL_RCPT=[email protected]
EML=0000004b.eml
SERVER='192.168.100.175'
PORT=25
curl smtp://$SERVER:$PORT -v --anyauth --mail-from $MAIL_FROM --mail-rcpt $MAIL_RCPT --upload-file $EML
`
Maybe useful.