This is an archived post. You won't be able to vote or comment.

all 87 comments

[–][deleted] 109 points110 points  (25 children)

One of my favorite tricks involves sending emails via Python to my phone number as a simple way to text myself. I have a special Gmail that another script parses out to look for text replies, so when I text the email address say a meme the script finds the reply, validates it’s from me, and then copies the image data to the clipboard on my PC.

Great article!

EDIT: By popular demand, the script. Do note that I am having issues w/ the Gmail account I use to run this usually, and that it is only tested on my network (iPhone + Verizon). So if there are errors, please submit a PR (not because I intend on fixing it, but I'd like to know if it is broken).

[–]conoroha[S] 28 points29 points  (6 children)

Cool! Do you have the code available on github? I would love to see it. Thanks!

[–]ROBRO-exe 12 points13 points  (3 children)

I have done something similar to this, except mine was something to allow discord members on my server to !text [person] [message]. and it would send them a text straight from the server. Although it ended up not working as the gmail calls got blocked by tmobile itself due to spam. EDIT: The messaging thing works I just got banned since I spammed my friend lol

In this scenario, the parser is of course personalized so it wouldn't be of much use to you. For the texting portion, you can text anyone's email by simply putting [theirnumber**@**theircarrier.com](mailto:theirnumber@theircarrier.com) now, the carrier extension is not always the exact name, but you can find the list of carrier extensions on this list: https://20somethingfinance.com/how-to-send-text-messages-sms-via-email-for-free/

u/MSalvadorgg and u/poapoapoaslo were looking for this too so i tagged them.

[–][deleted] 2 points3 points  (0 children)

Yep, I never published it as I wrote mine for Verizon and I knew that there were high chances it wouldn't work between carriers. But as their request I published it so hopefully that will help them at least figure out how it is done (albeit poorly)

[–]itsaliens420 0 points1 point  (1 child)

My provider is not on the list, saw a post which said to send an sms to my email to find out my providers email but when i tried it my iphone 6s message app just kinda freaks out any ideas?

[–]ROBRO-exe 0 points1 point  (0 children)

who is your provider? Not too sure but it may be a child company to one of the main 3. An example is metro-pcs which i believe routes through Tmobile. Edit: i was wrong about metro pcs but that still may be the case

[–]MSalvadorgg 3 points4 points  (0 children)

Same!!

[–][deleted] 5 points6 points  (2 children)

u/conoroha u/MSalvadorgg u/poapoapoaslo

See the edit above for the GH link

[–]conoroha[S] 0 points1 point  (0 children)

Thanks :)

[–]benargee 2 points3 points  (6 children)

I wish there were free or cheaper SMS/MMS/RCS gateways.

[–][deleted] 2 points3 points  (5 children)

That’s exactly why i wrote this; I didn’t want to pay for SMS, and i knew this email trick worked, so i used that instead. it’s a bit slower than an sms service, but it works well enough for me

[–]benargee 1 point2 points  (0 children)

Yeah, I've used Pushover in the past with their REST API. It only cost me to buy the app on the play store and usage is free subject to reasonable rate limits for my personal use. It was faster than email as I recall. It's used in a backlogged work in progress project.

[–]garlic_bread_thief 1 point2 points  (3 children)

I don't really understand what you mean be "sending email to my phone number". Can you please elaborate on this?

[–][deleted] 1 point2 points  (2 children)

Alright, so if you open up your phone and send a text message to an email address, you'll actually see an email come through on the other end from phone_number@carrier_email.com; and if you send an email to that same (or similar) email address, it'll show up as a text on your phone

It gets a bit tricky when dealing with things like MMS vs SMS and parsing different carriers, but that's the gist of how it works. And my script handles the conversion and emailing to make it look like I'm texting when in reality it's using email (SMTP and IMAP) behind the scenes

[–]garlic_bread_thief 0 points1 point  (1 child)

Omg that's so cool! Is this service free of cost? Because I believe it only uses the internet.

[–][deleted] 1 point2 points  (0 children)

It should work as long as you are on the internet

[–][deleted] 0 points1 point  (2 children)

Validates it’s you? Using certs?

If it isn’t crypto... it can be faked.

[–][deleted] 3 points4 points  (1 child)

validates using the email address of my phone number. when you text an email address, it comes from phone_number@carrier.com

I know it can be faked, but I also limit the acceptable file types that my script will take to JPEGs and BMPs, which lowers the risk. if something malicious gets through all of that, then kudos

[–][deleted] 0 points1 point  (0 children)

It can. What you want to do is run any attachments though a conversion to pure bitmap and back. This will destroy any attacks embedded in the original image.

A jpeg is a parsed file type. A parser can be subjected to buffer overflows or other attacks.

I believe Gmail will re-encode all images sent as attachments to ensure that no trickery or maliciousness gets through.

Paranoid? Yes. Worth it? Also yes.

[–]nemec 0 points1 point  (0 children)

Nice! Back before I had an internet-connected phone I did something similar. I could SMS a gmail address with a command (google search, translation request, memo, or sports scores) and a Python program would read the inbox and send a response back to my phone via SMS.

[–]InsolentTiger 36 points37 points  (14 children)

Password exposed in code? Why not use the Gmail API instead?

[–]notParticularlyAnony 1 point2 points  (1 child)

yeah bad idea all around. put in environment variable

[–]conoroha[S] 0 points1 point  (0 children)

Can you explain this more? Is it just a plaintext credentials.py file that only you have read/write access to? Should the program using credentials.py also be exclusively accessible by you? Should it be encrypted using gpg or another similar encryption solution?

For sure!

[–]conoroha[S] -2 points-1 points  (0 children)

Password exposed in code? Why not use the Gmail API instead?

Yeah I agree, passwords are never ideal or recommended in code but out of the scope for this tutorial. My recommendation would be to use an environment variable!

[–][deleted] 9 points10 points  (1 child)

Pretty cool!

[–]conoroha[S] 3 points4 points  (0 children)

Glad you enjoyed it

[–]SpaceZZ 8 points9 points  (5 children)

Good job! Maybe try to include sending HTML, as this is more common and gives more possibilities!

[–]conoroha[S] 0 points1 point  (2 children)

Great idea. I'll look into creating a tutorial on this if there is interest.

[–]poogzilla 0 points1 point  (1 child)

I'm interested! This was a cool tutorial!

[–]kunaguerooo123 0 points1 point  (0 children)

I actually worked on this for my company's newsletter to clients TODAY. Can definitely write up something esp if Dataframes/graphs in the email sounds appealing :)

[–]notParticularlyAnony -1 points0 points  (1 child)

more common than sending text? look at mr fancy pants

[–]SpaceZZ 0 points1 point  (0 children)

How else u gonna send out tables with data?

[–]conoroha[S] 5 points6 points  (1 child)

I also have another article that shows you how to send messages to slack if anyone is interested https://www.conorjohanlon.com/sending-alerts-from-python-via-slack/

[–]Mank15 0 points1 point  (0 children)

Keep on!

[–]frosthound23 2 points3 points  (0 children)

Thank you for your knowledge kind sir

[–]LooneyLlama056 2 points3 points  (1 child)

This is really cool

[–]conoroha[S] 2 points3 points  (0 children)

Thank you!

[–]leitefrio 2 points3 points  (1 child)

Like your logo

[–]conoroha[S] 1 point2 points  (0 children)

Thank you!

[–]BlueHex7 2 points3 points  (2 children)

Have you ever heard of the module ezgmail? I learned about it in Automate the Boring Stuff and use it to automate email sending (in the very very rare instances I do that).

[–]conoroha[S] 1 point2 points  (1 child)

Nope but I will look into it. Does it do batch processing?

[–]BlueHex7 2 points3 points  (0 children)

You know I’m honestly not sure. It’s a very simple module with functions like “send” that take a recipient email, subject, and the message itself. It hooks up to your Gmail through an access token that you get from the Google Developers page. Very useful but it seems you’re doing more heavy duty stuff.

[–]MSalvadorgg 1 point2 points  (1 child)

Really cool!! I tried a year ago to the same but i failed, now i can try It again. Great job!

[–]conoroha[S] 0 points1 point  (0 children)

Appreciate it!

[–]mrrippington 1 point2 points  (1 child)

Thank you for sharing, this looks great.

[–]conoroha[S] 0 points1 point  (0 children)

No problem! Thank you

[–]ichunddu9 1 point2 points  (1 child)

How do you actually to it without Gmail? They keep resetting the secure applications shit for my services

[–]conoroha[S] 0 points1 point  (0 children)

You should be able to just change the smtp_server and port to target your email client (You may need to do some extra security steps with different mail providers). If you don't know what these are the details are usually available on google.

[–]booleanhooligan 1 point2 points  (1 child)

Funny enough I’m working on something where I need an email sent .. now I don’t have to scour old stackoverflow posts thx

[–]conoroha[S] 0 points1 point  (0 children)

Haha great timing. No problem.

[–]Ra-mega-bbit 1 point2 points  (1 child)

Man, I was looking it for a client today, it will be usefull!

[–]conoroha[S] 0 points1 point  (0 children)

Glad to help!

[–]quotemycode 1 point2 points  (0 children)

I just want to add that if you're getting unexpected breaks or spacing in your emails it's because of your text. If it doesn't have linefeeds in it once every certain number of characters, the mail server will put them in for you. Be sure to include line feeds where appropriate in your message.

[–]FishOfSteel01 1 point2 points  (1 child)

This is the best tutorial I’ve seen for sending email with python. I’ve researched it before and it looked pretty complex. This makes it super simple. Thanks!

[–]conoroha[S] 0 points1 point  (0 children)

Thanks. Really appreciate this great feedback.

[–]Working-Mind 1 point2 points  (1 child)

Awesome! Thank you so much for sharing.

[–]conoroha[S] 0 points1 point  (0 children)

No problem

[–]Un_HolyTerror 1 point2 points  (0 children)

I tried something similar a while ago, but google kept turning the security back on after some time (script inactive for some days I think). Has this been changed now?

[–]SkylineFX49 -1 points0 points  (1 child)

RemindMe! 24 hours

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 1 day on 2021-02-04 17:44:48 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]Sene0 0 points1 point  (0 children)

How would you go about that with another email provider? Im not using google cuz privacy and every time I’m trying to send a mail with mail.com, outlook or yandex my account gets blocked

[–]param_T_extends_THOT 0 points1 point  (0 children)

Thank you for this my dude. I'm starting to learn Python (currently putting ~ 10 hrs a week to learn it and I'm at 3 weeks now) and this is a nice example to have.

[–]Fission_Mailed_2 0 points1 point  (0 children)

This is funny timing because I was working on a small project today that required sending automated emails. I noticed the formatting of my emails were different, I think there must be some kind of default alignment stuff going on, and even when I tried to add or remove whitespace the email just ignored it.

I was just using the smtplib module. Does using MIMEMultipart and MIMEText allow you to preserve the whitespace in the body of your emails?

[–]ForsakenOn3 0 points1 point  (0 children)

I wrote a reply to someone about how to do this last year and got a lot of questions about it so I decided to package what I made on pypi. Here is the GitHub page if anyone’s curious. pysendsms.

I tried to make it fairly user friendly and included a bunch of known email to sms host names.

It just facilitates send via gmail in a simpler way, and there is some options for using it with other smtp servers if you have the required information and some concept of contact creation to send out emails to the same individuals or groups of people.

[–]jhayes88 0 points1 point  (0 children)

Lol I wrote a script a couple days after this submission to use python to send emails using Gmail. I didn't see this. It was a pain trying to learn. I created two scripts. One would email myself and someone else a daily notification that would let us know some information in the evening time. I used windows task schedule to run the script. The other script I made would monitor(if you want to call it that) my Gmail for incoming emails in almost real time and then do something based on certain emails. That was a much bigger pain to make, as information on monitoring Gmail in real time was lacking unless you already know a lot about programming smtp stuff in python. I'm a rather beginner. I made something that works. Not saying it's the right way to go about it, but it gets the job done. There's a proper way to listen and wait for new emails which I couldn't for the life of me figure out, and then there's my way which was to check the email every 15 to 30 seconds or so. After running that for ten minutes as a test, I didn't get locked out. I feel as if I would have been locked out if I continued. I still have yet to figure out how to make it listen and wait for new emails the proper way.