我有一个Go程序,可以向我发送邮件。
package main
import (
"log"
"net/smtp"
"os"
)
func main() {
send(os.Args[2] + " program completed.", os.Args[1], os.Args[2], os.Args[3])
}
func send(body string, to string, s string, date string) {
from := "foo@gmail.com"
pass := "bar"
msg := "From: " + from + "n" +
"To: " + to + "n" +
"Subject: "+ s + " mainnn" +
body + "n" + date
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
return
}
log.Print("sent, visit mail address: "+to)
}
还有一个bash脚本,它运行时带有一个邮件列表以备将来准备,
Do things.....
.
.
filename='list'
while read line; do
# reading each line of list
echo "$(date '+%d-%m-%Y-%T') Mail sent to address : $line" >> ${now}-log.log
./mailsend ${line} foo ${date}
done < $filename
Do final things .....
正如您所看到的,有一些简单的日志尝试来查看程序是否运行良好。而且没有任何错误。
当我手动触发程序时,它运行得很好,但从cronjob触发的bash脚本来看,它不起作用。
有什么建议吗?
edit1:变量是实心的。Manuel触发器按预期工作。当被cron触发时,我不会收到邮件。
在调用./mailsend
之前,可能需要将cd
保存到您的目录中?