Skip to content

Commit

Permalink
Email sending from sendgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzadelic committed May 30, 2024
1 parent a0be2dd commit 8aa4f31
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
10 changes: 6 additions & 4 deletions crawler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@
DATABASE_URL = os.getenv("DATABASE_URL")
TEMPLATES_DIR = "{}/templates/".format(os.path.dirname(__file__))

SMTP_SERVER = os.getenv("SMTP_SERVER", "")
SMTP_PORT = os.getenv("SMTP_PORT", 465)
SMTP_USERNAME = os.getenv("SMTP_USERNAME", "")
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "")
# SMTP_SERVER = os.getenv("SMTP_SERVER", "")
# SMTP_PORT = os.getenv("SMTP_PORT", 465)
# SMTP_USERNAME = os.getenv("SMTP_USERNAME", "")
# SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "")
EMAIL_FROM = os.getenv("EMAIL_FROM", "")
SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY", "")

SENTRY_DSN = os.getenv("SENTRY_DSN", None)
47 changes: 25 additions & 22 deletions crawler/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import From, To, Subject, HtmlContent, Mail
import urllib.request as urllib
from jinja2 import Environment, FileSystemLoader
from scrapy.utils.project import get_project_settings

Expand All @@ -18,20 +16,25 @@ def generate_mail(context):


def send_mail(subject, receivers, body):
SMTP_SERVER = settings.get("SMTP_SERVER")
SMTP_PORT = settings.get("SMTP_PORT")
SMTP_USERNAME = settings.get("SMTP_USERNAME")
SMTP_PASSWORD = settings.get("SMTP_PASSWORD")

message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = SMTP_USERNAME
message["To"] = ", ".join(receivers)
html_body = MIMEText(body, "html")
message.attach(html_body)

context = ssl.create_default_context()

with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT, context=context) as server:
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(SMTP_USERNAME, receivers, message.as_string())
EMAIL_FROM = settings.get("EMAIL_FROM")
SENDGRID_API_KEY = settings.get("SENDGRID_API_KEY")

sendgrid_client = SendGridAPIClient(api_key=SENDGRID_API_KEY)
from_email = From(EMAIL_FROM)
to_emails = [To(receiver) for receiver in receivers]
subject = Subject(subject)
html_content = HtmlContent(body)

message = Mail(
from_email=from_email,
to_emails=to_emails,
subject=subject,
plain_text_content=None,
html_content=html_content,
)

try:
sendgrid_client.send(message=message)
except urllib.HTTPError as e:
print(e.read())
exit()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Scrapy==2.11.2
MarkupSafe==2.0.1
scrapy-useragents==0.0.1
sendgrid==6.11.0
SQLAlchemy==1.3.22
psycopg2-binary==2.9.9
Jinja2==2.11.2
Expand Down

0 comments on commit 8aa4f31

Please sign in to comment.