#🔒 How can I send emails?
10 messages · Page 1 of 1 (latest)
@plucky dew
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.
Can you show the exact line of code, where it says that you don't have any permission to send an email using gmail? 🤔
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 ...
Important: App passwords aren’t recommended and are unnecessary in most cases. To help keep your account secure, use "Sign in with Google" to connect apps to your Google Account. An app password is a
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)
You obviously need an application-specific password in order to send an email using gmail
thank you done
!close
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.