Send email from python
In Python, you can send an email using the smtplib
library, which is a built-in library in Python. Here's a simple example of how to send an email:
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 from 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.
Please note that you need to allow less secure apps to access your Gmail account. You can do this by going to your Google Account settings and enabling "Less secure app access".
Also, be aware that storing your password in plain text is not secure. In a real-world application, you should consider using a more secure method to store and retrieve your password.
If you're using a different email provider, you'll need to use their SMTP server instead of smtp.gmail.com
. You can find the SMTP server information in your email provider's documentation.
Here's a list of some common SMTP servers:
- Gmail:
smtp.gmail.com
- Outlook:
smtp.office365.com
- Yahoo:
smtp.mail.yahoo.com
- AOL:
smtp.aol.com
- Hotmail:
smtp.live.com
Remember to replace the from_addr
and password
with your actual email address and password.