1

So I am sending an automated text to my number at mynumber@tmomail.net and I am trying to send just a body as apposed to an email and it doesn't work but also doesn't throw an error. My code is below and it works when I add everything but doesn't work when only adding just the body (got from here).

import smtplib
email = "myemail@outlook.com"
pwd = "my_password"
phone_num = "my_number@tmomail.net"
server = smtplib.SMTP('smtp.office365.com',587)

server.starttls()
server.login(email, pwd)

body = "This working?"
server.sendmail(email, phone_num, body)
server.quit

This doesn't seem to work but if I add more to it and give it multipart containing from, to and subject it works just fine.

Hercislife
  • 159
  • 2
  • 10
  • This seems just to be a typo. You wrote `server.sendemail(email, phone_num, body)` but the function is just `server.sendmail` (mail not email). You said it doesn't throw an error though. This should through a attribute error so maybe something else is going on. – Zev Jul 28 '21 at 22:25
  • 1
    @Zev You are right, that is definitely a typo. I double checked my script and they both say `sendmail`. Muscle memory just takes over every time I type that and goes straight to `sendemail` hence the mistake. – Hercislife Jul 30 '21 at 14:58

1 Answers1

2

All you need to do is add a newline:

body = "\nThis working?"

The server.sendmail is a pretty low-level function that requires you to do the work to format the message correctly (and as expected by your SMTP server). At first, I tried modifying your code as follows and received the message successfully:

body = (f"From: {email}\r\nTo: {phone_num}\r\n\r\n")
body += "This working?"

server.sendmail(email, phone_num, msg=body)
server.quit()

The code above provides the headers as described here: https://docs.python.org/3/library/smtplib.html#smtp-example (I just converted their example to f-strings)

I think you are looking to use a bit higher level function where you don't have to worry about doing the headers yourself.

You may want to look into using smpt.send_message() https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.send_message which helps you construct the correct headers.

I see that you were looking to avoid adding the "from" and "to" as per the example you were looking at. It will work with just that new line as detailed in this related question: How to send SMTP email for office365 with python using tls/ssl

Zev
  • 3,423
  • 1
  • 20
  • 41
  • I was doing this but wanted to get rid of the header and subject line. In the link I put in my questions seems to state you can add just the body which is where my question kinda stems from. Did that use to be a function but is now no longer the case? The answer seems to have a few upvotes so I assumed it was a viable option. Thank you for `send_message()`, I think basically T-Mobile formats the messages a certain way and there is no way for me to get around that. – Hercislife Jul 30 '21 at 16:17
  • I think the headers (and from and to) are part of the protocol of smtp so I don't think you can avoid that. – Zev Jul 30 '21 at 16:32
  • I do see where you are coming from trying to follow that example. When I tried what you had originally, I just got an empty message (on verizon) so that example didn't work for me either. – Zev Jul 30 '21 at 16:35
  • I guess the other difference may be gmail and office365's handling of smtp messages. – Zev Jul 30 '21 at 16:40
  • 1
    Actually, this may better answer your question: https://stackoverflow.com/a/50086719/1830793 – Zev Jul 30 '21 at 16:43
  • Thanks for the link but unfortunately it still formats the same way but it does allow me to just have a body, so there's that. I appreciate the help with this! Though I do think this is probably good enough. – Hercislife Jul 30 '21 at 17:07