64
votes

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?

3
Can't you send email thru a local (or near to you) SMTP server? - Basile Starynkevitch
Indeed I could, but that was not the question. - NSNolan
It does not surprise me that Google forbids using their SMTP server as spam proxies... - Basile Starynkevitch
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... - NSNolan
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 . - rojo

3 Answers

120
votes
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.

6
votes

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.

1
votes

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