我正在尝试使用smtp包的内置功能从GO发送一封简单的电子邮件.
我的简单代码如下:
func sendEmail(to string, body []byte) error {
auth := smtp.PlainAuth(
"",
config.SmtpUsername,
config.SmtpPassword,
config.SmtpHostname,
)
return smtp.SendMail(
fmt.Sprintf("%s:%d", config.SmtpHostname, config.SmtpPort),
auth,
config.SmtpUsername,
[]string{to},
body,
)
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理是,它始终将Return-Path标头设置为config.SmtpUsername的值,即使我发送包含自定义Return-Path标头的消息,基本上在发送消息之后,似乎以某种方式返回 - 消息的路径将替换为smtp用户名.
有关如何避免这种情况的任何想法,并使GO使用我给出的任何返回路径?
LE 1:如果有任何帮助,可以在以下网址找到代码片段:http
://play.golang.org/p/ATDCgJGKZ3 LE 2:我可以通过swiftmailer从php实现所需的行为,因此我不认为传送服务器正以任何方式改变标题.
更多代码:
PHP与swiftmailer,它设置正确的返回路径:
Yii::import('common.vendors.SwiftMailer.lib.classes.Swift', true);
Yii::registerAutoloader(array('Swift', 'autoload'));
Yii::import('common.vendors.SwiftMailer.lib.swift_init', true);
$hostname = '';
$username = '';
$password = '';
$returnPath = '';
$subject = 'Swiftmailer sending, test return path';
$toEmail = '';
$transport = Swift_SmtpTransport::newInstance($hostname, 25);
$transport->setUsername($username);
$transport->setPassword($password);
$logger = new Swift_Plugins_LoggerPlugin(new Swift_Plugins_Loggers_ArrayLogger());
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($logger);
$message = Swift_Message::newInstance();
$message->setReturnPath($returnPath);
$message->setSubject($subject);
$message->setFrom($username);
$message->setTo($toEmail);
$message->setBody('Hello, this is a simple test going on here...');
$sent = $mailer->send($message);
print_r($logger->dump());
Run Code Online (Sandbox Code Playgroud)
使用自定义mysmtp包,我只是InsecureSkipVerify: true在tls配置中设置以避免证书错误,但返回路径仍然是错误的:
hostname := ""
username := ""
password := ""
returnPath := ""
subject := "GO sending, test return path"
toEmail := ""
body := "Hello, this is a simple test going on here..."
auth := mysmtp.PlainAuth(
"",
username,
password,
hostname,
)
header := make(map[string]string)
header["Return-Path"] = returnPath
header["From"] = username
header["To"] = toEmail
header["Subject"] = subject
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + string([]byte(body))
err := mysmtp.SendMail(
fmt.Sprintf("%s:%d", hostname, 25),
auth,
username,
[]string{toEmail},
[]byte(message),
)
log.Fatal(err)
Run Code Online (Sandbox Code Playgroud)
我几乎不知道失败的原因和原因,最后一次测试是针对后缀mta进行的,我刚从reject_sender_login_mismatchpostfix配置中删除了该策略以允许此行为.
小智 5
Return-Path派生自客户端在发送消息时指定的"MAIL FROM"命令.
在http://golang.org/src/pkg/net/smtp/smtp.go上查看 smtp包的实现细节,实现一个使用替代地址作为参数的SendMail函数应该不会太困难func (c *Client) Mail(from string) error.