本文整理汇总了Golang中net/smtp.CRAMMD5Auth函数的典型用法代码示例。如果您正苦于以下问题:Golang CRAMMD5Auth函数的具体用法?Golang CRAMMD5Auth怎么用?Golang CRAMMD5Auth使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CRAMMD5Auth函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Notify
func (emailer *EmailNotifier) Notify(msg Message) error {
if emailer.auth == nil {
switch emailer.AuthType {
case "plain":
emailer.auth = smtp.PlainAuth("", emailer.Username, emailer.Password, emailer.Server)
case "crammd5":
emailer.auth = smtp.CRAMMD5Auth(emailer.Username, emailer.Password)
}
}
if emailer.template == nil {
template, err := template.ParseFiles(emailer.TemplateFile)
if err != nil {
log.Critical("Cannot parse email template: %v", err)
return err
}
emailer.template = template
}
if emailer.groupMsgs == nil {
emailer.groupMsgs = make(map[string]Message)
}
for _, group := range emailer.Groups {
clusterGroup := fmt.Sprintf("%s,%s", msg.Cluster, msg.Group)
if clusterGroup == group {
emailer.groupMsgs[clusterGroup] = msg
}
}
if len(emailer.Groups) == len(emailer.groupMsgs) {
return emailer.sendConsumerGroupStatusNotify()
}
return nil
}
示例2: NewSMTP
// NewSMTP is called with smtp[s]://[username:[password]]@server:[port]
func NewSMTP(rawURL string) (*SMTP, error) {
url, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
if url.Scheme != "smtp" && url.Scheme != "smtps" {
return nil, ErrInvalidScheme
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
mysmtp := &SMTP{
scheme: url.Scheme,
hostname: hostname,
}
_, _, err = net.SplitHostPort(url.Host)
if err != nil {
mysmtp.server = url.Host + ":25"
} else {
mysmtp.server = url.Host
}
if url.User != nil {
p, _ := url.User.Password()
a := smtp.CRAMMD5Auth(url.User.Username(), p)
mysmtp.auth = &a
}
return mysmtp, nil
}
示例3: Init
func (s *SmtpOutput) Init(config interface{}) (err error) {
s.conf = config.(*SmtpOutputConfig)
if s.conf.SendTo == nil {
return fmt.Errorf("send_to must contain at least one recipient")
}
host, _, err := net.SplitHostPort(s.conf.Host)
if err != nil {
return fmt.Errorf("Host must contain a port specifier")
}
s.sendFunction = smtp.SendMail
if s.conf.Auth == "Plain" {
s.auth = smtp.PlainAuth("", s.conf.User, s.conf.Password, host)
} else if s.conf.Auth == "CRAMMD5" {
s.auth = smtp.CRAMMD5Auth(s.conf.User, s.conf.Password)
} else if s.conf.Auth == "none" {
s.auth = nil
} else {
return fmt.Errorf("Invalid auth type: %s", s.conf.Auth)
}
return
}
示例4: SendMail
func (m *Mailer) SendMail(to, subject, body string) error {
host := m.Server
if strings.Contains(host, ":") {
host = strings.Split(host, ":")[0]
}
//log.Println("host=",host)
auths := []smtp.Auth{m.bestAuth}
msg := "from: \"" + MAIL_LABEL + "\" <" + m.User + ">\nto: " + to +
"\nsubject: (" + MAIL_LABEL + ") " + subject + "\n\n" + body
if m.bestAuth == nil {
auths = []smtp.Auth{smtp.CRAMMD5Auth(m.User, m.Passwd),
smtp.PlainAuth("", m.User, m.Passwd, host)}
}
var errs error
for _, auth := range auths {
err := smtp.SendMail(m.Server, auth, m.User, []string{to}, ([]byte)(msg))
if err == nil {
m.bestAuth = auth
return nil
} else {
errs = fmt.Errorf("%v%v\n", errs, err)
}
}
return errs
}
示例5: auth
// auth resolves a string of authentication mechanisms.
func (n *Email) auth(mechs string) (smtp.Auth, error) {
username := n.conf.AuthUsername
for _, mech := range strings.Split(mechs, " ") {
switch mech {
case "CRAM-MD5":
secret := string(n.conf.AuthSecret)
if secret == "" {
continue
}
return smtp.CRAMMD5Auth(username, secret), nil
case "PLAIN":
password := string(n.conf.AuthPassword)
if password == "" {
continue
}
identity := n.conf.AuthIdentity
// We need to know the hostname for both auth and TLS.
host, _, err := net.SplitHostPort(n.conf.Smarthost)
if err != nil {
return nil, fmt.Errorf("invalid address: %s", err)
}
return smtp.PlainAuth(identity, username, password, host), nil
}
}
return nil, nil
}
示例6: auth
// auth resolves a string of authentication mechanisms.
func (n *Email) auth(mechs string) (smtp.Auth, error) {
username := os.Getenv("SMTP_AUTH_USERNAME")
for _, mech := range strings.Split(mechs, " ") {
switch mech {
case "CRAM-MD5":
secret := os.Getenv("SMTP_AUTH_SECRET")
if secret == "" {
continue
}
return smtp.CRAMMD5Auth(username, secret), nil
case "PLAIN":
password := os.Getenv("SMTP_AUTH_PASSWORD")
if password == "" {
continue
}
identity := os.Getenv("SMTP_AUTH_IDENTITY")
// We need to know the hostname for both auth and TLS.
host, _, err := net.SplitHostPort(n.conf.Smarthost)
if err != nil {
return nil, fmt.Errorf("invalid address: %s", err)
}
return smtp.PlainAuth(identity, username, password, host), nil
}
}
return nil, nil
}
示例7: getSMTPAuth
func getSMTPAuth(hasAuth bool, mechs string) (smtp.Auth, *tls.Config, error) {
if !hasAuth {
return nil, nil, nil
}
username := os.Getenv("SMTP_AUTH_USERNAME")
for _, mech := range strings.Split(mechs, " ") {
switch mech {
case "CRAM-MD5":
secret := os.Getenv("SMTP_AUTH_SECRET")
if secret == "" {
continue
}
return smtp.CRAMMD5Auth(username, secret), nil, nil
case "PLAIN":
password := os.Getenv("SMTP_AUTH_PASSWORD")
if password == "" {
continue
}
identity := os.Getenv("SMTP_AUTH_IDENTITY")
// We need to know the hostname for both auth and TLS.
host, _, err := net.SplitHostPort(*smtpSmartHost)
if err != nil {
return nil, nil, fmt.Errorf("invalid address: %s", err)
}
auth := smtp.PlainAuth(identity, username, password, host)
cfg := &tls.Config{ServerName: host}
return auth, cfg, nil
}
}
return nil, nil, nil
}
示例8: Dial
// Dial dials and authenticates to an SMTP server. The returned SendCloser
// should be closed when done using it.
func (d *Dialer) Dial() (SendCloser, error) {
conn, err := netDial("tcp", addr(d.Host, d.Port))
if err != nil {
return nil, err
}
if d.SSL {
conn = tlsClient(conn, d.tlsConfig())
}
c, err := smtpNewClient(conn, d.Host)
if err != nil {
return nil, err
}
if d.LocalName != "" {
if err := c.Hello(d.LocalName); err != nil {
return nil, err
}
}
if !d.SSL {
if ok, _ := c.Extension("STARTTLS"); ok {
if err := c.StartTLS(d.tlsConfig()); err != nil {
c.Close()
return nil, err
}
}
}
if d.Auth == nil && d.Username != "" {
if ok, auths := c.Extension("AUTH"); ok {
if strings.Contains(auths, "CRAM-MD5") {
d.Auth = smtp.CRAMMD5Auth(d.Username, d.Password)
} else if strings.Contains(auths, "LOGIN") &&
!strings.Contains(auths, "PLAIN") {
d.Auth = &loginAuth{
username: d.Username,
password: d.Password,
host: d.Host,
}
} else {
d.Auth = smtp.PlainAuth("", d.Username, d.Password, d.Host)
}
}
}
if d.Auth != nil {
if err = c.Auth(d.Auth); err != nil {
c.Close()
return nil, err
}
}
return &smtpSender{c}, nil
}
示例9: Mail
func (sm SMTPMailer) Mail(to, from string, msg []byte) error {
var auth smtp.Auth
if sm.UseCRAMMD5 {
auth = smtp.CRAMMD5Auth(sm.Username, sm.Password)
} else {
auth = smtp.PlainAuth("", sm.Username, sm.Password, sm.Host)
}
return smtp.SendMail(sm.Addr, auth, from, []string{to}, msg)
}
示例10: Get
func (ec *EmailConfig) Get(cfg *ServerConfig) (*templates.Templater, emails.Deliverer, error) {
proto.DefaultCommonEmailParams = *cfg.CommonEmailParams
localDomain := cfg.CommonEmailParams.EmailDomain
cfg.CommonEmailParams.CommonData.LocalDomain = localDomain
// Load templates and configure email sender.
templater := &templates.Templater{}
// TODO: replace -static with a better sense of a static root
if errs := templater.Load(filepath.Join(cfg.StaticPath, "..", "email")); errs != nil {
return nil, nil, errs[0]
}
// Verify templates.
if errs := proto.ValidateEmailTemplates(templater); errs != nil {
for _, err := range errs {
fmt.Printf("error: %s\n", err)
}
return nil, nil, fmt.Errorf("template validation failed: %s...", errs[0].Error())
}
// Set up deliverer.
fmt.Printf("setting up deliverer for %#v\n", ec)
switch ec.Server {
case "":
return templater, nil, nil
case "$stdout":
return templater, &mockDeliverer{Writer: os.Stdout}, nil
default:
var sslHost string
if ec.UseTLS {
var err error
sslHost, _, err = net.SplitHostPort(ec.Server)
if err != nil {
return nil, nil, err
}
}
var auth smtp.Auth
switch strings.ToUpper(ec.AuthMethod) {
case "":
case "CRAM-MD5":
auth = smtp.CRAMMD5Auth(ec.Username, ec.Password)
case "PLAIN":
if !ec.UseTLS {
return nil, nil, fmt.Errorf("PLAIN authentication requires TLS")
}
auth = smtp.PlainAuth(ec.Identity, ec.Username, ec.Password, sslHost)
}
deliverer := emails.NewSMTPDeliverer(localDomain, ec.Server, sslHost, auth)
return templater, deliverer, nil
}
}
示例11: AuthMechanism
func (c *Client) AuthMechanism(logger lager.Logger) smtp.Auth {
switch c.config.AuthMechanism {
case AuthCRAMMD5:
c.PrintLog(logger, "crammd5-authentication")
return smtp.CRAMMD5Auth(c.config.User, c.config.Secret)
case AuthPlain:
c.PrintLog(logger, "plain-authentication")
return smtp.PlainAuth("", c.config.User, c.config.Pass, c.config.Host)
default:
c.PrintLog(logger, "no-authentication")
return nil
}
}
示例12: NewSmtpAlerter
func NewSmtpAlerter() (Alerter, error) {
envHost := os.Getenv("SMTP_HOST")
envPort := os.Getenv("SMTP_PORT")
envAuth := os.Getenv("SMTP_AUTH")
envUsername := os.Getenv("SMTP_USERNAME")
envPassword := os.Getenv("SMTP_PASSWORD")
envIdentity := os.Getenv("SMTP_PLAIN_IDENTITY")
envTo := os.Getenv("SMTP_RECIPIENT")
envFrom := os.Getenv("SMTP_FROM")
if envHost == "" {
return nil, fmt.Errorf("Please define SMTP_HOST env var")
}
if envPort == "" {
return nil, fmt.Errorf("Please define SMTP_PORT env var")
}
if envAuth != "" && envAuth != "MD5" && envAuth != "PLAIN" {
return nil, fmt.Errorf("Please either leave SMTP_AUTH env empty or set it to MD5 or PLAIN")
}
var addr string
if envPort != "" {
addr = net.JoinHostPort(envHost, envPort)
} else {
addr = envHost
}
var auth smtp.Auth
if envAuth == "MD5" {
auth = smtp.CRAMMD5Auth(envUsername, envPassword)
} else if envAuth == "PLAIN" {
auth = smtp.PlainAuth(envIdentity, envUsername, envPassword, envHost)
}
message := ezmail.NewMessage()
message.SetFrom("", envFrom)
for _, v := range strings.Split(envTo, ";") {
message.AddTo("", v)
}
smtp := &smtpAlerter{
addr: addr,
auth: auth,
message: *message}
return smtp, nil
}
示例13: Get
func (ec *EmailConfig) Get(cfg *ServerConfig) (emails.Emailer, error) {
proto.DefaultCommonEmailParams = cfg.CommonEmailParams
localDomain := cfg.CommonEmailParams.EmailDomain
cfg.CommonEmailParams.CommonData.LocalDomain = localDomain
if ec.Server == "" {
return &emails.TestEmailer{}, nil
}
var sslHost string
if ec.UseTLS {
var err error
sslHost, _, err = net.SplitHostPort(ec.Server)
if err != nil {
return nil, err
}
}
var auth smtp.Auth
switch strings.ToUpper(ec.AuthMethod) {
case "":
case "CRAM-MD5":
auth = smtp.CRAMMD5Auth(ec.Username, ec.Password)
case "PLAIN":
if !ec.UseTLS {
return nil, fmt.Errorf("PLAIN authentication requires TLS")
}
auth = smtp.PlainAuth(ec.Identity, ec.Username, ec.Password, sslHost)
}
// Load templates and configure email sender.
emailer, err := emails.NewSMTPEmailer(filepath.Join(cfg.StaticPath, "email"), localDomain, ec.Server, sslHost, auth)
if err != nil {
return nil, err
}
// Verify templates.
if errs := proto.ValidateEmailTemplates(emailer.Templater); errs != nil {
for _, err := range errs {
fmt.Printf("error: %s\n", err)
}
return nil, fmt.Errorf("template validation failed: %s...", errs[0].Error())
}
return emailer, nil
}
示例14: sendSecretEmail
func sendSecretEmail(user *model.User, secret string) {
email := user.Email
msg := `Subject: Your account at the Character Sheets Translator
Content-Type: text/plain; charset="UTF-8"
To set your password, click here:
http://%s/account/reclaim?email=%s&secret=%s
`
msg = fmt.Sprintf(msg, hostname, email, secret)
to := []string{user.Email}
fmt.Println("Sending message to", user.Email, "\n", msg)
err := smtp.SendMail("localhost:25", smtp.CRAMMD5Auth("[email protected]", "password"), "[email protected]", to, []byte(msg))
if err != nil {
fmt.Println("Error sending mail:", err)
}
}
示例15: notify
/*
* sends email when site status changes
*/
func notify(status string, site *Site) {
api_key := os.Getenv("POSTMARK_API_KEY")
mail_server := os.Getenv("POSTMARK_SMTP_SERVER") + ":25"
auth := smtp.CRAMMD5Auth(
api_key,
api_key,
)
from := config.From
stat := http.StatusText(site.Status)
time := time.Now().Format("Mon Jan 02 15:04:05 2006")
body := "<center><h3>" + site.Url + " is " + status + "</h3>"
body += fmt.Sprintf("<p><strong>%s (%d)</strong> @ %s</p>", stat, site.Status, time)
body += "<br><br></center>"
header := make(map[string]string)
header["From"] = from
header["To"] = config.Recipient
header["Subject"] = "Monitor - " + site.Url + " - " + status
header["Content-Type"] = "text/html; 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))
err := smtp.SendMail(
mail_server,
auth,
from,
[]string{config.Recipient},
[]byte(message),
)
if err != nil {
fmt.Println("Unable to notify via email")
fmt.Println(err)
}
}