Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/python-email/main.py
5925 views
1
import smtplib
2
import os
3
from email.mime.multipart import MIMEMultipart
4
from email.mime.text import MIMEText
5
from email.mime.base import MIMEBase
6
from email import encoders
7
from platform import python_version
8
9
server = 'smtp.mail.ru'
10
user = '[email protected]'
11
password = 'MySuperPassword'
12
13
recipients = ['[email protected]', '[email protected]']
14
sender = '[email protected]'
15
subject = 'Тема сообщения 213213 777'
16
text = 'Текст сообщения sdf sdf sdf sdaf <b>sdaf sdf</b> fg hsdgh <h1>f sd</h1> dfhjhgs sd gsdfg sdf'
17
html = '<html><head></head><body><p>' + text + '</p></body></html>'
18
19
filepath = "fish.png"
20
basename = os.path.basename(filepath)
21
filesize = os.path.getsize(filepath)
22
23
msg = MIMEMultipart('alternative')
24
msg['Subject'] = subject
25
msg['From'] = 'Python script <' + sender + '>'
26
msg['To'] = ', '.join(recipients)
27
msg['Reply-To'] = sender
28
msg['Return-Path'] = sender
29
msg['X-Mailer'] = 'Python/' + (python_version())
30
31
part_text = MIMEText(text, 'plain')
32
part_html = MIMEText(html, 'html')
33
part_file = MIMEBase('application', 'octet-stream; name="{}"'.format(basename))
34
part_file.set_payload(open(filepath, "rb").read())
35
part_file.add_header('Content-Description', basename)
36
part_file.add_header('Content-Disposition', 'attachment; filename="{}"; size={}'.format(basename, filesize))
37
encoders.encode_base64(part_file)
38
39
msg.attach(part_text)
40
msg.attach(part_html)
41
msg.attach(part_file)
42
43
mail = smtplib.SMTP_SSL(server)
44
mail.login(user, password)
45
mail.sendmail(sender, recipients, msg.as_string())
46
mail.quit()
47
48