Graph send mail

Here is a simple example of how to send an email using Python's smtplib library:

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)

This script will send an email from from_addr to to_addr with the subject subject and the message message. The password for the email account is password.

Please note that you need to replace "[email protected]", "[email protected]", and "your_password" with your actual Gmail address, the recipient's email address, and your Gmail password.

Also, please note that this script will only work if you 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". However, this is not recommended as it can compromise your account security.

If you want to send emails using a more secure method, you can use OAuth 2.0 to authenticate with Gmail. Here is an example of how to do this:

import base64
import email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://mail.google.com/']

def send_email(subject, message, from_addr, to_addr):
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gmail', 'v1', credentials=creds)

    message = MIMEMultipart()
    message['From'] = from_addr
    message['To'] = to_addr
    message['Subject'] = subject

    body = message
    message.attach(MIMEText(body, 'plain'))

    raw_message = base64.urlsafe_b64encode(message.as_string().encode('utf-8'))
    raw_message = raw_message.decode('utf-8')

    message = service.users().messages().send(userId='me', body={'raw': raw_message}).execute()
    print('Message ID: %s' % message['id'])

subject = "Test Email"
message = "This is a test email sent using Python."
from_addr = "[email protected]"
to_addr = "[email protected]"

send_email(subject, message, from_addr, to_addr)

This script will send an email from from_addr to to_addr with the subject subject and the message message. The password for the email account is not needed as the script uses OAuth 2.0 to authenticate with Gmail.

Please note that you need to replace "[email protected]" and "[email protected]" with your actual Gmail address and the recipient's email address.

Also, please note that you need to create a file named credentials.json in the same directory as the script and fill it with your OAuth 2.0 credentials. You can get these credentials by going to the Google Cloud Console and creating a new project, then enabling the Gmail API and creating credentials for your project.