-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
73 lines (59 loc) · 2.52 KB
/
mail.py
File metadata and controls
73 lines (59 loc) · 2.52 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from getpass import getpass # Used for securely entering your password
import os
user_Mail = ""#PLACE YOUR MAIL HERE
def sendEmail(recipient_email,subject,password = None):
def returnEmailMsg():
# Specify the path to the text file within the "output" folder
file_path = "output/email.txt"
# Initialize an empty string variable to store the contents
file_contents = ""
# Try to open the file and read its contents
try:
with open(file_path, "r",encoding="utf-8") as file:
file_contents = file.read()
except FileNotFoundError:
print("EMAIL: The file does not exist.")
except Exception as e:
print("EMAIL: An error occurred:", e)
# Check if the file_contents variable contains the string from the file
if file_contents:
return file_contents
else:
print("EMAIL: No content found in the file.")
# Sender's email address and password
sender_email = user_Mail
if password == None or password == "":password = getpass("Enter your Gmail password: ")
message = returnEmailMsg()
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain',"utf-8"))
# Add attachments
attachment_files = ["output/CV.pdf", "output/Motiv.pdf"] # File paths
for file_path in attachment_files:
with open(file_path, "rb") as file:
part = MIMEApplication(file.read(), Name=os.path.basename(file_path))
part['Content-Disposition'] = f'attachment; filename="{os.path.basename(file_path)}"'
msg.attach(part)
# Establish a connection to the Gmail SMTP server
smtp_server = "smtp.gmail.com"
smtp_port = 587
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, password)
# Send the email
server.sendmail(sender_email, recipient_email, msg.as_string())
# Close the connection
server.quit()
print("EMAIL: Email sent successfully!")
return 0
except smtplib.SMTPException as e:
print(f"EMAIL: Email sending failed. Error: {e}")
return 1
# Note: If you have 2-Step Verification enabled on your Google account, you may need to generate an "App Password" for the script.