-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ Go Simple Mail supports: | |
- Support text/calendar content type body (since v2.11.0) | ||
- Support add a List-Unsubscribe header (since v2.11.0) | ||
- Support to add a DKIM signarure (since v2.11.0) | ||
- Support to send using custom connection, ideal for proxy (since v2.15.0) | ||
|
||
## Documentation | ||
|
||
|
@@ -102,7 +103,7 @@ package main | |
import ( | ||
"log" | ||
|
||
"github.com/xhit/go-simple-mail/v2" | ||
mail "github.com/xhit/go-simple-mail/v2" | ||
"github.com/toorop/go-dkim" | ||
) | ||
|
||
|
@@ -269,6 +270,77 @@ func main() { | |
} | ||
``` | ||
|
||
# Send with custom connection | ||
|
||
It's possible to use a custom connection with custom dieler, like a dialer that uses a proxy server, etc... | ||
|
||
With this, these servers params are ignored: `Host`, `Port`. You have total control of the connection. | ||
|
||
Example using a conn for proxy: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
mail "github.com/xhit/go-simple-mail/v2" | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
func main() { | ||
server := mail.NewSMTPClient() | ||
|
||
host := "smtp.example.com" | ||
port := 587 | ||
proxyAddr := "proxy.server" | ||
|
||
conn, err := getCustomConnWithProxy(proxyAddr, host, port) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
server.Username = "[email protected]" | ||
server.Password = "examplepass" | ||
server.Encryption = mail.EncryptionSTARTTLS | ||
server.CustomConn = conn | ||
|
||
smtpClient, err := server.Connect() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
email := mail.NewMSG() | ||
|
||
err = email.SetFrom("From Example <[email protected]>").AddTo("[email protected]").Send(smtpClient) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
} | ||
|
||
func getCustomConnWithProxy(proxyAddr, host string, port int) (net.Conn, error) { | ||
dial, err := proxy.SOCKS5("tcp", proxyAddr, nil, proxy.Direct) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dialer, err := dial.Dial("tcp", fmt.Sprintf("%s:%d", host, port)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conf := &tls.Config{ServerName: host} | ||
conn := tls.Client(dialer, conf) | ||
|
||
return conn, nil | ||
} | ||
|
||
``` | ||
|
||
## More examples | ||
|
||
See [example/example_test.go](example/example_test.go). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters