-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use Postmark for contact emails
- Loading branch information
1 parent
24db2f0
commit 68de778
Showing
1 changed file
with
24 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,13 @@ | ||
import json | ||
import os | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
import requests | ||
|
||
from typing import Any, Dict, Optional | ||
import solara | ||
|
||
|
||
# Set up email server details | ||
smtp_server = "smtp.gmail.com" | ||
smtp_port = 465 # For SSL | ||
email_user = None | ||
email_password = None | ||
try: | ||
email_user = os.environ.get("CONTACT_EMAIL_USER") | ||
email_password = os.environ.get("CONTACT_EMAIL_PASSWORD") | ||
postmark_api_key = os.environ["POSTMARK_API_KEY"] | ||
except Exception: | ||
pass | ||
|
||
|
@@ -35,31 +28,36 @@ def Contact( | |
error: solara.Reactive[Optional[str]] = solara.use_reactive(None) | ||
|
||
def send(*_ignore): | ||
if email_user is None or email_password is None: | ||
error.set("Email server details not set. Please set environment variables.") | ||
if postmark_api_key is None: | ||
error.set("Email service not properly configured. Please contact the site administrator at [email protected].") | ||
else: | ||
# Create the email content | ||
msg = MIMEMultipart() | ||
msg["From"] = email_user | ||
msg["To"] = "contact@solara.dev" | ||
msg = {} | ||
msg["From"] = "[email protected]" | ||
msg["To"] = "solara@widgetti.io" | ||
msg["Subject"] = email_subject | ||
msg["ReplyTo"] = email.value | ||
|
||
# Email body | ||
body = f""" | ||
First Name: {first_name.value} | ||
Last Name: {last_name.value} | ||
Email: {email.value} | ||
Company: {company.value} | ||
Message: {message.value} | ||
msg["HtmlBody"] = f""" | ||
<b>First Name</b>: {first_name.value}<br /> | ||
<b>Last Name</b>: {last_name.value}<br /> | ||
<b>Email</b>: {email.value}<br /> | ||
<b>Company</b>: {company.value}<br /> | ||
<b>Message</b>: {message.value}<br /> | ||
""" | ||
msg.attach(MIMEText(body, "plain")) | ||
|
||
# Send the email | ||
try: | ||
server = smtplib.SMTP_SSL(smtp_server, smtp_port) | ||
server.login(email_user, email_password) | ||
server.sendmail(email_user, msg["To"], msg.as_string()) | ||
server.quit() | ||
requests.post( | ||
"https://api.postmarkapp.com/email", | ||
headers={ | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
"X-Postmark-Server-Token": postmark_api_key, | ||
}, | ||
data=json.dumps(msg), | ||
) | ||
print("Email sent successfully!") | ||
except Exception as e: | ||
error.set(f"Error sending email: {e}") | ||
|