Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There's a way to efficiently send more than one mail in one connection? #10

Closed
marcalj opened this issue Dec 4, 2014 · 6 comments
Closed
Milestone

Comments

@marcalj
Copy link

marcalj commented Dec 4, 2014

I made a quick review of your code and I think you open close a connection for each Send (https://github.com/go-gomail/gomail/blob/master/send.go#L22).

I want to implement a job to send emails from a queue in bulk, but reusing the connection to be as more efficient as possible.

I miss something from your API?

Thanks!

@alexcesaro
Copy link
Member

You can already do it by using getSendMailFunc and implementing your own email-sending function.

But I can also add new methods so you can do something like that:

c := mailer.Connect()
defer c.Close()
for _, msg := range list {
    if err := c.Send(msg); err != nil {
        panic(err)
    }
}

Would that fits your need?

@alexcesaro alexcesaro added this to the v2 milestone Dec 18, 2014
@marcalj
Copy link
Author

marcalj commented Jan 11, 2015

Sorry, I wasn't able to play with your library until now! :)

Yep, this would be a great addition to this package! I'll try to implement a custom email-sending function. Not sure when btw...

Thanks!!!

@0x7061
Copy link

0x7061 commented May 10, 2015

+1

@alexcesaro
Copy link
Member

I will do it for Gomail v2 (which should be released in August as Go 1.5). In the meantime you can already do it by implementing your own email-sending function and using SetSendMail.

@alexcesaro
Copy link
Member

Gomail v2 will handle sending many mails on one connection. You can already try the unstable version and tell me if it is fine:

package main

import (
    "fmt"

    "gopkg.in/gomail.v2-unstable"
)

func main() {
    messages := make([]*gomail.Message, 10)
    for i := 0; i < len(messages); i++ {
        m := gomail.NewMessage()
        m.SetHeader("From", "from@example.com")
        m.SetHeader("To", "to@example.com")
        m.SetHeader("Subject", fmt.Sprintf("Test %d", i))
        m.SetBody("text/html", fmt.Sprintf("Test %d", i))
        messages[i] = m
    }

    d := gomail.NewPlainDialer("smtp.example.com", "user", "123456", 587)
    if err := d.DialAndSend(messages...); err != nil {
        panic(err)
    }
}

@0x7061
Copy link

0x7061 commented Sep 20, 2015

Awesome! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants