0

Ok, I'm trying to send a email with smtplib with the code which is working! But I don't know how to put a subject in, I've seen some other people here talk about it but the method they used made it so you couldn't put any text in so I'm asking here.

also, the sender is a gmail account and same as the receiver.

import smtplib
import socket

email='xxxxxxxxxx'
password='xxxxxxxxxx'
    
    
def sendMail(to, subject, message):
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        print("Connection Status: Connected")
    except socket.gaierror:
        print("Connection Status: ERROR: Not connected to the internet")
    server.ehlo()
    server.starttls()
    server.login(email, password)
    # subject here or something
    server.sendmail('text', to, message)
    
try:
    sendMail('xxxxxxxx', 'this is the text')
except smtplib.SMTPAuthenticationError:
    print("ERROR: Username or Password are not accepted")
Zacky2613
  • 15
  • 6
  • The below link can help you https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module – Arun Kumar Sep 01 '21 at 07:40

1 Answers1

1
import smtplib
import socket


message = f'Subject: {SUBJECT}\n\n{TEXT}'
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
Arun Kumar
  • 70
  • 8
  • This will work for simple email messages, but you really want to use the `email` library to construct a valid RFC5322 message instead of pasting together random strings, especially if you are not too familiar with the requirements of the RFC. – tripleee Sep 01 '21 at 07:41
  • we have similar questing existing that may help: https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module – Arun Kumar Sep 01 '21 at 07:44