Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added gomail/.DS_Store
Binary file not shown.
52 changes: 29 additions & 23 deletions send_mail.go → gomail/send_mail.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gomail

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still want to retain the name either on gostmp or go_smtp


import (
"bytes"
Expand All @@ -13,44 +13,50 @@ import (
Thank you very much.
**/

const (
/**
Gmail SMTP Server
**/
SMTPServer = "smtp.gmail.com"
)

type Sender struct {
User string
Password string
// SMTPServer stores host, port for SMTP map
type SMTPServer struct {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is redundant since you have defined in the Sender struct.

Host string
Port string
}

func NewSender(Username, Password string) Sender {
// SMTP Servers
var SMTP = map[string]*SMTPServer{
"mail.ru": &SMTPServer{Host: "smtp.mail.ru", Port: "465"},
"yandex.com": &SMTPServer{Host: "smtp.yandex.com", Port: "465"},
"gmail.com": &SMTPServer{Host: "smtp.gmail.com", Port: "465"},
}

return Sender{Username, Password}
// Sender client request
type Sender struct {
User string
Password string
SMTPServer string
SMTPPort string
}

func (sender Sender) SendMail(Dest []string, Subject, bodyMessage string) {
// NewSender create Sender
func NewSender(Username, Password, SMTPServer, SMTPPort string) Sender {
return Sender{Username, Password, SMTPServer, SMTPPort}
}

// SendMail send mail to client
func (sender Sender) SendMail(Dest []string, Subject, bodyMessage string) (err error) {
msg := "From: " + sender.User + "\n" +
"To: " + strings.Join(Dest, ",") + "\n" +
"Subject: " + Subject + "\n" + bodyMessage

err := smtp.SendMail(SMTPServer+":587",
smtp.PlainAuth("", sender.User, sender.Password, SMTPServer),
err = smtp.SendMail(sender.SMTPServer+":"+sender.SMTPPort,
smtp.PlainAuth("", sender.User, sender.Password, sender.SMTPServer),
sender.User, Dest, []byte(msg))

if err != nil {

fmt.Printf("smtp error: %s", err)
return
}

fmt.Println("Mail sent successfully!")
return
}

// WriteEmail to message
func (sender Sender) WriteEmail(dest []string, contentType, subject, bodyMessage string) string {

header := make(map[string]string)
header["From"] = sender.User

Expand Down Expand Up @@ -84,12 +90,12 @@ func (sender Sender) WriteEmail(dest []string, contentType, subject, bodyMessage
return message
}

// WriteHTMLEmail to html email
func (sender *Sender) WriteHTMLEmail(dest []string, subject, bodyMessage string) string {

return sender.WriteEmail(dest, "text/html", subject, bodyMessage)
}

// WritePlainEmail to plain email
func (sender *Sender) WritePlainEmail(dest []string, subject, bodyMessage string) string {

return sender.WriteEmail(dest, "text/plain", subject, bodyMessage)
}
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package main

import (
"fmt"

"github.com/darkfoxs96/go_smtp/gomail"
)

func main() {

sender := NewSender("<YOUR EMAIL ADDRESS>", "<YOUR EMAIL PASSWORD>")
sender := gomail.NewSender("<YOUR EMAIL ADDRESS>", "<YOUR EMAIL PASSWORD>", gomail.SMTP["yandex.com"].Host, gomail.SMTP["yandex.com"].Port)

//The receiver needs to be in slice as the receive supports multiple receiver
Receiver := []string{"abc@gmail.com", "xyz@gmail.com", "larrypage@googlemail.com"}
Receiver := []string{"abc@yandex.com", "xyz@gmail.com", "larrypage@googlemail.com"}

Subject := "Testing HTLML Email from golang"
message := `
Expand All @@ -25,5 +31,8 @@ func main() {
`
bodyMessage := sender.WriteHTMLEmail(Receiver, Subject, message)

sender.SendMail(Receiver, Subject, bodyMessage)
err := sender.SendMail(Receiver, Subject, bodyMessage)
if err != nil {
fmt.Println("Error: ", err)
}
}