import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender = '[email protected]'
password = 'mypassword'
smtp_server = 'smtp.gmail.com'
smtp_port = 587
def send_notification(message):
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender, password)
except Exception as e:
print(f'Login failed: {e}')
return
print('Connected. Creating email...')
email = MIMEMultipart()
email['From'] = sender
email['To'] = '[email protected]'
email['Subject'] = 'Test Email'
email.attach(MIMEText(message, 'plain'))
server.send_message(email)
print('Email sent successfully')
server.quit()
send_notification('Hey there! This is a test.')
I’m having a weird problem with my Python script for sending emails between two Gmail accounts I own. The script runs without errors and the email shows up in the sender’s Sent folder. But the recipient’s inbox (and spam folder) stays empty. Has anyone run into this before? Any ideas on how to fix it? I’m scratching my head here!
I’ve dealt with this frustrating issue before. One thing that helped me was adding a delay between sending emails. Gmail can flag rapid-fire messages as potential spam. Try adding a time.sleep(10) after each send_message() call if you’re sending multiple emails.
Also, check your sender account’s outbound filters. Sometimes overzealous filters can silently block outgoing messages. If all else fails, consider using a transactional email service like SendGrid or Mailgun. They have better deliverability rates and can help diagnose issues.
Lastly, make sure your IP isn’t blacklisted. You can check this using online tools. If it is, you might need to use a different network or VPN to send emails temporarily.
I’ve encountered this issue before and believe it is often due to Gmail’s security settings rather than a fault in the script itself. Although the email appears in the sender’s Sent folder, Gmail may be classifying the message as suspicious. One way to address this is to enable less secure app access in the sender’s account or switch to using OAuth2 for authentication. It is also wise to verify that the recipient’s address is entered correctly and try an alternative SMTP port, or even consider using Gmail’s API for a more reliable connection.
hey, ive had this problem before. check ur spam folder first. if not there, its probably gmail’s security blocking it. try using an app password instead of ur regular one. also, double-check the recipient email. sometimes its just a typo lol. good luck!