-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit cbc78ad
Showing
8 changed files
with
248 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Alex Arce | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# O365Sender | ||
Office 365 Mail tester | ||
|
||
#### Compilation | ||
|
||
Clone the repository: | ||
`git clone https://github.com/alex-arce/O365Sender` | ||
|
||
Build the binary: | ||
``` | ||
cd cmd | ||
./build.sh | ||
``` | ||
|
||
#### Usage | ||
``` | ||
./O365Sender -config o365sender.conf --from="[email protected]" --to="[email protected]" --subject="TEST SUBJECT" --text="TEST BODY" | ||
``` |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
go build -o O365Sender |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
o365 "github.com/alex-arce/O365Sender" | ||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
var configFilePath string | ||
|
||
var mailServerCfg *o365.MailServerConfig | ||
|
||
var from, to, subject, text string | ||
|
||
//var filenameToAttach string | ||
|
||
func main() { | ||
fmt.Println("[O365 Mail sender]") | ||
|
||
flag.StringVar(&configFilePath, "config", "o365sender.conf", "config location") | ||
|
||
flag.StringVar(&from, "from", "", "FROM address") | ||
flag.StringVar(&to, "to", "", "TO address") | ||
flag.StringVar(&subject, "subject", "", "Subject") | ||
flag.StringVar(&text, "text", "", "Mail text") | ||
//flag.StringVar(&filenameToAttach, "filename", "", "filename to add") | ||
|
||
flag.Parse() | ||
|
||
if _, err := toml.DecodeFile(configFilePath, &mailServerCfg); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
o365Client := o365.NewMailClient(mailServerCfg) | ||
|
||
o365Client.AddFrom(from) | ||
o365Client.AddTo(to) | ||
o365Client.AddSubject(subject) | ||
o365Client.AddBody(text) | ||
|
||
sendError := o365Client.Send() | ||
if sendError != nil { | ||
fmt.Errorf("[-] ERROR sending mail :-(") | ||
} | ||
|
||
fmt.Println("[+] Mail Sent!!") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Username = "" | ||
Password = "" | ||
Host = "outlook.office365.com" | ||
Port = "587" |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/alex-arce/O365Sender | ||
|
||
go 1.15 | ||
|
||
require github.com/BurntSushi/toml v0.3.1 |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= | ||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= |
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package o365 | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/smtp" | ||
) | ||
|
||
type MailServerConfig struct { | ||
Username string | ||
Password string | ||
Host string | ||
Port string | ||
} | ||
|
||
func NewMailServerConfig(username, password, host, port string) *MailServerConfig { | ||
return &MailServerConfig{ | ||
Username: username, | ||
Password: password, | ||
Host: host, | ||
Port: port, | ||
} | ||
} | ||
|
||
type Attachment struct { | ||
filename string | ||
data []byte | ||
} | ||
|
||
type O365Client struct { | ||
mailServerConfig *MailServerConfig | ||
fromAddress string | ||
toAddress []string | ||
subject string | ||
body []byte | ||
attachments []Attachment | ||
} | ||
|
||
func NewMailClient(cfgMail *MailServerConfig) *O365Client { | ||
return &O365Client{ | ||
mailServerConfig: cfgMail, | ||
fromAddress: "", | ||
toAddress: nil, | ||
subject: "", | ||
body: nil, | ||
attachments: nil, | ||
} | ||
} | ||
|
||
//------------------------------------------------- | ||
|
||
type loginAuth struct { | ||
username, password string | ||
} | ||
|
||
func LoginAuth(username, password string) smtp.Auth { | ||
return &loginAuth{username, password} | ||
} | ||
|
||
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { | ||
return "LOGIN", []byte(a.username), nil | ||
} | ||
|
||
func (a *loginAuth) 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 from server") | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
//------------------------------------------------- | ||
|
||
func (oc *O365Client) AddFrom(fromAddress string) { | ||
oc.fromAddress = fromAddress | ||
} | ||
|
||
func (oc *O365Client) AddTo(toAdd string) { | ||
oc.toAddress = append(oc.toAddress, toAdd) | ||
} | ||
|
||
func (oc *O365Client) AddSubject(subject string) { | ||
oc.subject = subject | ||
} | ||
|
||
func (oc *O365Client) AddBody(content string) { | ||
var body bytes.Buffer | ||
mimeHeaders := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" | ||
body.Write([]byte(fmt.Sprintf("Subject: %s\n%s\n\n", oc.subject, mimeHeaders))) | ||
body.Write([]byte(content)) | ||
oc.body = body.Bytes() | ||
} | ||
|
||
func (oc *O365Client) AddAttachment(filename string) { | ||
// TODO | ||
} | ||
|
||
func (oc *O365Client) Send() error { | ||
server := fmt.Sprintf("%s:%s", oc.mailServerConfig.Host, oc.mailServerConfig.Port) | ||
|
||
conn, err := net.Dial("tcp", server) | ||
if err != nil { | ||
println(err) | ||
return err | ||
} | ||
|
||
c, err := smtp.NewClient(conn, oc.mailServerConfig.Host) | ||
if err != nil { | ||
println(err) | ||
return err | ||
} | ||
|
||
tlsconfig := &tls.Config{ | ||
ServerName: oc.mailServerConfig.Host, | ||
} | ||
|
||
if err = c.StartTLS(tlsconfig); err != nil { | ||
println(err) | ||
return err | ||
} | ||
|
||
auth := LoginAuth(oc.mailServerConfig.Username, oc.mailServerConfig.Password) | ||
if err = c.Auth(auth); err != nil { | ||
println(err) | ||
return err | ||
} | ||
|
||
var body bytes.Buffer | ||
mimeHeaders := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" | ||
body.Write([]byte(fmt.Sprintf("Subject: %s\n%s\n\n", oc.subject, mimeHeaders))) | ||
err = smtp.SendMail(oc.mailServerConfig.Host+":"+oc.mailServerConfig.Port, auth, oc.fromAddress, oc.toAddress, body.Bytes()) | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |