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?
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--