#🔒 How can I send emails?

10 messages · Page 1 of 1 (latest)

plucky dew
#

Before when I tried to use smtplib, it kept telling me that I didn't have permission to send an email. I use gmail.

timid radishBOT
#

@plucky dew

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rocky hedge
plucky dew
# rocky hedge Can you show the exact line of code, where it says that you don't have any permi...

Failed to send email: (534, b'5.7.9 Application-specific password required. For more information, go to\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor ...

#
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_email(sender_email, sender_password, recipient_email, subject, body):
    try:
        # Set up the email
        msg = MIMEMultipart()
        msg['From'] = sender_email
        msg['To'] = recipient_email
        msg['Subject'] = subject

        # Attach the email body
        msg.attach(MIMEText(body, 'plain'))

        # Connect to the SMTP server
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()  # Start TLS encryption

        # Log in to your email account
        server.login(sender_email, sender_password)

        # Send the email
        server.sendmail(sender_email, recipient_email, msg.as_string())
        print("Email sent successfully!")

        # Quit the server
        server.quit()
    except Exception as e:
        print(f"Failed to send email: {e}")

# Example usage
if __name__ == "__main__":
    sender_email = input("Enter your email: ")
    sender_password = input("Enter your password: ")
    recipient_email = input("Enter the recipient's email: ")
    subject = input("Enter the subject: ")
    body = input("Enter the email body: ")

    send_email(sender_email, sender_password, recipient_email, subject, body)
rocky hedge
timid radishBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.