Python send email free

Here are a few ways to send an email for free using Python:

Method 1: Using Gmail's SMTP Server

You can use Gmail's SMTP server to send emails for free. You'll need to allow less secure apps to access your Gmail account and generate an app password.

Here's an example code:

import smtplib
from email.mime.text import MIMEText

# Set up the email server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'your_app_password')

# Set up the email message
msg = MIMEText('Hello, this is a test email!')
msg['Subject'] = 'Test Email'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

# Send the email
server.sendmail('[email protected]', '[email protected]', msg.as_string())
server.quit()

Method 2: Using Sendgrid's Free Plan

Sendgrid offers a free plan that allows you to send up to 100 emails per day. You'll need to sign up for an account and generate an API key.

Here's an example code:

import requests

# Set up the Sendgrid API key
api_key = 'your_sendgrid_api_key'

# Set up the email message
msg = {
    'from': '[email protected]',
    'to': '[email protected]',
    'subject': 'Test Email',
    'text': 'Hello, this is a test email!'
}

# Send the email using the Sendgrid API
response = requests.post(
    'https://api.sendgrid.com/v3/mail/send',
    headers={'Authorization': f'Bearer {api_key}'},
    json=msg
)

if response.status_code == 202:
    print('Email sent successfully!')
else:
    print('Error sending email:', response.text)

Method 3: Using Mailgun's Free Plan

Mailgun offers a free plan that allows you to send up to 10,000 emails per month. You'll need to sign up for an account and generate an API key.

Here's an example code:

import requests

# Set up the Mailgun API key
api_key = 'your_mailgun_api_key'

# Set up the email message
msg = {
    'from': '[email protected]',
    'to': '[email protected]',
    'subject': 'Test Email',
    'text': 'Hello, this is a test email!'
}

# Send the email using the Mailgun API
response = requests.post(
    'https://api.mailgun.net/v3/your_domain.mailgun.org/messages',
    auth=('api', api_key),
    data=msg
)

if response.status_code == 202:
    print('Email sent successfully!')
else:
    print('Error sending email:', response.text)

Remember to replace the placeholders with your own values, such as your email address, password, and recipient's email address.