-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachmentEmailSentUsingPython.py
More file actions
59 lines (47 loc) · 1.48 KB
/
AttachmentEmailSentUsingPython.py
File metadata and controls
59 lines (47 loc) · 1.48 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import smtplib as s
from email.message import EmailMessage
# Define the SMTP server
SMTP_SERVER = 'smtp.gmail.com'
PORT = 587
# Define email credentials
email_address = ''
password = ''
# Define email content
subject = ""
body = """
"""
# Path to the image file you want to attach
attachment_path = r''
# Define recipient email addresses
recipients = ['']
# Create the email message
msg = EmailMessage()
msg['From'] = email_address
msg['To'] = ', '.join(recipients)
msg['Subject'] = subject
msg.set_content(body)
# Add the image attachment
try:
with open(attachment_path, 'rb') as file:
file_data = file.read()
file_name = os.path.basename(attachment_path)
msg.add_attachment(file_data, maintype='image', subtype='jpeg', filename=file_name)
except Exception as e:
print(f"Failed to read the attachment: {e}")
raise
try:
# Connect to the SMTP server
with s.SMTP(SMTP_SERVER, PORT) as smtp:
smtp.ehlo() # Identify ourselves to the SMTP server
smtp.starttls() # Start TLS encryption
smtp.ehlo() # Re-identify ourselves as an encrypted connection
# Log in to the email account
smtp.login(email_address, password)
# Send the email
smtp.send_message(msg)
print("Mail sent successfully.")
except Exception as e:
# Print an error message if the email fails to send
print("Failed to send mail.")
print(f"Error: {e}")