-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail.py
77 lines (63 loc) · 2.27 KB
/
sendmail.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#/usr/bin/env python
# simple email message library
import smtplib
# for mime messages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
# package for fetching data from clipboard
import pyperclip
# config parser
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('.config')
# function for sending email
def sendemail(from_addr, to_addr_list, cc_addr_list,
subject, message,
login, password,
smtpserver=parser.get('smtp')):
"""Function for connecting, authenticating , sending mail and quitting from smtp server """
server = smtplib.SMTP()
server.connect(smtpserver)
server.starttls()
server.login(login,password)
problems = server.sendmail(from_addr, to_addr_list, message.as_string())
server.quit()
return problems
# Taking required inputs
username = raw_input("Enter Username!")
password = raw_input("Enter Password!")
to_addr = raw_input("Enter To Address")
cc_addr = raw_input("Enter CC Address")
subject = raw_input("Enter Subject!")
message = pyperclip.getcb().strip()
if(len(message)==0):
message = raw_input("Enter Message!")
_attachment = raw_input("Full name of the attachment! (assumes file is in current directory)") or 'image.jpg'
#Preparing mime message
mime_message = MIMEMultipart()
"""# Converting simple text to mime text and adding it to mime message
mime_text= MIMEText(message)
mime_message.attach(mime_text)"""
# Adding html content(image) to mime message
msgText = MIMEText('<b>'+message+'</b><br><img src="cid:'+_attachment+'"><br>', 'html')
mime_message.attach(msgText)
# Adding image as attachment
mime_image = None
with open(_attachment, 'rb') as mimefile:
mime_image = MIMEImage(mimefile.read())
if mime_image:
mime_image.add_header("Content-ID",_attachment)
mime_message.attach(mime_image)
# Adding sender and receiver to mime message
mime_message["To"] = to_addr
mime_message["From"] = ''
mime_message["Subject"] = subject
# Sending mime message
sendemail(from_addr = '',
to_addr_list = [to_addr],
cc_addr_list = [cc_addr],
subject = subject,
message = mime_message,
login = username,
password = password)