2014-07-25 31 views
6

我想使用smtp軟件包的內置功能從GO發送簡單的電子郵件。電子郵件發送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, 
    ) 
} 

而且它的工作原理,事情是,它總是返回路徑頭設置爲config.SmtpUsername的值,即使我發送一個包含自定義返回的消息-Path頭,基本上在發送消息之後,似乎以某種方式來自消息的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()); 

去定製mysmtp包,在這裏我只是TLS配置設置InsecureSkipVerify: true避免證書錯誤,仍然是返回路徑是錯誤的:

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) 

我真的不知道該怎麼失敗,爲什麼失敗,這最後的測試已經對後綴的MTA,其中IJ進行ust從後綴配置中刪除reject_sender_login_mismatch策略以允許此行爲。

+0

你可以添加你的消息正文和實際的[]字節嗎? – Momer

+0

@Momer - 看看http://play.golang.org/p/ATDCgJGKZ3無論你扔在那裏,結果是一樣的,返回路徑被覆蓋 – Twisted1919

+1

你可能無法通過代碼來實現。 SMTP系統設置了「返回路徑」(當已經存在時可以重置)。 [RFC 5321指定](https://tools.ietf.org/html/rfc5321#page-58)消息發起系統不應指定返回路徑。例如,據我所知,如果您使用Gmail並且您的返回路徑與您的用戶名不同,則Gmail會將其重置爲您的用戶名。 – bernhardw

回答

5

Return-Path是從客戶端在發送消息時指定的「MAIL FROM」命令派生而來的。

查看smtp包的實現細節http://golang.org/src/pkg/net/smtp/smtp.go實現一個SendMail函數使用另一個地址作爲參數func (c *Client) Mail(from string) error應該不會太困難。

+0

是的,你是對的,這種方式工作得很好。我試過這是第一件事,但沒有奏效,因爲postfix指令'smtpd_sender_restrictions'包含'reject_sender_login_mismatch'規則。如果我把它放回去,它不會工作,但我得出的結論是,在這種情況下是正確的行爲。 – Twisted1919

相關問題