-
Notifications
You must be signed in to change notification settings - Fork 9
/
python-email.py
executable file
·52 lines (40 loc) · 1.58 KB
/
python-email.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
#!/usr/bin/python
# to test email from python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
email_from_addr = "myfromemail@gmail.com"
email_to_addr = "mytoemail@gmail.com"
email_smtp_server = "smtp.gmail.com"
email_smtp_port = "587"
email_user = "myfromemail"
email_password = "mypassword"
# Email Subject
email_subject = "Welcome to Python Email Test"
# HTML + CSS for body
email_body = None
email_body_header = ' '
email_body_header = email_body_header + '<html><head></head><body>'
email_body_header = email_body_header + '<style type="text/css"></style>'
email_body_header = email_body_header + '<br><p>Hello Team,<br><br>This is a test email.<br>'
email_body_content = ' '
email_body_content = email_body_content + '<H1>This is main content area</h1>'
email_body_footer = ' '
email_body_footer = email_body_footer + '<br>Thank you'
email_body_footer = email_body_footer + '<br>Support Team<br>'
email_body = str(email_body_header) + str(email_body_content) + str(email_body_footer)
# Prepare email content - MIMEMultipart
message = MIMEMultipart('alternative')
message['From'] = email_from_addr
message['To'] = email_to_addr
message['Subject'] = email_subject
body = email_body
message.attach(MIMEText(body, 'html'))
#print (email_body)
server = smtplib.SMTP(email_smtp_server,int(email_smtp_port))
#server = smtplib.SMTP(email_smtp_server, int(email_smtp_port), timeout=30,)
text = message.as_string()
server.starttls()
server.login(email_user, email_password)
server.sendmail(email_from_addr,email_to_addr,message.as_string())
server.quit()