使用curl发送电子邮件

xhv8bpkk  于 2022-11-13  发布在  其他
关注(0)|答案(5)|浏览(633)

如何使用curl命令行程序从gmail帐户发送电子邮件?
我已尝试以下方法:

curl -n --ssl-reqd --mail-from "<sender@gmail.com>" --mail-rcpt "<receiver@server.tld>" --url smtps://smtp.gmail.com:465 -T file.txt

但是,当file.txt作为电子邮件的内容时,当我运行此命令时,我得到以下错误:

curl: (67) Access denied: 530

是否可以从个人服务器托管的帐户发送电子邮件,仍然使用curl?这是否使认证过程更容易?

ahy6op9u

ahy6op9u1#

curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user 'username@gmail.com:password' \
  --mail-from 'username@gmail.com' \
  --mail-rcpt 'john@example.com' \
  --upload-file mail.txt
  • mail.txt * 文件内容:
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!

其他信息:
1.我使用的是支持SSL的curl版本7.21.6。
1.您不需要使用--insecure开关,它会阻止curl执行SSL连接验证。请参阅this online resource了解更多详细信息。
1.通过命令行参数传递帐户凭证被认为是一种不好的安全做法。请使用--netrc-file。请参阅the documentation
1.对于安全性较低的应用程序或较新的App passwords,您必须使用turn on access

r7knjye2

r7knjye22#

如果要发送抄送或密件抄送邮件:
第一个

cotxawn7

cotxawn73#

创建一个简单的email.conf文件,如下所示

Username:   hi@example.com
Password:   OKbNGRcjiV
POP/IMAP Server:    mail.example.com

在使其可执行(sudo chmod +x sendmail.sh)后,只需运行sendmail.sh即可。

./sendmail.sh

编码

#!/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']='your-email-address@gmail.com';

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

pkwftd7m

pkwftd7m4#

请注意mail.txt的格式似乎很重要/ CRLF用于win,LF用于Linux,特殊字符等。
最后经过2个小时的挣扎,它为我工作的GMX(他们告诉他们的SMPT端口是587 -并进一步在小写字母的提示:“465也可与SSL一起使用”):
在Linux下(安装了curl.tcz的Raspberry 3B+上的TinyCore Linux):

curl --ssl-reqd --url 'smtps://mail.gmx.net:465' --user 'mymail@gmx.at:mymailPassword' --mail-from 'mymail@gmx.at' --mail-rcpt 'mymail@gmx.at' --upload-file mail.txt

窗下:

curl --ssl-reqd --url "smtps://mail.gmx.net:465" --user "mymail@gmx.at:mymailPassword" --mail-from "mymail@gmx.at" --mail-rcpt "mymail@gmx.at" --upload-file mail_win.txt

使用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!
x8goxv8g

x8goxv8g5#

请注意,如果使用Perl的“system()“函数执行curl命令,则每个参数'word'在参数数组中是单独的项,并且单词不得用引号引起来
另请注意,如果在2022年5月30日之后通过Gmail发送邮件,Gmail账户必须设置双因素身份验证,然后必须创建“应用程序密码”。应用程序密码是一个长字符串,作为替代密码,并取代“--user“参数上的常用密码。

相关问题