1

I am trying to set up auto sending emails.

I figured out how to do it via HTTP:

curl -v \
-H "Content-Type: application/json" \
-H "Authorization: <MY_KEY>" \
-POST https://api.sparkpost.com/api/v1/transmissions \
-d '{
        "options":
        {
            "open_tracking": true,
            "click_tracking": true
        },
        "recipients":
        [
            {
                "address":
                {
                    "email": "to@domain.com"
                }
            }
        ],
        "content":
        {
            "from":
            {
                "name": "ME",
                "email": "from@domain.com"
            },
            "subject": "testing",
            "text": "testing"
        }
    }'

But i want to use SMTP protocol, because it's better way for me to generate emails with encoded in base64 files.

For example how i send mail to google account from bash script:

curl \
--ssl-reqd \
--url 'smtps://smtp.gmail.com:465' \
--user "$SENDER:$PASSWORD" \
--mail-from "$SENDER" \
--mail-rcpt "$RECIPIENT" \
-T "mail.txt" -k --anyauth

mail.txt contains:

From: "ME" <from@gmail.com>
To: "YOU" <to@gmail.com>
Subject: Test
Content-Type: multipart/mixed; boundary=MixedBoundary

--MixedBoundary\n
Content-Type: text/html; charset=\"utf-8
<html>
 <body><div>
  <p>Hello from linux terminal</p>
 </div></body>
</html>

--MixedBoundary
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=file.xlsx

...base64symbols...

[base64-encoded text]

--MixedBoundary-

The second option is much more convenient than the first, because i can easily collect any files that i need with bash script, so i want to remake it for sparkpost.

Unfortunately, I did not find a normal description for doing this literally. In the early stages, I ran into an authorization problem. As i understood from smtp api documentation: https://developers.sparkpost.com/api/smtp/ i have to authorize like:

my_email@domain.com:<MY_AUTH_KEY>

So i tried to connect to see output from sparkpost:

curl -v \
--ssl-reqd \
--url "smtps://smtp.sparkpostmail.com:587" \
--user "my_email@domain.com:<MY_AUTH_KEY>"

Output:

*   Trying 52.34.121.1:587...
* TCP_NODELAY set
* Connected to smtp.sparkpostmail.com (52.34.121.1) port 587 (#0)
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* error:1408F10B:SSL routines:ssl3_get_record:wrong version number
* Closing connection 0
curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number

So i have two questions:

How to get auth to sparkpost using SMTP?

Will the second option of sending emails work for sparkpost?

  • To me, it looks like you are trying to use CURL to send SMTP. CURL is only for HTTP and not SMTP. Are you asking how to implement code for an SMTP communication and encode RFC822 all in BASH... I am not saying it cannot be done, but that is not an easy task. There is a reason most folk like RESTful protocols over SMTP If you are just asking how to authenticate with SparkPost SMTP here is what you need to know https://developers.sparkpost.com/api/smtp/ There are "A LOT" of tools that can handle a lot of that for you for Linux CLI so maybe think about using one of them... – Yepher Mar 25 '21 at 14:22
  • 1
    Man says the opposite: curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, **SMTP**, SMTPS, TELNET and TFTP). Why use third-party programs when everything can be done with standard Linux tools, which are installed by default in almost any distribution kit. And it's not that hard. Anyway I found and fixed mistakes i did and I managed to send email with curl, SMTP and MIME, just as i wanted. – vsezanatodazheeto Mar 26 '21 at 14:23
  • Thanks, I learned something new about curl!!! – Yepher Mar 27 '21 at 15:04

1 Answers1

1

I figured out the problem more precisely and found errors. At first i tried to connect like this:

--url "smtps://smtp.sparkpostmail.com:587"

when I had to do it like this :

--url "smtp://smtp.sparkpostmail.com:587"

To get authorization in Sparkpost via SMTP according to the documentation instead of login you should write SMTP_Injection.

The authorization itself looks like this:

--user "SMTP_Injection:<YOUR_COOL_API_KEY>"

In general, everything looks like this:

curl -v \
--ssl --url "smtp://smtp.sparkpostmail.com:587" \
--user "SMTP_Injection:<YOUR_COOL_API_KEY>" \
--mail-from "from@domain.com" \
--mail-rcpt "to@domain.com" \
-T "example_file.txt" -k --anyauth

example_file.txt:

From: Name Surname <from@domain.com>
To: Name Surname <to@domain.com>
Subject: Example
Cc:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="DELIMETER"

--DELIMETER
Content-Type: text/plain; charset=utf-8

Hello, Johny, look at this cat!

--DELIMETER
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=funny_cat.jpg

LOMmL91PLmHcp0/E+eErv5zObgIRo/+vJJN6cto1cDRJ2g

--DELIMETER--