First mail class
Here is a simple example of a mail class in Python:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Mail:
def __init__(self, sender_email, sender_password, recipient_email):
self.sender_email = sender_email
self.sender_password = sender_password
self.recipient_email = recipient_email
def send_email(self, subject, message):
msg = MIMEMultipart()
msg['From'] = self.sender_email
msg['To'] = self.recipient_email
msg['Subject'] = subject
body = message
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(self.sender_email, self.sender_password)
text = msg.as_string()
server.sendmail(self.sender_email, self.recipient_email, text)
server.quit()
# Example usage:
mail = Mail('[email protected]', 'your_password', '[email protected]')
mail.send_email('Test Email', 'This is a test email.')
This class takes the sender's email, sender's password, and recipient's email as parameters in its constructor. The send_email
method takes the subject and message as parameters and sends an email using the provided credentials.
Please note that you need to replace '[email protected]'
, 'your_password'
, and '[email protected]'
with your actual email and password. Also, make sure to allow less secure apps in your Google account settings if you're using Gmail.
This is a very basic example and doesn't include any error handling or advanced features. You may want to add these depending on your specific needs.