I am trying to send myself text messages using Python. To do so, I created a gmail account, turned on less secure apps, and ran the following code:
import smtplib
from email.mime.text import MIMEText
# Establish a secure session with gmail's outgoing SMTP server using your gmail account
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login('me@gmail.com', 'pw!' )
msg = '\r\n'.join([
'From: me@gmail.com',
'To: 5551234567@tmomail.net',
'Subject: Cats',
'',
'on wheels'
])
# sendmail(from, to, msg)
server.sendmail('me@gmail.com', '5551234567@tmomail.net', msg)
This does indeed send a text message to my phone, but the message displays like this:
me@gmail.com / Cats / on wheels
Also, the "from" agent in my text message list shows up as a random hyphenated pair of numbers, like "970-2" or "910-2".
However, when I send a message from gmail itself to my phone number, it shows up in my list of text messages as being from "me@gmail.com" and displays like this:
<b>Subject is here</b>
Body of email is here
Is there a way for me to change my msg
object above to make messages sent from Python display like those sent from Gmail itself?
Any suggestions others can offer on this question would be hugely helpful!
To figure this out, I sent an email from Gmail, then clicked on the three dots next to the sent message and clicked "Show Original", which displayed the full packet of data delivered by the Gmail server. Then I just added those fields to my message:
import smtplib
import email.mime.multipart
# Establish a secure session with gmail's outgoing SMTP server using your gmail account
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login('me@gmail.com', pw)
msg = '\r\n'.join([
'MIME-Version: 1.0',
'Date: Fri, 22 Feb 2019 11:29:27 -0500',
'Message-ID: <CAKyky413ikdYD4-Oq_H_FPF-g__weSFehQNLVuspotupWhJaLA@mail.gmail.com>',
'Subject: wow on what',
'From: Ludwig Wittgenstein <me@gmail.com>',
'To: 5551234567@tmomail.net',
'Content-Type: multipart/alternative; boundary="0000000000003c664305827e1862"',
'',
'--0000000000003c664305827e1862',
'Content-Type: text/plain; charset="UTF-8"',
'',
'weeeee',
'',
'--0000000000003c664305827e1862',
'Content-Type: text/html; charset="UTF-8"',
'Content-Transfer-Encoding: quoted-printable',
'',
'<div dir=3D"ltr">here=C2=A0</div>',
''
'--0000000000003c664305827e1862--'
])
# sendmail(from, to, msg)
server.sendmail('me@gmail.com', '5551234567@tmomail.net', msg)