Spaces:
Runtime error
Runtime error
Create send_email.py
Browse files- send_email.py +35 -0
send_email.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import smtplib
|
| 2 |
+
from email.mime.multipart import MIMEMultipart
|
| 3 |
+
from email.mime.base import MIMEBase
|
| 4 |
+
from email import encoders
|
| 5 |
+
|
| 6 |
+
def send_email(pdf_path, to_email):
|
| 7 |
+
from_email = "your_email@example.com"
|
| 8 |
+
subject = "Quote Invoice"
|
| 9 |
+
body = "Please find attached the quote invoice."
|
| 10 |
+
|
| 11 |
+
# Create the email message
|
| 12 |
+
msg = MIMEMultipart()
|
| 13 |
+
msg['From'] = from_email
|
| 14 |
+
msg['To'] = to_email
|
| 15 |
+
msg['Subject'] = subject
|
| 16 |
+
|
| 17 |
+
msg.attach(MIMEText(body, 'plain'))
|
| 18 |
+
|
| 19 |
+
# Attach the PDF
|
| 20 |
+
attachment = open(pdf_path, "rb")
|
| 21 |
+
part = MIMEBase('application', 'octet-stream')
|
| 22 |
+
part.set_payload(attachment.read())
|
| 23 |
+
encoders.encode_base64(part)
|
| 24 |
+
part.add_header('Content-Disposition', f"attachment; filename={pdf_path.split('/')[-1]}")
|
| 25 |
+
msg.attach(part)
|
| 26 |
+
|
| 27 |
+
# Send the email via SMTP
|
| 28 |
+
server = smtplib.SMTP('smtp.gmail.com', 587)
|
| 29 |
+
server.starttls()
|
| 30 |
+
server.login(from_email, "your_email_password")
|
| 31 |
+
server.sendmail(from_email, to_email, msg.as_string())
|
| 32 |
+
server.quit()
|
| 33 |
+
|
| 34 |
+
# Call to send email
|
| 35 |
+
send_email(pdf_file, "customer_email@example.com")
|