Using curl to send email

LinuxEmailCurlCommand LineGmail

Linux Problem Overview


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 "<sender@gmail.com>" --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?

Linux Solutions


Solution 1 - Linux

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" <username@gmail.com>
To: "John Smith" <john@example.com>
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.

Solution 2 - Linux

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" <username@gmail.com>
To: "John Smith" <john@example.com>
Cc: "Mary Smith" <mary@example.com>
Subject: This is a test

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

Solution 3 - Linux

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

Solution 4 - Linux

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" <mymail@gmx.at>
To: "John Smith" <mymail@gmx.at>
Subject: This is a test

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNSNolanView Question on Stackoverflow
Solution 1 - LinuxbambamView Answer on Stackoverflow
Solution 2 - LinuxsoloturnView Answer on Stackoverflow
Solution 3 - LinuxShakiba MoshiriView Answer on Stackoverflow
Solution 4 - LinuxEl GiganteView Answer on Stackoverflow