Hi Andrey,

Thank you. I have been trying to figure out how to use your example. But 
the email that I receive from it are plain text even the encoded attached 
file. It does not appear as attached but as encoded characters in the mail 
body. And there is still no attachment seen. I tried modifying your example 
but to no avail. I end up using your example code per code but still does 
not work. Please see below excerpt of my code where I call your example in 
a function I called "attachToEmail". I have a send mail function 
"sendEmail" using smpt:

I do not know which part I am missing. I have been trying to figure this 
out for more than a week now.

func sendEmail(email string, typeOfEmail int, lastInsertedId int) {
/*e := email.NewEmail()

e.From = "em...@email.com"
e.To = []string{emails}
e.Bcc = []string{""}
e.Cc = []string{""}
e.Subject = "Text Receipt"
e.Text = []byte("This is to confirm that we received your email.")
if IncludeEmailDump {
e.Text = []byte(getStats(emails, lastInsertedId))
}
e.HTML = []byte("<h1>Fancy HTML is supported, too!</h1>")
e.Send(EmailHostName, smtp.PlainAuth("", EmailUserName, 
EmailServerPassword, EmailHostName))*/

addr := EmailHostName + ":" + EmailServerPort

fromName := "emailFrom"
fromEmail := "em...@email.com"
toNames := []string{"Client"}
toEmails := []string{email}
subject := "Email"
body := "This is to confirm that we received your email."
if IncludeEmailDump {
body = body + getStats(email, lastInsertedId)
}

// Build RFC-2822 email
toAddresses := []string{}
for i := range toEmails {
mAdd := mail.Address{}
mAdd.Name = toNames[i]
mAdd.Address = toEmails[i]
toAddresses = append(toAddresses, mAdd.String())
}

toHeader := strings.Join(toAddresses, ", ")
fAdd := mail.Address{}
fAdd.Name = fromName
fAdd.Address = fromEmail
fromHeader := fAdd.String()
subjectHeader := subject
header := make(map[string]string)

header["To"] = toHeader
header["From"] = fromHeader
header["Subject"] = subjectHeader
header["Content-Type"] = `text/html; charset="UTF-8"`
msg := ""
for headerKey, headerValue := range header {
msg += fmt.Sprintf("%s: %s\r\n", headerKey, headerValue)
}
msg += "\r\n" + body
bMsg := []byte(msg)

// Send using local postfix service
smtpPostfix, err := smtp.Dial(addr)
if err != nil {
logError(err.Error())
}

defer smtpPostfix.Close()
if err = smtpPostfix.Mail(fromHeader); err != nil {
logError(err.Error())
}
for _, addr := range toEmails {
if err = smtpPostfix.Rcpt(addr); err != nil {
logError(err.Error())
}
}
smtpData, err := smtpPostfix.Data()
if err != nil {
logError(err.Error())
}

_, err = smtpData.Write(bMsg)
if err != nil {
logError(err.Error())
}

attachToEmail(smtpData, "file.txt", "boundary1234567890")

err = smtpData.Close()
if err != nil {
logError(err.Error())
}
err = smtpPostfix.Quit()
if err != nil {
logError(err.Error())
}

//attachToEmail(email)
}

func attachToEmail(writeToEmail io.Writer, filename string, boundary 
string) {
fmt.Fprintf(writeToEmail, "\n--%s\n", "\n\n")
fmt.Fprintf(writeToEmail, "\n--%s\n", boundary)
contents, err := os.Open(filename)
if err != nil {
fmt.Fprintf(writeToEmail, "Content-Type: text/plain; charset=utf-8\n")
fmt.Fprintf(writeToEmail, "could not open file: %v\n", err)
} else {
defer contents.Close()
//fmt.Fprintf(writeToEmail, "Content-Type: application/octet-stream\n")
fmt.Fprintf(writeToEmail, "Content-Type: application/octet-stream; 
charset=utf-8\n")
fmt.Fprintf(writeToEmail, "Content-Transfer-Encoding: base64\n")
fmt.Fprintf(writeToEmail, "Content-Disposition: 
attachment;filename=\"%s\"\n\n", filepath.Base(filename)+".gz")
fmt.Fprintf(writeToEmail, "\n--%s\n", boundary)
fmt.Fprintf(writeToEmail, "\n\n\n")

b64 := base64.NewEncoder(base64.StdEncoding, writeToEmail)
gzip := gzip.NewWriter(b64)
io.Copy(gzip, contents)
//io.Copy(b64, contents)
gzip.Close()
b64.Close()
}
fmt.Fprintf(writeToEmail, "\n--%s\n", boundary)
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to