81

How can I use the curl command line program to send an email from a gmail account?

I have tried the following:

curl -n --ssl-reqd --mail-from "<[email protected]>" --mail-rcpt "<[email protected]>" --url smtps://smtp.gmail.com:465 -T file.txt

With file.txt being the email's contents, however, when I run this command I get the following error:

curl: (67) Access denied: 530

Is it possible to send an email from an account that is hosted by a personal server, still using curl? Does that make the authentication process easier?

5
  • Can't you send email thru a local (or near to you) SMTP server? Commented Feb 6, 2013 at 7:23
  • 9
    Indeed I could, but that was not the question. Commented Feb 6, 2013 at 7:27
  • It does not surprise me that Google forbids using their SMTP server as spam proxies... Commented Feb 6, 2013 at 7:29
  • I think it is possible I just don't think I have my syntax correct. I have attempted slight variations of what I posted and have gotten different feedback such as prompting me for a password, but the email still fails... Commented Feb 6, 2013 at 7:33
  • I believe Gmail will require you to use Oauth for authentication. This won't be easy with curl. You can see Google's Oauth documentation at developers.google.com/google-apps/gmail/xoauth2_protocol . Commented Feb 6, 2013 at 14:52

5 Answers 5

149
curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user '[email protected]:password' \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --upload-file mail.txt

mail.txt file contents:

From: "User Name" <[email protected]>
To: "John Smith" <[email protected]>
Subject: This is a test

Hi John,
I’m sending this mail with curl thru my gmail account.
Bye!

Additional info:

  1. I’m using curl version 7.21.6 with SSL support.

  2. You don't need to use the --insecure switch, which prevents curl from performing SSL connection verification. See this online resource for further details.

  3. It’s considered a bad security practice to pass account credentials thru command line arguments. Use --netrc-file. See the documentation.

  4. You must turn on access for less secure apps or the newer App passwords.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks man. That totally did the trick. Any recommendations for doing this more securely. I want to have a cron job on my server run a script that will send out emails at a specific time. I figured a curl command would be the easiest but not the most secure. I am not very familiar with smtp servers and such, so any advice would be much appreciated.
If I'd want to use something like that to send automated mails from a bash script while avoiding password in the command line ? I've noticed that if you omit the password part you get prompted so I could use expect to deal with that but is there a simpler approach to have curl read from a redirect ? or maybe even an environment variable ?
oh ... I get it ... yo can put credentials in .netrc
The command is successfully executed but the email arrives empty, no content.
@louigi600, I wouldn't use a .netrc file. However, you could use the --netrc-file parameter, and then use bash subshell redirection. E.g. curl --netrc-file <(printf '%s' "${MY_CREDENTIALS}" ... bash will create a file descriptor to feed the credential data in, and (I think) the data won't even touch disk.
|
11

if one wants to send mails as carbon copy or blind carbon copy:

curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
  --mail-from '[email protected]' --mail-rcpt '[email protected]' \
  --mail-rcpt '[email protected]' --mail-rcpt '[email protected]' \
  --upload-file mail.txt --user '[email protected]:password' --insecure
From: "User Name" <[email protected]>
To: "John Smith" <[email protected]>
Cc: "Mary Smith" <[email protected]>
Subject: This is a test

a BCC recipient eli is not specified in the data, just in the RCPT list.

2 Comments

No, that's incorrect, or at least dubious; there is no need for the email message to contain an explicit Bcc: header. As far as SMTP is concerned, the message headers are just data, and the actual recipients are specified by other means (in this case, by specifying --mail-rcpt multiple times if you want to send to multiple recipients).
yes. it was incorrect thanks for pointing out! i am correcting the answer. see also here: stackoverflow.com/questions/2750211/….
4

Crate a simple email.conf file like so

Username:   [email protected]
Password:   OKbNGRcjiV
POP/IMAP Server:    mail.example.com

And simply run sendmail.sh, like so after making it executable (sudo chmod +x sendmail.sh)

./sendmail.sh

Code

#!/bin/bash

ARGS=$(xargs echo  $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' email.conf) < /dev/null)
set -- $ARGS "$@";  

declare -A email;
email['user']=$1
email['pass']=$2
email['smtp']=$3
email['port']='587';
email['rcpt']='[email protected]';


email_content='From: "The title" <'"${email['user']}"'>
To: "Gmail" <'"${email['rcpt']}"'>
Subject: from '"${email['user']}"' to Gmail
Date: '"$(date)"'

Hi Gmail,
'"${email['user']}"' is sending email to you and it should work.
Regards
';


echo "$email_content" | curl -s \
    --url "smtp://${email['smtp']}:${email['port']}" \
    --user "${email['user']}:${email['pass']}" \
    --mail-from "${email['user']}" \
    --mail-rcpt "${email['rcpt']}" \
    --upload-file - # email.txt


if [[ $? == 0 ]]; then
    echo;
    echo 'okay';
else
    echo "curl error code $?";
    man curl | grep "^ \+$? \+"
fi

more

2 Comments

This works fine, I'm on Ubuntu 16 and need to attach a file. How to do this ?
@cwhisperer Did you try this one https://stackoverflow.com/a/44728856/4643584
3

Mind that the form of mail.txt seems to be important / CRLF for win, LF for Linux, special characters etc.

Finally after struggling 2 hours, it works for me for GMX (they tell their SMPT port to be 587 - and further down in small letters the hint: "also 465 can be used with SSL"):

UNDER Linux (TinyCore Linux on Raspberry 3B+ with curl.tcz installed):

curl --ssl-reqd --url 'smtps://mail.gmx.net:465' --user '[email protected]:mymailPassword' --mail-from '[email protected]' --mail-rcpt '[email protected]' --upload-file mail.txt

UNDER Windows:

curl --ssl-reqd --url "smtps://mail.gmx.net:465" --user "[email protected]:mymailPassword" --mail-from "[email protected]" --mail-rcpt "[email protected]" --upload-file mail_win.txt

with mail.txt:

From: "User Name" <[email protected]>
To: "John Smith" <[email protected]>
Subject: This is a test

Hi John,
Im sending this mail with curl thru my gmx account.
Bye!

2 Comments

This works fine, but how to attach a file ?
instead of --upload-file mail_win.txt you can also use -T <(echo -e From: "User Name" <[email protected]>\r\nTo: "John Smith" <[email protected]>\r\nSubject: This is a test\r\n\r\nHi John,\r\nIm sending this mail with curl thru my gmx account.\r\nBye!") Do note that the CRLF or LF depends on the mail server, not the system that executes curl
1

Note that if Perl's "system()" function is used to execute the curl command, each argument 'word' is a separate item in the argument array, and words must NOT be quoted.

Also note that if sending via Gmail after May 30, 2022, the gmail account must be set up with 2-factor authentication and then you must create an "App Password". The App Password is a long character string that acts as an alternative password and replaces the usual password on the "--user" parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.