|
| 1 | +import os |
| 2 | +import smtplib |
| 3 | +from email.mime.multipart import MIMEMultipart |
| 4 | +from email.mime.text import MIMEText |
| 5 | +from email.mime.base import MIMEBase |
| 6 | +from email.utils import formatdate |
| 7 | +from email import encoders |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +class EmailClient: |
| 12 | + """ An email client to send emails. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, server: str, server_port: int, user_name: str, password: str): |
| 16 | + """ Class constructor to initialise EmailClient |
| 17 | +
|
| 18 | + Parameters: |
| 19 | + server (str): Email server used by the user |
| 20 | + server_port (int): Port number of the email server |
| 21 | + user_name (str): Email address |
| 22 | + password (str): Email password |
| 23 | + """ |
| 24 | + |
| 25 | + # Email credentials |
| 26 | + self.user_name = user_name |
| 27 | + self.__password = password |
| 28 | + |
| 29 | + # used to add plain texts/HTML parts in the email |
| 30 | + self.__email_content = MIMEMultipart() |
| 31 | + # add sender's info |
| 32 | + self.__email_content['From'] = user_name |
| 33 | + # uses SMTP email protocol to communicate with email service provider |
| 34 | + self.__mail_server = smtplib.SMTP( |
| 35 | + host=server, port=server_port) |
| 36 | + |
| 37 | + # necessary for internal workings |
| 38 | + self.__is_subject_added = False |
| 39 | + self.__is_body_added = False |
| 40 | + self.__is_attached = False |
| 41 | + self.__is_signature_added = False |
| 42 | + |
| 43 | + def set_subject(self, subject: str): |
| 44 | + """ Method to set subject for the email (optional). |
| 45 | +
|
| 46 | + Parameters: |
| 47 | + subject (str): Email subject to set |
| 48 | + """ |
| 49 | + |
| 50 | + self.__is_subject_added = (subject is not None and subject != '') |
| 51 | + if self.__is_subject_added: |
| 52 | + self.__email_content['Subject'] = subject |
| 53 | + |
| 54 | + def set_body(self, body: str): |
| 55 | + """ Method to set body for the email (optional). |
| 56 | +
|
| 57 | + Parameters: |
| 58 | + body (str): Email body to set |
| 59 | + """ |
| 60 | + |
| 61 | + self.__is_body_added = (body is not None and body != '') |
| 62 | + if self.__is_body_added: |
| 63 | + self.__email_content.attach(MIMEText(body, 'plain')) |
| 64 | + |
| 65 | + def set_signature(self, signature: str): |
| 66 | + """ Method to set signature for the email (optional). |
| 67 | +
|
| 68 | + Parameters: |
| 69 | + signature (str): Email signature to set |
| 70 | + """ |
| 71 | + |
| 72 | + self.__is_signature_added = (signature is not None and signature != '') |
| 73 | + if self.__is_signature_added: |
| 74 | + self.__email_content.attach(MIMEText(signature, 'plain')) |
| 75 | + |
| 76 | + def add_attachment(self, attachment_path: str): |
| 77 | + """ Method to attach attachments in the email (optional). |
| 78 | +
|
| 79 | + Parameters: |
| 80 | + attachment_path (str): Path of attachment |
| 81 | + """ |
| 82 | + |
| 83 | + attachment = MIMEBase('application', "octet-stream") |
| 84 | + |
| 85 | + with open(attachment_path, 'rb') as file: |
| 86 | + attachment.set_payload(file.read()) |
| 87 | + |
| 88 | + encoders.encode_base64(attachment) |
| 89 | + attachment.add_header('Content-Disposition', |
| 90 | + 'attachment; filename="{}"'.format(Path(attachment_path).name)) |
| 91 | + |
| 92 | + self.__email_content.attach(attachment) |
| 93 | + |
| 94 | + # added attachment |
| 95 | + self.__is_attached = True |
| 96 | + |
| 97 | + def send(self, recipient: str) -> bool: |
| 98 | + """ Method to send email message. |
| 99 | +
|
| 100 | + Parameters: |
| 101 | + recipient (str): Recipient's email address |
| 102 | +
|
| 103 | + Returns: |
| 104 | + bool: Determines success of email being sent |
| 105 | + """ |
| 106 | + if self.__is_attached and not self.__is_subject_added: |
| 107 | + print('Error: Subject is empty. Please add a subject and send again.') |
| 108 | + return False |
| 109 | + |
| 110 | + if not self.__is_subject_added and not self.__is_body_added and not self.__is_signature_added: |
| 111 | + print('Error: Cannot send empty email message. Please add at least one from subject, body or signature.') |
| 112 | + return False |
| 113 | + |
| 114 | + self.__email_content['To'] = recipient |
| 115 | + self.__email_content['Date'] = formatdate(localtime=True) |
| 116 | + |
| 117 | + try: |
| 118 | + self.__mail_server.starttls() # start a secure TLS connection |
| 119 | + # login with user credentials on email server |
| 120 | + self.__mail_server.login(self.user_name, self.__password) |
| 121 | + |
| 122 | + # send email message |
| 123 | + self.__mail_server.send_message(self.__email_content) |
| 124 | + |
| 125 | + return True |
| 126 | + except Exception as e: |
| 127 | + print('Something went wrong :(\n', e) |
| 128 | + |
| 129 | + return False |
| 130 | + finally: |
| 131 | + # close connection with email server |
| 132 | + self.__mail_server.quit() |
| 133 | + |
| 134 | + def reset_email(self): |
| 135 | + """ Resets all email content except for the initialisation details. |
| 136 | + """ |
| 137 | + |
| 138 | + # used to add plain texts/HTML parts in the email |
| 139 | + self.__email_content = MIMEMultipart() |
| 140 | + # add sender's info |
| 141 | + self.__email_content['From'] = self.user_name |
| 142 | + |
| 143 | + # necessary for internal workings |
| 144 | + self.__is_subject_added = False |
| 145 | + self.__is_body_added = False |
| 146 | + self.__is_attached = False |
| 147 | + self.__is_signature_added = False |
| 148 | + |
| 149 | + |
| 150 | +# driver code |
| 151 | +if __name__ == "__main__": |
| 152 | + |
| 153 | + # NOTE: if you're using Gmail account for sending email, you may have to turn off one of Google account's setting. |
| 154 | + # link: https://myaccount.google.com/lesssecureapps |
| 155 | + # More about this issue: https://stackoverflow.com/questions/16512592/login-credentials-not-working-with-gmail-smtp |
| 156 | + |
| 157 | + # using Google mail server |
| 158 | + mail_server = 'smtp.gmail.com' |
| 159 | + port_number = 587 |
| 160 | + # sender's credentials |
| 161 | + username = '[email protected]' # enter your gmail address |
| 162 | + with open('password.txt', 'r') as pass_file: |
| 163 | + email_password = pass_file.read() |
| 164 | + # recipient's email address |
| 165 | + email_recipient = '[email protected]' # enter sender's email address |
| 166 | + |
| 167 | + # Email message content |
| 168 | + # Email subject |
| 169 | + email_subject = 'Hello, world!' |
| 170 | + |
| 171 | + # Email body |
| 172 | + email_body = 'Hello there, \n\nThis is my automated email. Please do not reply.' |
| 173 | + |
| 174 | + # Email signature |
| 175 | + email_signature = '\n\nKind regards,\n{}'.format(username) |
| 176 | + |
| 177 | + # using 'EmailClient' class for sending email messages |
| 178 | + email_client = EmailClient( |
| 179 | + mail_server, port_number, username, email_password) |
| 180 | + email_client.set_subject(email_subject) |
| 181 | + email_client.set_body(email_body) |
| 182 | + email_client.set_signature(email_signature) |
| 183 | + # email_client.add_attachment('file_to_attach.txt') |
| 184 | + |
| 185 | + # sending email |
| 186 | + if email_client.send(email_recipient): |
| 187 | + print('Email sent.') |
| 188 | + else: |
| 189 | + print('Failed :(') |
0 commit comments