本文整理汇总了Golang中net/smtp.PlainAuth函数的典型用法代码示例。如果您正苦于以下问题:Golang PlainAuth函数的具体用法?Golang PlainAuth怎么用?Golang PlainAuth使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PlainAuth函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Send
func (self *MailMessage) Send() error {
var auth smtp.Auth
if err := self.Validate(); err != nil {
return err
}
to := make([]string, len(self.To))
for i := range self.To {
to[i] = self.To[i].Address
}
from := self.From.Address
if from == "" {
from = AppConfig.smtpUser // Config.From.Address
}
addr := fmt.Sprintf("%s:%d", AppConfig.smtpHost, AppConfig.smtpPort)
if AppConfig.smtpTLS {
auth = fakeAuth{smtp.PlainAuth("", AppConfig.smtpUser,
AppConfig.smtpPassword, AppConfig.smtpHost)}
} else {
auth = smtp.PlainAuth("", AppConfig.smtpUser, AppConfig.smtpPassword,
AppConfig.smtpHost)
}
return smtp.SendMail(addr, auth, from, to, []byte(self.String()))
}
示例2: sendEmailHtml
// Sends the content passed as parameter to the recipient list
// of the given batch result
func (s *SmtpConfig) sendEmailHtml(r *BatchResult, content string) {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
subject := fmt.Sprintf("Subject: MysqlBatch Results for the batch \"%s\"\n", r.BatchName)
TraceActivity.Printf("sending the email : %s \n", subject)
dest := make([]string, 0)
for _, d := range r.Recipients.RecipientList {
dest = append(dest, d.Address)
}
auth := smtp.PlainAuth(
"",
s.User,
s.Password,
s.Server,
)
err := smtp.SendMail(
s.Server+":"+s.Port,
auth,
s.getSender(),
dest,
[]byte(subject+mime+content),
)
if err != nil {
TraceActivity.Printf("error sending the email : %s \n", err.Error())
log.Fatal(err)
}
}
示例3: getSMTPAuth
func getSMTPAuth(hasAuth bool, mechs string) (smtp.Auth, *tls.Config, error) {
if !hasAuth {
return nil, nil, nil
}
username := os.Getenv("SMTP_AUTH_USERNAME")
for _, mech := range strings.Split(mechs, " ") {
switch mech {
case "CRAM-MD5":
secret := os.Getenv("SMTP_AUTH_SECRET")
if secret == "" {
continue
}
return smtp.CRAMMD5Auth(username, secret), nil, nil
case "PLAIN":
password := os.Getenv("SMTP_AUTH_PASSWORD")
if password == "" {
continue
}
identity := os.Getenv("SMTP_AUTH_IDENTITY")
// We need to know the hostname for both auth and TLS.
host, _, err := net.SplitHostPort(*smtpSmartHost)
if err != nil {
return nil, nil, fmt.Errorf("invalid address: %s", err)
}
auth := smtp.PlainAuth(identity, username, password, host)
cfg := &tls.Config{ServerName: host}
return auth, cfg, nil
}
}
return nil, nil, nil
}
示例4: alert
func alert(domain string, error_message string) {
fmt.Println("Sending mail")
subject := fmt.Sprintf("Subject: %s is down\r\n\r\n", domain)
auth := smtp.PlainAuth(
"",
config.SMTP.Username,
config.SMTP.Password,
config.SMTP.Host,
)
err := smtp.SendMail(
config.SMTP.OutgoingServer,
auth,
config.SMTP.From,
config.ToEmail,
[]byte(subject+error_message),
)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Mail sent")
}
}
示例5: CreateMailer
// CreateMailer is a constructor for gsmtp.Mailer
func CreateMailer(username, password, host, port string) (m *Mailer) {
m = &Mailer{
auth: smtp.PlainAuth("", username, password, host),
address: fmt.Sprintf("%v:%v", host, port),
}
return m
}
示例6: Send
// Send mail.
func (mail Config) Send(to, subject, template string, args map[string]interface{}) {
switch mail.Mode {
case "smtp":
host := mail.Config["host"]
sender := mail.Config["sender"]
user, useAuth := mail.Config["username"]
password, _ := mail.Config["password"]
var auth smtp.Auth
if useAuth {
auth = smtp.PlainAuth("", user, password, host)
}
var buf bytes.Buffer
buf.WriteString("From: ")
buf.WriteString(sender)
buf.WriteString("\n")
buf.WriteString("To: ")
buf.WriteString(to)
buf.WriteString("\n")
buf.WriteString("Subject: ")
buf.WriteString(subject)
buf.WriteString("\n\n")
tmpl.ExecuteTemplate(&buf, template, args)
smtp.SendMail(host, auth, sender, []string{to}, bytes.Replace([]byte{'\n'}, buf.Bytes(), []byte{'\n', '\r'}, -1))
}
}
示例7: Send
func (self mailerImpl) Send(mail Mail) error {
e := email.NewEmail()
if mail.From != "" {
e.From = mail.From
} else {
e.From = self.cfg.Email
}
e.To = mail.To
e.Cc = mail.Cc
e.Bcc = mail.Bcc
e.Subject = mail.Subject
e.HTML = []byte(mail.Body)
for _, attachment := range mail.Attachments {
e.AttachFile(attachment)
}
hostAndPort := strings.Join([]string{
self.cfg.Host,
strconv.Itoa(self.cfg.Port),
}, ":")
plainAuth := smtp.PlainAuth(
"", // identity
self.cfg.Email,
self.cfg.Password,
self.cfg.Host,
)
return e.Send(hostAndPort, plainAuth)
}
示例8: Send
// Send sends the composed email
func (email *email) Send(address string) error {
var auth smtp.Auth
from := email.getFrom()
if from == "" {
return errors.New(`Mail Error: No "From" address specified.`)
}
if len(email.recipients) < 1 {
return errors.New("Mail Error: No recipient specified.")
}
msg := email.GetMessage()
host, port, err := net.SplitHostPort(address)
if err != nil {
return errors.New("Mail Error: " + err.Error())
}
if email.Username != "" || email.Password != "" {
auth = smtp.PlainAuth("", email.Username, email.Password, host)
}
return send(host, port, from, email.recipients, msg, auth, email.Encryption, email.TLSConfig, email.ConnectTimeout)
}
示例9: SendEmail
func (this *EmailService) SendEmail(to, subject, body string) (ok bool, e string) {
InitEmailFromDb()
if host == "" || emailPort == "" || username == "" || password == "" {
return
}
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", username, password, hp[0])
var content_type string
mailtype := "html"
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + username + "<" + username + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host+":"+emailPort, auth, username, send_to, msg)
if err != nil {
e = fmt.Sprint(err)
return
}
ok = true
return
}
示例10: send
func (e *Email) send(events []notices.Event) error {
if e.debounce {
events = e.cleanupEvents(events)
}
if len(events) == 0 {
return nil
}
identity := e.params["identity"]
from := e.params["from"]
to := e.params["to"]
password := e.params["password"]
host := e.params["host"]
port := e.params["port"]
auth := smtp.PlainAuth(identity, from, password, host)
buf := bytes.NewBuffer([]byte{})
if err := e.tmpl.Execute(buf, events); err != nil {
return err
}
msg := buf.String()
body := "From: " + from + "\r\n"
body += "To: " + to + "\r\n"
body += "Subject: " + e.subject(events) + "\r\n"
body += "MIME-Version: 1.0\r\n"
body += "Content-Type: text/plain; charset=\"utf-8\"\r\n"
body += "Content-Transfer-Encoding: base64\r\n"
body += base64.StdEncoding.EncodeToString([]byte(msg))
return smtp.SendMail(host+":"+port, auth, from, []string{to}, []byte(body))
}
示例11: sendEmail
func sendEmail(count int) {
if notificationsEnabled == false {
return
}
fmt.Println("Sending the email")
subjectString := "ALERT: INVENTORY LOW"
bodyString := ""
if count == 0 {
bodyString = "Out of items, please refill"
} else if count == 1 {
bodyString = "You only have 1 item left, order some moar"
} else if count < 3 {
bodyString = "Running low, be aware"
}
go sendSlackMessage(bodyString)
emailUser := &EmailUser{"monitor.inventory", "squaresquaresquare1!", "smtp.gmail.com", 587}
auth := smtp.PlainAuth("",
emailUser.Username,
emailUser.Password,
emailUser.EmailServer)
emailBody := fmt.Sprintf("From: AlwaysBeer\nTo: Dear Customer\nSubject: %s\n\n%s\nBottles Left: %d\n", subjectString, bodyString, count)
err := smtp.SendMail(emailUser.EmailServer+":"+strconv.Itoa(emailUser.Port), auth,
emailUser.Username,
emailList,
[]byte(emailBody))
if err != nil {
fmt.Println("ERROR: attempting to send a mail ", err)
}
}
示例12: mail
func (fm *FeedMailer) mail(ch *rss.Channel, item *rss.Item) error {
date, _ := item.ParsedPubDate()
data := struct {
SubjectPrefix, ChanTitle, ItemTitle string
Date time.Time
Links []*rss.Link
Description string
Content *rss.Content
}{fm.prof.SubjectPrefix, ch.Title, item.Title, date,
item.Links, item.Description, item.Content}
msg := &bytes.Buffer{}
if err := mailTemplate.Execute(msg, data); err != nil {
return err
}
log.Printf("Sending e-mail: [%s] %s", ch.Title, item.Title)
auth := smtp.PlainAuth("", fm.prof.SmtpUser, fm.prof.SmtpPass, fm.prof.SmtpHost)
err := smtp.SendMail(fm.prof.SmtpAddr, auth, fm.prof.SrcEmail,
fm.prof.DstEmails, msg.Bytes())
if err != nil {
return err
}
return nil
}
示例13: Send
// Send sends the message that has been setup.
func (m *Message) Send(c *Configuration) {
emailUser := &GmailConf{c.Username, c.Password, c.Server, c.Port}
auth := smtp.PlainAuth("", emailUser.Username, emailUser.Password, emailUser.EmailServer)
var err error
var doc bytes.Buffer
err = smtp.SendMail(fmt.Sprintf("%s:%s", emailUser.EmailServer, strconv.Itoa(emailUser.Port)),
auth,
emailUser.Username,
[]string{"[email protected]"},
doc.Bytes())
if err != nil {
log.Fatalln(err)
}
t, err := template.New("emailTemplate").Parse(emailTemplate)
if err != nil {
log.Fatalln("error trying to parse mail template")
}
err = t.Execute(&doc, m)
if err != nil {
log.Fatalln(err)
}
}
示例14: sendReportMail
// send a message through email
func sendReportMail(subject, body string) {
clear()
// indicate that mail is being sent
fmt.Printf("Sending mail to %s...\n", RCVR_ADD)
var err error // error
var doc bytes.Buffer // buffer of bytes
// authorize email
auth := smtp.PlainAuth("", DIVYA_ADD, DIVYA_PWD, EMAIL_SRV)
// setup a context for the mail
context := &SmtpTemplateData{"Divya", DIVYA_ADD, RCVR_ADD, subject, body}
// set up template
temp := template.New("Email Template")
temp, err = temp.Parse(TEMPLATE)
if err != nil {
log.Print("error trying to parse mail template")
}
// write the mail using template and context
err = temp.Execute(&doc, context)
if err != nil {
log.Print("error trying to execute mail template")
}
// send mail
err = smtp.SendMail(EMAIL_SRV+":"+strconv.Itoa(PORT), auth, DIVYA_ADD,
[]string{RCVR_ADD}, doc.Bytes())
if err != nil {
log.Print("ERROR: attempting to send a mail ", err)
} else {
fmt.Println("Mail sent successfully!")
}
}
示例15: NewMailer
// NewMailer returns a mailer. The given parameters are used to connect to the
// SMTP server via a PLAIN authentication mechanism.
func NewMailer(host string, username string, password string, port int, settings ...MailerSetting) *Mailer {
return NewCustomMailer(
fmt.Sprintf("%s:%d", host, port),
smtp.PlainAuth("", username, password, host),
settings...,
)
}