smtp send email

150 views
Skip to first unread message

Tester

unread,
Apr 18, 2015, 12:17:02 PM4/18/15
to golan...@googlegroups.com
Hello Gophers,

I have a problem
When I try to send email by smtp package 
The sender message is my smtp user, not user who send a message
Is there a way to make the sender from user not smtp user?

my example

func SendEmail(name, email, to, subject, msg,
 smtpUser
, smtpPass, smtpProvider, smtpPort string) {


 
var rawMsg = "From: %s <%s>"
 rawMsg
+= "\nTo: %s"
 rawMsg
+= "\nSubject: %s"
 rawMsg
+= "\nMIME-Version: 1.0"
 rawMsg
+= "\nContent-Type: text/html; charset=utf-8"
 rawMsg
+= "\nContent-Transfer-Encoding: plain"
 rawMsg
+= "\n\n%s"


 
// Set up authentication information.
 auth
:= smtp.PlainAuth("",
 smtpUser
,
 smtpPass
,
 smtpProvider
,
 
)


 
// Connect to the server, authenticate, set the sender and recipient,
 
// and send the email all in one step.
 body
:= fmt.Sprintf(rawMsg, name, email, to, subject, msg)


 err
:= smtp.SendMail(smtpProvider+":"+smtpPort,
 auth
,
 email
,
 
[]string{to},
 
[]byte(body),
 
)
 
if err != nil {
 
Logs(err)
 
}
}

name := "My Name"
email
:= "m...@example.com"
to
:= "m...@golang.com"
subject
:= "My subject"
smtpUser
:= "??????@gmail.com"
smtpPass
:= "**********"
smtpProvider
:= "smtp.gmail.com"
smtpPort
:= "587"


SendEmail(name, email, to, subject, msg, smtpUser, smtpPass, smtpProvider, smtpPort)

Tamás Gulácsi

unread,
Apr 18, 2015, 2:21:33 PM4/18/15
to golan...@googlegroups.com
Sure, pay your ISP or mail provider for right to rewrite your from field.

Tester

unread,
Apr 19, 2015, 6:00:16 AM4/19/15
to golan...@googlegroups.com
Thank you for your answer
But unfortunately I do not understand what this means,
can you explain more?

Tamás Gulácsi

unread,
Apr 19, 2015, 6:59:13 AM4/19/15
to golan...@googlegroups.com
With SMTP, you connect to a server, send them envelope settings, then the email.
That server decides, whether you're allowed to set the From address.

A server without authentication that allows setting the From address is called an Open Relay, and banned from everywhere, as a potential spam source.
Usually your ISP gives you a login/password and an SMTP server to use, and usually there you can set the From address, but only from machines in that ISP's IP range.
GMail does not allow you to forge the From address.

Do you want to get email from your friends, sent by spammers easily? Of course you don't.
That's why the From is guarded.

Egon

unread,
Apr 19, 2015, 7:04:27 AM4/19/15
to golan...@googlegroups.com


On Saturday, 18 April 2015 19:17:02 UTC+3, Tester wrote:
Hello Gophers,

I have a problem
When I try to send email by smtp package 
The sender message is my smtp user, not user who send a message
Is there a way to make the sender from user not smtp user?

Also why do you want to send an email from an account that doesn't match the FROM field? Maybe there are other solutions to your problem.

Egon

unread,
Apr 19, 2015, 7:09:23 AM4/19/15
to golan...@googlegroups.com


On Saturday, 18 April 2015 19:17:02 UTC+3, Tester wrote:
Hello Gophers,

I have a problem
When I try to send email by smtp package 
The sender message is my smtp user, not user who send a message
Is there a way to make the sender from user not smtp user?

my example

func SendEmail(name, email, to, subject, msg,
 smtpUser
, smtpPass, smtpProvider, smtpPort string) {


 
var rawMsg = "From: %s <%s>"
 rawMsg
+= "\nTo: %s"
 rawMsg
+= "\nSubject: %s"
 rawMsg
+= "\nMIME-Version: 1.0"
 rawMsg
+= "\nContent-Type: text/html; charset=utf-8"
 rawMsg
+= "\nContent-Transfer-Encoding: plain"
 rawMsg
+= "\n\n%s"

Also you must use \r\n (CRLF) as the new line, see https://tools.ietf.org/html/rfc2822

Tester

unread,
Apr 20, 2015, 5:05:19 AM4/20/15
to golan...@googlegroups.com
@Tamás Gulácsi Thank you for this valuable information and for your patience
Reply all
Reply to author
Forward
0 new messages