-
Notifications
You must be signed in to change notification settings - Fork 2
/
sndgmail.py
74 lines (70 loc) · 2.28 KB
/
sndgmail.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
import sys
import os
import os.path
import datetime
import tempfile
import re
import shutil
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
#--------------------------------------------------------------------------
# send_mail: Enva un email usando SMTP, usando los siguientes paetros:
#
# -toaddr Direccin a la que debe enviarse el correo
# -fromaddr Desde que direccin debe enviarse
# -password Contrasa de la diren desde
# -subject Tema del correo
# -body Cuerpo del correo
# -attachfp Path del archivo attachado
#--------------------------------------------------------------------------
def send_email(toaddr,
fromaddr,
password,
subject,
body='',
attachfp='',
):
try:
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
if attachfp <> '':
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(attachfp))
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
except:
pass
#--------------------------------------------------------------------------
# Punto de Entrada del Programa
#--------------------------------------------------------------------------
if __name__ == "__main__":
toaddr = sys.argv[1]
fromaddr = sys.argv[2]
password = sys.argv[3]
subject = sys.argv[4]
body=''
if len(sys.argv) >= 5:
body = sys.argv[4]
attach_full_path=''
if len(sys.argv) >= 6:
attach_full_path = sys.argv[5]
send_email(toaddr, \
fromaddr, \
password, \
subject, \
body, \
attach_full_path)