-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsendmail.py
More file actions
47 lines (45 loc) · 1.99 KB
/
sendmail.py
File metadata and controls
47 lines (45 loc) · 1.99 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
import smtplib
import getpass
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import datetime
import re
def mail():
"""
Docstring for mail function
mail(receiver), receiver should be email address
this function will ask sender email and password
for GMAIL, please make sure to allow less secure apps
https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
"""
split_term = '@'
sender = str(input("Please enter sender email: "))
receiver = str(input("Please enter receiver email: "))
sen = re.split(split_term, sender)
if sen[1] == 'gmail.com':
allow = str(input("Did you enable allow secure apps on gmail settings y or n? "))
if allow.lower() == 'y':
password = getpass.getpass(prompt='Enter {} Password password: '.format(sender))
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.starttls()
smtpObj.login(sender, password)
message = MIMEMultipart()
message["Subject"] = "Email generated by Python"
message["From"] = sender
message["To"] = receiver
user = re.split(split_term, receiver)
# Write the mail body text part
text = """Hi, {} \nThis email generated by Python programm! \nEmail generated on """.format(user[0]) +str(datetime.datetime.now().strftime("%Y %m %d %H:%M:%S'"))
part1 = MIMEText(text, "plain")
message.attach(part1)
try:
smtpObj.sendmail(sender, receiver, message.as_string())
print("{} Sent an email to {}".format(message.as_string(), receiver))
except:
print('error sending mail to {}'.format(receiver))
smtpObj.quit()
else:
print("Please enable allow secure apps on https://myaccount.google.com/u/0/lesssecureapps?pli=1&pageId=non")
else:
print("sender email {} is different than gmail.com".format(sen[1]))
mail(receiver)