Raspberry Pi

How to Send Mail Through Curl Command on Raspberry Pi

The curl command is one of the most useful commands for Linux users that is widely used for downloading files from the server. But can you use the curl command to send an email from the terminal? It is helpful for scripting and automating tasks and it requires email providers, such as SMTP or IMAP, followed by the email body and user credentials.

If you are searching for an easy way to send an email through the curl command from the Raspberry Pi terminal, follow this article’s guidelines.

Send Mail Through curl Command on Raspberry Pi

To send a mail through curl command on the Raspberry Pi terminal, follow the below-given steps:

Step 1: (Optional) First, you must allow the two-step verification from your email account. In my case, I am using a Gmail account for sending mail. So, if you are using the same Gmail provider, you can visit here and enable two-step verification.

Step 2: Then visit here and select the app and device to generate the password for. Go with the “Mail” option here.

Step 3: Now select the device and since we are using the Raspberry Pi device, you can go with the “Other” option. It’s not mandatory what option you pick.

Step 4: Add your device name and select the “Generate” option.

Save the password on the screen for later use.

Note: I am sending a file with the name “email.txt” here and inside the file, there is a message “Hello Linux Author”. You can send any file you want by adding text inside the file.

Step 5: Open the Raspberry Pi terminal and use the following syntax for sending mail through curl command.

curl --ssl-reqd \

  --url 'smtps://smtp.gmail.com:465' \
  --user 'yourmail@gmail.com:password generated through Step 4' \
  --mail-from 'yourmail@gmail.com' \
  --mail-rcpt 'receiver_mail@gmail.com' \
  --upload-file filename.txt

In the above syntax, the user has to add his/her email address in place of “yourmail@gmail.com” , while the password should be generated from the same email using Step 4. The receiver_mail could be any person’s mail to whom you want to send, while the file name should be the one, which includes some text.

After the above command execution, the mail will be sent to the receiver.

In this way, you can send any email you want directly from the terminal using the curl command.

Conclusion

Sending email through the curl command is an ideal way to send the email from the terminal within seconds and it’s just a matter of one single command. However, before that, the users must generate the password from the email account, which will be required when using the curl command. Without this password, the user won’t be able to email the receiver.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.