-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmail From Python.py
More file actions
55 lines (47 loc) · 1.55 KB
/
Copy pathEmail From Python.py
File metadata and controls
55 lines (47 loc) · 1.55 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
import smtplib
from email.mime.text import MIMEText
email_address = 'username@email.com'
password = 'password'
#Without CC
def simple_email (from_email, recipients_CC, subject, message):
msg = MIMEText(message,'html')
msg['To'] = ", ".join(recipients_CC)
msg['Subject'] = subject
msg['From'] = from_email
header = 'From: %s\n' % from_email
header += 'To: %s\n' % msg.get('To')
header += 'Subject: %s\n' % subject
message = header + message
try:
smtpObj = smtplib.SMTP('smtp.office365.com', 587)
except Exception as e:
print(e)
smtpObj = smtplib.SMTP_SSL('smtp.office365.com', 465)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(email_address, password)
smtpObj.sendmail(from_email, recipients_CC, msg.as_string())
smtpObj.quit()
#With CC
def simple_email_cc (from_email, recipients, recipients_CC, subject, message):
msg = MIMEText(message,'html')
msg['To'] = ", ".join(recipients)
msg['CC'] = ", ".join(recipients_CC)
msg['Subject'] = subject
msg['From'] = from_email
header = 'From: %s\n' % from_email
header += 'To: %s\n' % msg.get('To')
header += 'CC: %s\n' % msg.get('CC')
header += 'Subject: %s\n' % subject
message = header + message
to_email_all = recipients + recipients_CC
try:
smtpObj = smtplib.SMTP('smtp.office365.com', 587)
except Exception as e:
print(e)
smtpObj = smtplib.SMTP_SSL('smtp.office365.com', 465)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(email_address, password)
smtpObj.sendmail(from_email, to_email_all, msg.as_string())
smtpObj.quit()