我正在尝试设置自动发送电子邮件。
我想好了如何通过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"
}
}'
但我想使用SMTP协议,因为这对我来说是用base64编码文件生成电子邮件的更好方式。
例如,我如何从bash脚本向谷歌帐户发送邮件:
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包含:
From: "ME" <from@gmail.com>
To: "YOU" <to@gmail.com>
Subject: Test
Content-Type: multipart/mixed; boundary=MixedBoundary
--MixedBoundaryn
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-
第二种选择比第一种更方便,因为我可以用bash脚本轻松地收集我需要的任何文件,所以我想为sparkpost重新制作它。
不幸的是,我找不到一个正常的描述来形容这样做。在早期阶段,我遇到了一个授权问题。正如我从smtp api文档中了解到的:https://developers.sparkpost.com/api/smtp/我必须授权:
my_email@domain.com:<MY_AUTH_KEY>
所以我尝试连接以查看sparkpost:的输出
curl -v
--ssl-reqd
--url "smtps://smtp.sparkpostmail.com:587"
--user "my_email@domain.com:<MY_AUTH_KEY>"
输出:
* 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
所以我有两个问题:
如何使用SMTP获得sparkpost的身份验证?
发送电子邮件的第二种选择对sparkpost有效吗?
我更准确地解决了问题并发现了错误。一开始我试着这样连接:
--url "smtps://smtp.sparkpostmail.com:587"
当我不得不这样做的时候:
--url "smtp://smtp.sparkpostmail.com:587"
要根据文档而不是登录,通过SMTP在Sparkpost中获得授权,您应该编写SMTP_Injection
。
授权本身看起来是这样的:
--user "SMTP_Injection:<YOUR_COOL_API_KEY>"
总的来说,一切看起来都是这样的:
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--