-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-email_html.py
More file actions
37 lines (32 loc) · 1.04 KB
/
8-email_html.py
File metadata and controls
37 lines (32 loc) · 1.04 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
from email.message import EmailMessage
import smtplib
import ssl
import mimetypes
password = str(open('senha', 'r').read())
from_email = '6ff1fcdbbd7ad8'
to_email = 'erickbarretodasilva@gmail.com'
subject = 'Curso Python'
body = open('files/index.html.txt', 'r', encoding='utf-8').read()
message = EmailMessage()
message['From'] = from_email
message['To'] = to_email
message['Subject'] = subject
message.set_content(body, subtype='html')
anexo = 'files/estrela.png'
print(mimetypes.guess_type(anexo)[0])
mime_type, mime_subtype = mimetypes.guess_type(anexo)[0].split('/')
with open(anexo, 'rb') as a:
message.add_attachment(
a.read(),
maintype=mime_type,
subtype=mime_subtype,
filename=anexo
)
try:
with smtplib.SMTP('sandbox.smtp.mailtrap.io', 2525) as smtp:
smtp.starttls()
smtp.login(from_email, password)
smtp.sendmail("Private Person <from@example.com>", to_email, message.as_string())
print("Email enviado com sucesso!")
except Exception as e:
print(f"Ocorreu um erro: {e}")