I want to send mail to multiple recipients in Go, with my yahoo mail, but I from all recipients only I get mail.
Code:
err := smtp.SendMail(
"smtp.mail.yahoo.com:25",
auth,
"testmail1#yahoo.com",
[]string{"testmail1#yahoo.com, testmail2#yahoo.com"},
[]byte("test")
message:
From: "testMail1" <testmail1#yahoo.com>
To: testMail1 <testmail1#yahoo.com>, testMail2 <testmail2#yahoo.com>,
Subject: "mail"
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64
This is output:
2015/05/18 20:22:26 501 Syntax error in arguments
What have I done wrong?
Your code snippet is incomplete and does not match your email. Could you send more code?
Anyway you can try replacing:
[]string{"testmail1#yahoo.com, testmail2#yahoo.com"},
By:
[]string{"testmail1#yahoo.com", "testmail2#yahoo.com"},
Also you can try my package Gomail to easily send emails:
package main
import (
"gopkg.in/gomail.v2"
)
func main() {
m := gomail.NewMessage()
m.SetAddressHeader("From", "testmail1#yahoo.com", "testMail1")
m.SetHeader("To",
m.FormatAddress("testmail1#yahoo.com", "testMail1"),
m.FormatAddress("testmail2#yahoo.com", "testMail2"),
)
m.SetHeader("Subject", "mail")
m.SetBody("text/plain", "Hello!")
d := gomail.NewPlainDialer("smtp.mail.yahoo.com", 25, "login", "password")
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
This is my code send an email:
func SendEmail() {
from := "abc#xyz.com"
password := "password"
// Receiver email address.
to := []string{
"abc#example.com",
}
// smtp server configuration.
smtpHost := "smtp.gmail.com"
smtpPort := "587"
// Authentication.
auth := smtp.PlainAuth("", from, password, smtpHost)
t, _ := template.ParseFiles("template.html")
var body bytes.Buffer
subject := "Subject: This is a test subject\n"
mimeHeaders := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
test := Test{"123", "456"}
if err := t.Execute(&body, test); err != nil {
log.Fatalln(err.Error())
}
msg := []byte(subject + mimeHeaders + body.String())
// Sending email.
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, msg)
if err != nil {
log.Fatalln(err.Error())
return
}
fmt.Println("Email Sent!")
}
And below is the template HTML:
<html>
<body>
<h3>Name:</h3><span>{{.Name}}</span><br /><br />
<h3>Email:</h3><span>{{.Message}}</span><br />
</body>
</html>
When I execute SendEmail() on only file main.go then email is sent but when I call SendEmail in the body of API, I have a problem as below:
edit 1:
Maybe the path of file template is invalid.
Thanks for your attention.
I'm using smtp package of golang to send the mail from localhost to the given mail address. But there is a problem I'm providing my email and password for it but it will show me the error of
535 5.7.8 Username and Password not accepted. Learn more at
5.7.8 https://support.google.com/mail/?p=BadCredentials p24sm107930499pfk.155 - gsmtp
they want that I have to allow less secure app to use my account But I don't want to allow that I tried a small piece of code for it.
Tried Example1:-
// Set up authentication information.
auth := smtp.PlainAuth(
"",
"email",
"password",
"smtp.gmail.com",
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
"smtp.gmail.com:25",
auth,
"emailFrom",
[]string{EmailToooo},
[]byte("This is the email body."),
)
if err != nil {
log.Fatal(err)
}
*Tried Example 2:- *
m := gomail.NewMessage()
m.SetHeader("From", "SenderEmail#gmail.com")
m.SetHeader("To", "Email_Tooo#gmail.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
d := gomail.NewDialer("smtp.gmail.com", 587, "email", "password")
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
fmt.Println(err)
}
I also tried a gopkg.in/gomail.v2 package for doing NoAuth mail but in this it will give me the error of port connection see in given code:-
m := gomail.NewMessage()
m.SetHeader("From", "from#example.com")
m.SetHeader("To", "to#example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
d := gomail.Dialer{Host: "localhost", Port: 587}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
I also change the port to 8080 after doing 8080 it will not give any response it was showing only requesting.
Can anyone tell me that how will I send mail from localhost to the given mail address without auth?
Try to use port 587 on first example. It should be working.
err := smtp.SendMail(
"smtp.gmail.com:587",
auth,
"emailFrom",
[]string{EmailToooo},
[]byte("This is the email body."),
)
If you use smtp.gmail.com then the correct port is either 587 (TLS) or 465 (SSL), with the less secure app must be allowed.
Further information: https://support.google.com/a/answer/176600?hl=en
I am trying to send mail on my local system using gmail package of golang. For which I tried the following:
package main
import(
"fmt"
"github.com/SlyMarbo/gmail"
)
func main() {
email := gmail.Compose("Email subject", "Email body")
email.From = "account#gmail.com"
email.Password = "password"
// Defaults to "text/plain; charset=utf-8" if unset.
email.ContentType = "text/html; charset=utf-8"
// Normally you'll only need one of these, but I thought I'd show both.
email.AddRecipient("recepient#domain.com")
err := email.Send()
if err != nil {
fmt.Println(err)
// handle error.
}
}
I am neither getting any error nor mail. Not sure if it is not sent due to local server. Can anyone please guide me what am I missing? I took reference from this page.
I am trying to send an email to the localhost stmp server. I am using fakesmtp program to receive email from localhost.
Look at following code snippet
package mail
import (
"encoding/base64"
"fmt"
"log"
"net/mail"
"net/smtp"
"strings"
)
func encodeRFC2047(String string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{String, ""}
return strings.Trim(addr.String(), " <>")
}
func Send() {
// Set up authentication information.
smtpServer := "127.0.0.1:2525"
auth := smtp.PlainAuth(
"",
"admin",
"admin",
smtpServer,
)
from := mail.Address{"example", "info#example.com"}
to := mail.Address{"customer", "customer#example.com"}
title := "Mail"
body := "This is an email confirmation."
header := make(map[string]string)
header["From"] = from.String()
header["To"] = to.String()
header["Subject"] = encodeRFC2047(title)
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
smtpServer,
auth,
from.Address,
[]string{to.Address},
[]byte(message),
//[]byte("This is the email body."),
)
if err != nil {
log.Fatal(err)
}
}
When I executed the send function, I've got the error unencrypted connection. Why?
Most likely the server does not allow you to use plain-text authentication over an unencrypted connection, which is a sensible default for almost any MTA out there. Either change authentication info to e.g. digest, or enable SSL/TLS in you client code.
Remember to use tcpdump or wireshark to check what is actually transmitted.
This seems like a very common need, but I didn't find any good guides when I searched for it.
Assuming that you're using the net/smtp package and so the smtp.SendMail function, you just need to declare the MIME type in your message.
subject := "Subject: Test email from Go!\n"
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
body := "<html><body><h1>Hello World!</h1></body></html>"
msg := []byte(subject + mime + body)
smtp.SendMail(server, auth, from, to, msg)
Hope this helps =)
I am the author of gomail. With this package you can easily send HTML emails:
package main
import (
"gopkg.in/gomail.v2"
)
func main() {
m := gomail.NewMessage()
m.SetHeader("From", "alex#example.com")
m.SetHeader("To", "bob#example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b>!")
// Send the email to Bob
d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
You can also add a plain text version of the body in your email for the client that does not support HTML using the method AddAlternative.