Skip to content

Commit

Permalink
支持SSL端口发送邮件
Browse files Browse the repository at this point in the history
  • Loading branch information
Rvn0xsy committed Jan 14, 2022
1 parent fcf1bae commit 5edb9b9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/Rvn0xsy/SMTP-NC

go 1.17

require github.com/gin-gonic/gin v1.7.7
require (
github.com/gin-gonic/gin v1.7.7
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
)

require (
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
Expand Down
68 changes: 64 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"crypto/tls"
"embed"
"flag"
"fmt"
"github.com/gin-gonic/gin"
"html/template"
"io/fs"
"log"
"net"
"net/http"
"net/smtp"
"os"
Expand All @@ -25,17 +27,75 @@ type ProcessData struct {
Port string `json:"port"`
To []string `json:"to"`
Body string `json:"body"`
EnableSSL bool `json:"enable_ssl"`
}

func SendSMTP(Data * ProcessData){
auth := smtp.PlainAuth("", Data.Username, Data.Password, Data.Hostname)
msg := []byte(Data.Body)
err := smtp.SendMail(Data.Hostname +":" + Data.Port, auth, Data.SMTPFrom, Data.To, msg)
if err != nil {
log.Println(err)
}else {
servername := Data.Hostname +":" + Data.Port
if !Data.EnableSSL {
err := smtp.SendMail(servername, auth, Data.SMTPFrom, Data.To, msg)
if err != nil {
log.Println(err)
}else {
log.Println("Send Success!")
}
}else{
host, _, _ := net.SplitHostPort(servername)
// TLS config
tlsconfig := &tls.Config {
InsecureSkipVerify: true,
ServerName: host,
}
conn, err := tls.Dial("tcp", servername, tlsconfig)
if err != nil {
log.Println(err)
return
}
c, err := smtp.NewClient(conn, host)
if err != nil {
log.Println(err)
return
}

// Auth
if err = c.Auth(auth); err != nil {
log.Println(err)
return
}

// To && From
if err = c.Mail(Data.SMTPFrom); err != nil {
log.Println(err)
return
}

if err = c.Rcpt(Data.To[0]); err != nil {
log.Println(err)
return
}

// Data
w, err := c.Data()
if err != nil {
log.Println(err)
return
}
_, err = w.Write(msg)
if err != nil {
log.Println(err)
return
}
err = w.Close()
if err != nil {
log.Println(err)
return
}
c.Quit()
log.Println("Send Success!")
}

}

func init() {
Expand Down
14 changes: 11 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ <h1>SMTP NetCat</h1>
<div class="container">
<div class="row">
<div class="col-md-6">
<p>
SMTP PORT: 25 <br>
SMTP SSL PORT: 465
</p>
<hr>
<div class="form-inline form-group">
<label for="hostname">Hostname: </label>
<input type="text" class="form-control" id="hostname" placeholder="smtp.163.com">
<input type="text" class="form-control" id="hostname" placeholder="smtp.163.com" width="60%">
<label for="port">Port: </label>
<input type="number" class="form-control" id="port" placeholder="25">
<input type="number" class="form-control" id="port" placeholder="25" style="width: 20%">
<label for="enable_ssl"></label>
<input type="checkbox" class="form-control custom-checkbox" id="enable_ssl">Enable SSL
</div>

<hr>
<div class="form-group">
<label for="email">Email address: </label>
<input type="email" class="form-control" id="email" placeholder="">
Expand Down Expand Up @@ -71,6 +78,7 @@ <h1>SMTP NetCat</h1>
ObjData.username = $("#email").val()
ObjData.password = $("#pwd").val()
ObjData.to = $("#to").val()
ObjData.enable_ssl = $("#enable_ssl").is(":checked")
mailList = ObjData.to.split(",")
ObjData.to = mailList
ObjData.smtp_from = $("#smtp_from").val()
Expand Down

0 comments on commit 5edb9b9

Please sign in to comment.