Last active
October 14, 2024 17:42
-
-
Save jpillora/cb46d183eca0710d909a to your computer and use it in GitHub Desktop.
Send email using Go (Golang) via GMail with net/smtp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/smtp" | |
) | |
func main() { | |
send("hello there") | |
} | |
func send(body string) { | |
from := "[email protected]" | |
pass := "..." | |
to := "[email protected]" | |
msg := "From: " + from + "\n" + | |
"To: " + to + "\n" + | |
"Subject: Hello there\n\n" + | |
body | |
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 http://foobarbazz.mailinator.com") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
upd: this work for me, need add parametr from in msg object
msg := []byte("From: ***@mail.com\r\n" +
"To: ***@gmail.com\r\n" +
"Subject: New Hack\r\n" +
"\r\n" +
"Wonderful solution\r\n")