본문 바로가기
  • AI (Artificial Intelligence)
Informatioin/Apple

How To Send Mail From the MacOS Terminal

by 로샤스 2020. 8. 7.

Ref. https://medium.com/better-programming/how-to-send-mail-from-the-macos-terminal-dad1756b166f

Automate email sending — no installation necessary

Have you ever been writing a script and wanted to receive automated emails if the script fails? Or are you new to the command line, and looking for something more exciting to do than just changing directories and listing files?

Well, if you’re working on a Mac, you have everything you need to send an email directly from the command line to the inbox of your choice.


Send Your First Email

Your Mac should already be set up to send email out of the box, so you don’t even need to configure anything. You just need to ensure you’re logged in to a user account, and the Mac will handle sending the email.

To send your first email, open up Terminal and run:

echo "Hello World" | mail -s "Test email" someone@example.com

Obviously, you’ll need to replace someone@example.com with the actual email address you want to send the mail to.

After running the command above, check the account you sent the message to, and your email should have arrived. Make sure you check your spam inbox as well, as it’s likely your email provider will assume the message is unsafe.

Easy as pie

There you have it — that’s all it takes to get started. Now, let’s dive a little deeper into some of the commands, so we can produce more in-depth emails in the terminal.


Ways of Writing the Email Body

There are a number of methods we can use to create the actual body of the email. The length of your message and the way you prefer to use the command line may influence which of the following methods you end up using:

Command-line method

The simplest way of writing the email body is to simply call mail and type it in line by line. When there is no predefined message given to mail — it’ll default to Terminal’s standard input.

You can add as many lines as you like by pressing enter, and when your message is complete, you finish by pressing Ctrl + D (yes, Control, not Command).

The syntax to use this basic version is as follows:

mail someone@example.com

When using this default version, Terminal will prompt you to enter a subject line. It’s important to note that the following methods won’t prompt for a subject line, but one can still be added using an option discussed below.

Echo method

Believe it or not, if you ran the first example command, you’ve already used this method. Scroll back up, and take another look at the command. You can see it starts with echo not mail, and we used a pipe operator, |, to send the message from the first part of the command into the second.

We can echo whatever text we like and then pipe it into the email body using the mail command:

echo "This is not the same message as before" | mail -s "Echo test email" someone@example.com

Cat method

Finally, since we now know we’re able to pipe results into the mail command, we can use the cat command to copy the contents of a file as the message. This could be really useful if you’re wanting to send error logs or something that’s already contained within a file.

To demonstrate, firstly, let’s create a new text file with some content:

echo "This is a test file with a message to email" > file.txt

After that, we’re able to pipe the results of cat into mail to send the file’s contents to our email:

cat file.txt | mail -s "File test email" someone@example.com

Checking the message in our inbox, we can see that the file has successfully been copied into the body of the email.

Works like a charm!


Changing the Subject

An important aspect of any email is the subject line. As mentioned earlier, though, the echo and cat methods don’t prompt you for a subject. For those who’ve been watching closely, you’ll have seen the email examples above all have different subject lines due to the different values we have passed into the -s option.

This option allows us to easily create the subject line for an email, and again, we can change it to be whatever we like using the following syntax:

mail -s "Enter your subject here" someone@example.com


CC and BCC to Other Addresses

Sometimes, you’ll want to include more than one email address when sending mail. Terminal’s mail functionality allows both carbon copies (CC) and blind carbon copies (BCC) to be sent using command-line options.

CC someone in

In order to CC someone, add the -c option followed by a comma-separated list of email addresses, like so:

echo "Hello World" | mail -s "CC Testing email" someone@example.com -c second@example.com, third@example.com

BCC someone in

In order to BCC someone, add the -b option followed by a comma separated list of email addresses, like so:

echo "Hello World" | mail -s "BCC Testing email" someone@example.com -b second@example.com, third@example.com


Conclusion

So there you have it: a simple way to send mail from Terminal without needing to install or configure anything. Obviously, using the command line is not the most effective way to create and send email on a regular basis, and the setup shown here has issues regarding email encryption and validation that’ll often get your messages sent to a spam inbox.

If, however, you want to whitelist the address and send yourself some automated emails from a script on a Mac, this is a simple method to get the messages sent. Likewise, if you just enjoy trying new things on the command line for fun, this is a very easy way to get started using Terminal for more than just listing files and directories.


Resources

Unfortunately, there aren’t many online resources regarding the commands. You can, however, run man mail to see what’s possible.

If you find any other resources or any exciting uses of the commands, please drop them in the comments below for others to see.

'Informatioin > Apple' 카테고리의 다른 글

Code markup in Apple Notes app  (0) 2021.01.26
Mac OS X 메이븐설치  (0) 2019.08.08
HTML email signature in Apple Mail  (0) 2018.05.04

댓글