Skip to content

Instantly share code, notes, and snippets.

@zobayer1
Last active November 1, 2023 07:23
Show Gist options
  • Select an option

  • Save zobayer1/3c69cc2f3342f25339a82ec7b1369129 to your computer and use it in GitHub Desktop.

Select an option

Save zobayer1/3c69cc2f3342f25339a82ec7b1369129 to your computer and use it in GitHub Desktop.

Revisions

  1. zobayer1 revised this gist Jul 13, 2021. 1 changed file with 11 additions and 7 deletions.
    18 changes: 11 additions & 7 deletions send_mail.py
    Original file line number Diff line number Diff line change
    @@ -2,37 +2,41 @@
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.utils import formataddr
    from typing import List


    class SendMail(object):

    host, port = "smtp.gmail.com", 465
    username = "[email protected]"
    password = "your_app_password"
    sender = formataddr(("Your Name", "[email protected]"))

    def __init__(self):
    self.connected = False
    self.server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
    self.server.ehlo()
    try:
    self.server = smtplib.SMTP_SSL(self.host, self.port)
    self.server.ehlo()
    self.server.login(self.username, self.password)
    self.connected = True
    print("Connected to GMail server")
    except Exception as err:
    print(f"Error connecting to server: {str(err)}")

    def __del__(self):
    if self.connected:
    try:
    self.server.close()
    print("Connection closed")
    print("Connection closed")
    except Exception as err:
    print(f"Error occurred while closing connection: {str(err)}")

    def send(self, receivers: List[str], subject: str, body: str, subtype: str = "html") -> bool:
    try:
    message = MIMEMultipart("alternative")
    message["To"] = ", ".join(receivers)
    message["From"] = self.sender
    message["Subject"] = subject
    message.attach(MIMEText(body, subtype))
    self.server.sendmail(self.username, receivers, message.as_string())
    self.server.sendmail(self.sender, receivers, message.as_string())
    print("Email sent")
    return True
    except Exception as err:
  2. zobayer1 created this gist Jun 28, 2021.
    40 changes: 40 additions & 0 deletions send_mail.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    # -*- coding: utf-8 -*-
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from typing import List


    class SendMail(object):

    username = "[email protected]"
    password = "your_app_password"

    def __init__(self):
    self.connected = False
    self.server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
    self.server.ehlo()
    try:
    self.server.login(self.username, self.password)
    self.connected = True
    print("Connected to GMail server")
    except Exception as err:
    print(f"Error connecting to server: {str(err)}")

    def __del__(self):
    if self.connected:
    self.server.close()
    print("Connection closed")

    def send(self, receivers: List[str], subject: str, body: str, subtype: str = "html") -> bool:
    try:
    message = MIMEMultipart("alternative")
    message["To"] = ", ".join(receivers)
    message["Subject"] = subject
    message.attach(MIMEText(body, subtype))
    self.server.sendmail(self.username, receivers, message.as_string())
    print("Email sent")
    return True
    except Exception as err:
    print(f"Error occurred while sending email: {str(err)}")
    return False