Question:
How you can send email from a shared inbox in Python

With Python, you can use the smtplib library to send emails via the Simple Mail Transfer Protocol (SMTP) from a shared inbox. You'll need the shared inbox's authentication credentials and SMTP server information to send an email from it. This sample of code shows how to send an email using a shared inbox:

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText


# SMTP server details

smtp_server = 'your_smtp_server.com'

smtp_port = 587  # 587 is the default port for TLS

smtp_username = 'your_shared_inbox@example.com'

smtp_password = 'your_password'


# Sender and recipient email addresses

sender_email = 'your_shared_inbox@example.com'

recipient_email = 'recipient@example.com'


# Create the email message

message = MIMEMultipart()

message['From'] = sender_email

message['To'] = recipient_email

message['Subject'] = 'Subject of the Email'


# Email body

email_body = """

Hello,


This is the body of your email.


Best regards,

Your Name

"""

message.attach(MIMEText(email_body, 'plain'))


# Establish a connection to the SMTP server

try:

    server = smtplib.SMTP(smtp_server, smtp_port)

    server.starttls()  # Upgrade the connection to secure TLS

    server.login(smtp_username, smtp_password)


    # Send the email

    server.sendmail(sender_email, recipient_email, message.as_string())

    print("Email sent successfully!")


except Exception as e:

    print(f"Error: {str(e)}")


finally:

    # Close the SMTP server connection

    server.quit()


Make sure to enter your shared inbox email address, credentials, and specific SMTP server information in the placeholders. Using the shared inbox's SMTP server, this code generates a straightforward plain-text email message and delivers it to the designated recipient. The email's formatting and content can be changed as necessary. To improve security, think about storing sensitive data, such as SMTP credentials, in a secure configuration file or using environment variables.


Suggested blog:

>How to solve XGBoost model training fails in Python

>Python Error Solved: load_associated_files do not load a txt file

>What are common syntax errors and exceptions in Python


Ritu Singh

Ritu Singh

Submit
0 Answers