-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsendmail.py
More file actions
55 lines (45 loc) · 1.6 KB
/
sendmail.py
File metadata and controls
55 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# --------------------------------------------------------------------------------------------
# Generic python script for sending email over any SMTP host
# Created by: Pooja Bhat
# Date: 26-May-2020
# --------------------------------------------------------------------------------------------
import smtplib, email
from email import encoders
import os
import sys
SMTP_SERVER = 'xxxxx'
SMTP_PORT = 32
# SMTP_USERNAME = ''
# SMTP_PASSWORD = ''
print("To send an attachment execute: `python sendmail.py /path/to/your/attachment`\n\n")
SMTP_FROM = raw_input("Enter FROM address: ")
SMTP_TO = raw_input("Enter TO address: ")
SUBJECT = raw_input("SUBJECT: ")
text=""""""
# MESSAGE = 'Subject: {}\n\n{}'.format(SUBJECT,text)
MESSAGE = '{}\n'.format(text)
msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
if len(sys.argv)==2:
FILENAME = sys.argv[1]
if os.path.exists(FILENAME):
attachment = email.MIMEBase.MIMEBase('text', 'plain')
attachment.set_payload(open(FILENAME).read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(FILENAME))
encoders.encode_base64(attachment)
msg.attach(attachment)
else:
print('The file that you are trying to attach does not exist')
print('Aborting sendmail')
sys.exit()
msg.attach(body)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)
msg.add_header('Subject', SUBJECT)
# Send message
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
# mailer.connect()
# mailer.login(SMTP_USERNAME, SMTP_PASSWORD)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()
print("Message has been sent")