You can use this code in order to send with python an attachment:
Code:
#!/usr/bin/python
#use as: python send.py "receiver@gmail.com" "My Topic" "My text" "My attachment.jpg"
import os, re, sys, smtplib, base64
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.MIMEText import MIMEText
from email import encoders
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'myemail@gmail.com'
try:
f1=open("email.conf", "r")
except IOError:
password=raw_input("I don't have your password, please give it to me!\nPassword: ")
f1=open("email.conf", "w")
f1.write(base64.b64encode(password))
f1.close()
else:
password=base64.b64decode(f1.readline())
f1.close()
recipient = sys.argv[1]
subject = ''
message = sys.argv[3]
def main():
msg = MIMEMultipart()
msg['Subject'] = sys.argv[2]
msg['To'] = recipient
msg['From'] = sender
part = MIMEText('text', "plain")
part.set_payload(message)
msg.attach(part)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
fp = open(sys.argv[4], 'rb')
msgq = MIMEBase('audio', 'audio')
msgq.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
encoders.encode_base64(msgq)
# Set the filename parameter
filename=sys.argv[4]
msgq.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(msgq)
# Now send or store the message
qwertyuiop = msg.as_string()
session.sendmail(sender, recipient, qwertyuiop)
session.quit()
os.system('notify-send "Email sent"')
if __name__ == '__main__':
main()
Edit the myemail@gmail.com in the above code to match your e-mail. The first time the script runs it will ask you for your password. It will save it at a .conf file in base64 encoding so as not to be in plain text mode available to anyone... Use as:
Code:
python send.py "receiver@gmail.com" "My Topic" "My text" "My attachment.jpg"
DO NOT give the above script the name email.py (give it any other name, like send_email.py)!
Now, you have to figure out how to save your SMS into a .txt file and send it as an attachment to a recipient of your preference...
Bookmarks