Add mail util to python
To add mail utility to Python, you can use the smtplib
and email
modules, which are part of the Python Standard Library. Here's a basic example of how to send an email using these modules:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(subject, message, from_addr, to_addr, password):
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = subject
body = message
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_addr, password)
text = msg.as_string()
server.sendmail(from_addr, to_addr, text)
server.quit()
subject = "Test Email"
message = "This is a test email sent using Python."
from_addr = "[email protected]"
to_addr = "[email protected]"
password = "your_password"
send_email(subject, message, from_addr, to_addr, password)
In this example, we're using Gmail's SMTP server to send the email. You'll need to replace "[email protected]"
, "[email protected]"
, and "your_password"
with your actual Gmail address, the recipient's email address, and your Gmail password, respectively.
If you're using Gmail, you'll also need to allow less secure apps to access your account. You can do this by going to your Google Account settings and enabling "Less secure app access".
If you're using a different email provider, you'll need to use their SMTP server instead. For example, if you're using Outlook, you would use smtp.office365.com
as the SMTP server.
You can also use a library like yagmail
which is a wrapper around smtplib
and email
and provides a simpler interface for sending emails:
import yagmail
yag = yagmail.SMTP("[email protected]", "your_password")
yag.send("[email protected]", "Test Email", "This is a test email sent using yagmail.")
Remember to install yagmail
using pip:
pip install yagmail