如何使用SMTP在Linux shell脚本中发送电子邮件而不提供密码?

时间:2014-12-15 06:20:13

标签: shell email smtp

我想在shell脚本中发送电子邮件,该脚本使用smtp协议,没有任何密码验证。

我有以下批处理文件,无需密码即可发送邮件。以下脚本使用SMTP协议发送电子邮件:

@echo off
setlocal

set Port=25
set SSL=False
set From="xyz.joshi@company.com"
set To="qwerty@company.com"
set Subject="Started"
set Body="Body"
set SMTPServer="smtp.company.com"
set User="xyz.joshi@company.com"
set fileattach=""

if "%~7" NEQ "" (
set From="%~1"
set To="%~2"
set Subject="%~3"
set Body="%~4"
set SMTPServer="%~5"
set User="%~6"
set Pass="%~7"
set fileattach="%~8"
)

set "vbsfile=%temp%\email-bat.vbs"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From     = %From%
echo >>"%vbsfile%" objEmail.To       = %To%
echo >>"%vbsfile%" objEmail.Subject  = %Subject%
echo >>"%vbsfile%" objEmail.Textbody = %body%
if exist %fileattach% echo >>"%vbsfile%" objEmail.AddAttachment %fileattach%
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = %SMTPServer%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = %port%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = %user%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = %SSL%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 30
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send

cscript.exe /nologo "%vbsfile%"
echo email sent
del "%vbsfile%" 2>nul
goto :EOF 

2 个答案:

答案 0 :(得分:1)

您可以使用sendmail:

/usr/sbin/sendmail -f from@from.com to@to.com < /file/to/send

其中/ file / to / send包含RFC 2822 Internet消息格式的消息。

答案 1 :(得分:0)

mail -s "Test Email" xyz.joshi@company.com < /dev/null