-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail.py
More file actions
72 lines (55 loc) · 1.99 KB
/
Copy pathgmail.py
File metadata and controls
72 lines (55 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
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
#
# Gmail integration requires an account with "Less Secure App Access" enabled
# in the security settings. Consider using a dedicated gmail account for this
# app.
#
import smtplib
import sys
def send_email(local_user, local_password, to, subject, body, mail_server, mail_port):
# Defines function to send email using generic SMTP service
# local_password is currently not implemented and requires a server without authentication
try:
server = smtplib.SMTP(mail_server, mail_port)
server.ehlo()
except:
raise Exception('something went wrong with login')
if type(to) == list:
email_to = ', '.join(to)
else:
email_to = to
email_text = f'From: {local_user}\nTo: {email_to}\nSubject: {subject}\n\n{body}'
print(email_text)
try:
server.sendmail(local_user, to, email_text)
print('Email sent.')
except:
raise Exception('something went wrong with email send')
server.close()
def send_gmail(gmail_user, gmail_password, to, subject, body):
# Defines function to send e-mail using gmail service. Requires gmail account settings
# to allow "Less secure apps" or login will be rejected.
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
except:
print('Something went wrong with login...')
end()
if type(to)==list:
email_to = ', '.join(to)
else:
email_to = to
email_text = f'From: {gmail_user}\nTo: {email_to}\nSubject: {subject}\n\n{body}'
print(email_text)
try:
server.sendmail(gmail_user, to, email_text)
print('Email sent!')
except:
print('Something went wrong with email send...')
server.close()
if __name__ == '__main__':
#
# send e-mail with cli
# python3 gmail.py gmail_user@gmail.com password 'recipient1,recipient2,etc' 'subject' 'body'
#
send_gmail(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5])