Advertisement
alpa_s

Untitled

Oct 4th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.  
  5. "log"
  6. "net/mail"
  7. "encoding/base64"
  8. "net/smtp"
  9. "fmt"
  10. "strings"
  11. )
  12.  
  13. func encodeRFC2047(String string) string{
  14. // use mail's rfc2047 to encode any string
  15. addr := mail.Address{String, ""}
  16. return strings.Trim(addr.String(), " <>")
  17. }
  18.  
  19.  
  20. func main() {
  21. // Set up authentication information.
  22.  
  23. smtpServer := "smtp.yandex.ru"
  24. auth := smtp.PlainAuth(
  25. "",
  26. "*****",
  27. smtpServer,
  28. )
  29.  
  30. from := mail.Address{"Alex", "[email protected]"}
  31. to := mail.Address{"Alex", "[email protected]"}
  32. title := "Hello Alex"
  33.  
  34. body := "Hello Alex, this is test mail"
  35.  
  36. header := make(map[string]string)
  37. header["From"] = from.String()
  38. header["To"] = to.String()
  39. header["Subject"] = encodeRFC2047(title)
  40. header["MIME-Version"] = "1.0"
  41. header["Content-Type"] = "text/plain; charset=\"utf-8\""
  42. header["Content-Transfer-Encoding"] = "base64"
  43.  
  44. message := ""
  45. for k, v := range header {
  46. message += fmt.Sprintf("%s: %s\r\n", k, v)
  47. }
  48. message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
  49.  
  50. // Connect to the server, authenticate, set the sender and recipient,
  51. // and send the email all in one step.
  52. err := smtp.SendMail(
  53. smtpServer + ":25",
  54. auth,
  55. from.Address,
  56. []string{to.Address},
  57. []byte(message),
  58. //[]byte("This is the email body."),
  59. )
  60. if err != nil {
  61. log.Fatal(err)
  62. } else {
  63. fmt.Println("all is ok")
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement