-1

How can I add a subject in it like I did in a normal message? When I am trying to send an email with the code below, it is showing with no Subject:

import smtplib, ssl

email = "fromemailhere"
password = "passwordhere"
receiver = "toemailhere"

message = """
Hello World
"""

port = 465
sslcontext = ssl.create_default_context()
connection = smtplib.SMTP_SSL(
    "smtp.gmail.com",
    port,
    context=sslcontext
)

connection.login(email, password)
connection.sendmail(email, receiver, message)

print("sent")

I have seen this example, but I don't understand how can I do this in my project. Can anyone tell me with a code example?

I am not a Python developer, but I want to use this program.

accdias
  • 5,160
  • 3
  • 19
  • 31
  • 1
    Subject goes in message body. It should be RFCs according formatted string. See https://www.tutorialspoint.com/python/python_sending_email.htm –  Nov 30 '22 at 13:49
  • @SulemanElahi can you give me its format? how to format it according to my code? –  Nov 30 '22 at 13:53
  • 1
    use `MIMEText` class from `emails` library – Bijay Regmi Nov 30 '22 at 13:53
  • 1
    `message = MIMEText(message, "plain")` and `message["Subject"] = "My Email Subject"` then `connection.sendmail(email, receiver, message.as_string())` – Bijay Regmi Nov 30 '22 at 13:54
  • @BijayRegmi, post that as an answer. I guess it will help others that way. – accdias Nov 30 '22 at 13:55
  • @Bijay Regmi its sound very confusing to me i just want emails to send with subject Can you answer like i just copy paste –  Nov 30 '22 at 13:57

1 Answers1

1

It is fairly straight forward. Use email library (documentation). AFAIK it is a standard built in library, so no additional installation required. Your could would look like this:

import smtplib, ssl
from email.mime.text import MIMEText

email = "fromemailhere"
password = "passwordhere"
receiver = "toemailhere"

message = """
Hello World
"""
message = MIMEText(message, "plain")
message["Subject"] = "Hello World"
message["From"] = email

port = 465
sslcontext = ssl.create_default_context()
connection = smtplib.SMTP_SSL(
    "smtp.gmail.com",
    port,
    context=sslcontext
)

connection.login(email, password)
connection.sendmail(email, receiver, message.as_string())

print("sent")
Bijay Regmi
  • 1,187
  • 2
  • 11
  • 25
  • its working fine but any way to not use MIMEText –  Nov 30 '22 at 14:02
  • I am sure you can print dump of `message.as_string()` and use that string and modify it accordigly but I personally advise against that because you have to comply with email RFC standards and it might result in lots of refusals of emails etc. – Bijay Regmi Nov 30 '22 at 14:04
  • your language is very heavy for me If its possible than pls edit your answer –  Nov 30 '22 at 14:06
  • use `print(message.as_string())` to see how your email string looks. Then use this as template for future emails, if you dont want to use MIMEText. But as I said, I do not think this is a good idea. – Bijay Regmi Nov 30 '22 at 14:09
  • i dont want to use MIMEText. pls tell how to do this same thing in my ssl –  Nov 30 '22 at 14:10
  • It seems OP is confusing the e-mail format `MIMEText` with the connection type `SSL`. They are two different beasts, and the answer posted address them both. – accdias Nov 30 '22 at 14:16
  • @accdias WHAT YOU WANT TO SAY? CAN YOU PLS TELL ME HOW CAN I DO THIS SAME THING WITOUT USING MIMEText, cAN you answer me? i can just copy paste –  Nov 30 '22 at 14:20
  • @ecwaecwaecaw, your original question is already addressed by this answer. If you want something different, please post another question. That is the way SO works. And, please, try to not write in all capitals. It makes us feel like you are YELLING at us. :-) – accdias Nov 30 '22 at 14:25
  • @BijayRegmi, you can improve your answer by linking the modules references to the official documentation, like [`email`](https://docs.python.org/3/library/email.examples.html#email-examples). – accdias Nov 30 '22 at 14:33