Skip to content

Commit fcd62ef

Browse files
author
Ra Inta
committed
Added automatic email sending script
1 parent 9923778 commit fcd62ef

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

SendEmail/SendEmail.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import smtplib
2+
from email.mime.multipart import MIMEMultipart
3+
from email.mime.text import MIMEText
4+
5+
import getpass
6+
7+
email_user = input("Email address to send from:\n")
8+
9+
email_domain = email_user.split("@")[1]
10+
11+
if email_domain in ["outlook.com", "hotmail.com"]:
12+
smtpserver = "smtp.live.com:587" # Kludge for hotmail/outlook addresses
13+
else:
14+
smtpserver = "smtp." + email_domain + ":587" # Guess for SMTP server; works for gmail, yahoo etc.
15+
16+
to_addr = input("Address to send to:\n")
17+
18+
subject = input("What is your email subject?\n")
19+
20+
body_text = input("What message did you want to email? (currently this is only one line)\n\n")
21+
22+
body_html= """\
23+
<html>
24+
<head></head>
25+
<body>
26+
<h1>I sent this email automatically, using Python!</h1>
27+
<p>
28+
"""
29+
30+
body_html += body_text
31+
32+
body_html += """</p>
33+
</body>
34+
</html>
35+
"""
36+
37+
def send_mail(to_addr, subject="Test email",
38+
body_text="Test message",
39+
body_html="Test message",
40+
from_addr=email_user, email_user=email_user,
41+
email_passwd=email_passwd,
42+
smtpserver="smtp.live.com:587"):
43+
"""A function to send email, in MIME multi-part (plain-text and HTML).
44+
45+
For example: to send to myself:
46+
send_mail(to_addr, subject, body_text=body_text, body_html=body_html)
47+
"""
48+
49+
# Construct the message header
50+
message = MIMEMultipart('alternative')
51+
message['From'] = from_addr
52+
message['To'] = to_addr
53+
message['Subject'] = subject
54+
55+
# Append the body text
56+
message.attach(MIMEText(body_text, 'plain'))
57+
message.attach(MIMEText(body_html, 'html'))
58+
59+
# Connect to the SMTP server
60+
server = smtplib.SMTP(smtpserver)
61+
server.starttls()
62+
server.login(email_user, email_passwd)
63+
problems = server.sendmail(from_addr, to_addr, message.as_string())
64+
server.quit()
65+
66+
67+
email_passwd = getpass.getpass()
68+
69+
send_mail(to_addr, subject, body_text, body_html, email_user, email_user, email_passwd, smtpserver)

0 commit comments

Comments
 (0)