# Sending SMS from the command line

There are two broad approaches to sending text messages via the command line: email-SMS gateways and SMS gateway APIs.

### Email <a href="#email" id="email"></a>

Most phone networks provide email-to-SMS gateways.  For example, Verizon will route emails to `<phone number>@vtext.com` as text messages to that phone number.  Not all carriers support this, but most do.

Here are the benefits of the SMS via email approach:

* Easy to use
* Can use common email tools, libraries, and clients

Here are the downsides:

* You must know the carrier associated with the phone number (this is becoming harder as it's much easier to transfer numbers between carriers these days).
* Some carriers append cruft to the message ("Sent via Email at xyz.com" or similar).
* Emails are not handled in a standard way (e.g. some carriers will include the subject, some won't).
* Carriers restrict email to personal messages and very low volume (if you send many emails over time, expect them to start bouncing).
* Your email address is exposed to recipients.

In my opinion, the easiest and most beginner-friendly way to send mail from the command line is `mutt`.  [Mutt](https://web.archive.org/web/20200807183335/http://www.mutt.org/) offers a command-line GUI to help with message sending, but you can also construct command-line one-liners to include in scripts.   In its simplest form, you can do the following:

```
echo "my message" | mutt -s "my subject" abc@gmail.com
```

If you're a purist or you don't want the fanciness of mutt, you might use `ssmtp` to send mail via SMTP mail hub, or if you want to just send raw mail, use `sendmail`.

In all cases, you will probably have to configure your mailer so that it won't be rejected by spam filters.  Doing so is beyond the scope of this article.

[Textbelt Open Source](https://web.archive.org/web/20200807183335/https://github.com/typpo/textbelt) is a free MIT-licensed library that abstracts many of these issues with using email-to-SMS gateways.  However, it still requires your own email setup (it used to be available as a free online service, but it was abused by spammers and taken offline after about 5 years of operation).

Rolling your own email setup can be tricky.  Usually it's best to use something like Mailgun, Sendgrid, etc.  Textbelt uses [Nodemailer](https://web.archive.org/web/20200807183335/https://nodemailer.com/about/) which supports standard SMTP as well as email APIs.

### SMS services <a href="#sms-services" id="sms-services"></a>

The next best way to send SMS from the command line is through text message APIs.  Textbelt offers a paid SMS API in addition to the free open-source offering.  There are many paid offerings in this space and I encourage you to shop around.

However, Textbelt's is probably the simplest API to work with.  It was built with command-line use cases in mind.  A `curl` request to Textbelt is simple:

```
curl -X POST https://textbelt.com/text \
       --data-urlencode phone='5557727420' \
       --data-urlencode message='Hello world' \
       -d key=textbelt
```

It's often useful to save this as an alias.  On most Unix-based systems this can be done in your `~/.bashrc` file:

```
text() {
  curl https://textbelt.com/text --data-urlencode number="$1" --data-urlencode "message=$2" -d key=textbelt
} 
```

For example, you can chain a long-running job to notify you via SMS when it completes (I used to do this when I worked at Google and building [Google Web Server](https://web.archive.org/web/20200807183335/https://en.wikipedia.org/wiki/Google_Web_Server) took forever):

```
./my_long_running_command.sh && text 5551234567
```

## Deciding your approach <a href="#deciding-your-approach" id="deciding-your-approach"></a>

If you're just texting yourself from the command line, it's cheap (free) and relatively easy to use the email-to-SMS gateway of your mobile carrier.  You can send this email either manually or via the [open source project](https://web.archive.org/web/20200807183335/https://github.com/typpo/textbelt).

If you want to skip the headache of maintaining a local email setup, you may use [Textbelt's hosted service](https://web.archive.org/web/20200807183335/https://textbelt.com/) or some other SMS provider.  Textbelt lets you send one SMS per day for free, and you can buy quota if you think you'll be sending more.

Any questions?  Email [support@textbelt.com](https://web.archive.org/web/20200807183335/mailto:support@textbelt.com) about this blog post and I'll get your message.  Happy coding!
