mirror of
				https://github.com/linux-do/new-api.git
				synced 2025-11-04 13:23:42 +08:00 
			
		
		
		
	[fix] fix send email error using outlook smtp
This commit is contained in:
		
							
								
								
									
										32
									
								
								common/email-outlook-auth.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								common/email-outlook-auth.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
package common
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net/smtp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type outlookAuth struct {
 | 
			
		||||
	username, password string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func LoginAuth(username, password string) smtp.Auth {
 | 
			
		||||
	return &outlookAuth{username, password}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *outlookAuth) Start(_ *smtp.ServerInfo) (string, []byte, error) {
 | 
			
		||||
	return "LOGIN", []byte{}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *outlookAuth) Next(fromServer []byte, more bool) ([]byte, error) {
 | 
			
		||||
	if more {
 | 
			
		||||
		switch string(fromServer) {
 | 
			
		||||
		case "Username:":
 | 
			
		||||
			return []byte(a.username), nil
 | 
			
		||||
		case "Password:":
 | 
			
		||||
			return []byte(a.password), nil
 | 
			
		||||
		default:
 | 
			
		||||
			return nil, errors.New("unknown fromServer")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -62,6 +62,9 @@ func SendEmail(subject string, receiver string, content string) error {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	} else if strings.HasSuffix(SMTPAccount, "outlook.com") {
 | 
			
		||||
		auth = LoginAuth(SMTPAccount, SMTPToken)
 | 
			
		||||
		err = smtp.SendMail(addr, auth, SMTPAccount, to, mail)
 | 
			
		||||
	} else {
 | 
			
		||||
		err = smtp.SendMail(addr, auth, SMTPAccount, to, mail)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user