3

I'm writing a python script to send emails from the terminal. In the mail which I currently send, it is without a subject. How do we add a subject to this email?

My current code:

    import smtplib

    msg = """From: hello@hello.com
    To: hi@hi.com\n
    Here's my message!\nIt is lovely!
    """

    server = smtplib.SMTP_SSL('smtp.example.com', port=465)
    server.set_debuglevel(1)
    server.ehlo
    server.login('examplelogin', 'examplepassword')
    server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
    server.quit()
user248884
  • 851
  • 1
  • 11
  • 21

4 Answers4

7

You need to put the subject in the header of the message.

Example -

import smtplib

msg = """From: hello@hello.com
To: hi@hi.com\n
Subject: <Subject goes here>\n
Here's my message!\nIt is lovely!
"""

server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 1
    That did not work. A slight modification of your response did. The placement of \n made the difference. The right code is: From: hello@hello.com To: hi@hi.com Subject: \n Though, Thank you for making the relevant suggestion. :) – user248884 Jun 30 '15 at 09:44
  • oh ok , yea updated the answer, though ideally spaces newlines should get read correctly in multiline strings – Anand S Kumar Jun 30 '15 at 09:45
  • Would also advice you to accept answers for all your questions, if they helped you satisfactorily, would help the community. – Anand S Kumar Jun 30 '15 at 09:51
5

You can simply use MIMEMultipart()

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase

msg = MIMEMultipart()
msg['From'] = 'EMAIL_USER'
msg['To'] = 'EMAIL_TO_SEND'
msg['Subject'] = 'SUBJECT'

body = 'YOUR TEXT'
msg.attach(MIMEText(body,'plain'))

text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('email','password')
server.sendmail(email_user,email_send,text)
server.quit()

Hope this works!!!

skaul05
  • 2,154
  • 3
  • 16
  • 26
1
import smtp

def send_email(SENDER_EMAIL,PASSWORD,RECEIVER_MAIL,SUBJECT,MESSAGE):
    try:
        server = smtplib.SMTP("smtp.gmail.com",587)
#specify server and port as per your requirement
        server.starttls()
        server.login(SENDER_EMAIL,PASSWORD)
        message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (SENDER_EMAIL, ", ".join(TO), SUBJECT, MESSAGE)
        server.sendmail(SENDER_EMAIL,TO,message)
        server.quit()
        print 'successfully sent the mail'
    except:
        print "failed to send mail"
send_email("sender@gmail.com","Password","receiver@gmail.com","SUBJECT","MESSAGE")
H S Umer farooq
  • 981
  • 1
  • 8
  • 14
0

Indeed, the subject you missed. Those things can easily be avoided by using some API (such as yagmail) rather than the headers style.

I believe yagmail (disclaimer: I'm the maintainer) can be of great help to you, since it provides an easy API.

import yagmail
yag = yagmail.SMTP('hello@gmail.com', 'yourpassword')
yag.send(to = 'hi@hi.com', subject ='Your subject', contents = 'Some message text')

That's only three lines indeed.

Install with pip install yagmail or pip3 install yagmail for Python 3.

More info at github.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • @user248884 What is the domain you will use? You can use `yagmail.SMTP('hello@domain.com', 'password', host = "YOUR HOST", port = "YOUR PORT")` – PascalVKooten Jun 30 '15 at 12:10