-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemails.py
More file actions
31 lines (24 loc) · 904 Bytes
/
emails.py
File metadata and controls
31 lines (24 loc) · 904 Bytes
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
#!/usr/bin/env python3
import email.message
import mimetypes
import os.path
import smtplib
def generate(sender, recipient, subject, body, attachment_path):
#Basic email format
message = email.message.EmailMessage()
message["From"] = sender
message["To"] = recipient
message["subject"] = subject
message.set_content(body)
#Process attachment to add to email
attachment_filename = os.path.basename(attachment_path)
mime_type, _ = mimetypes.guess_type(attachment_path)
mime_type, mime_subtype = mime_type.split('/',1)
with open(attachment_path, 'rb') as ap:
message.add_attachment(ap, read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename)
return message
def send(message):
#Sends message to the configured SMTP
mail.server = smtplib.SMTP('localhost')
mail.server.send_message(message)
mail.server.quit()