-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathEmail-sender.py
87 lines (71 loc) · 2.11 KB
/
Email-sender.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# coding: utf-8
# Marta Laís, 2018. https://github.com/martalais/
# Automatic sending of e-mails with attachments via the terminal.
# After the implementation, just create a routine in the crontab for its execution.
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.Utils import COMMASPACE, formatdate
import smtplib
def enviaEmail(servidor, porta, FROM, PASS, TO, subject, texto, anexo=[]):
"""
Sends an email.
Args:
servidor: (str): write your description
porta: (int): write your description
FROM: (str): write your description
PASS: (todo): write your description
TO: (str): write your description
subject: (str): write your description
texto: (str): write your description
anexo: (todo): write your description
"""
global saida
servidor = servidor
porta = porta
FROM = FROM
PASS = PASS
TO = TO
subject = subject
texto = texto
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = subject
msg.attach(MIMEText(texto))
for i in anexo:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(i, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment;filename="%s"'% os.path.basename(i))
msg.attach(part)
try:
gm = smtplib.SMTP(servidor,porta)
gm.ehlo()
gm.starttls()
gm.ehlo()
gm.login(FROM, PASS)
gm.sendmail(FROM, TO, msg.as_string())
gm.close()
except Exception,e:
mensagemErro = "Erro ao enviar o e-mail." % str(e)
print '%s' % mensagemErro
# E-mail addressee.
addressee = ''
# Subject of the email.
subject = ''
# E-mail message.
message = ''
# Smtp server address that will be used.
# An example is: smtp.gmail.com
server = ''
# SMTP server port.
# An example is: 587 (gmail)
port =
# E-mail address and sender password.
sender = ''
password = ''
# E-mail sending function call.
# Note that in [""] the exact path of the attachment must be referenced.
sendmail (server, port, sender, password, addressee, subject, message, [""])