Last active
April 11, 2020 10:54
-
-
Save nomkhonwaan/29284d50dd73da7550fa21072ca10e7a to your computer and use it in GitHub Desktop.
prototype-pattern-ใน-go-5e9088fc0e16fb18c1d56c13
This file contains hidden or 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
package main | |
import ( | |
"net/smtp" | |
"strings" | |
) | |
func main() { | |
s := NewNoAuthSender("localhost:25") | |
err := s.From("[email protected]"). | |
To([]string{"admin@localhost"}). | |
Subject("HTML<ipsum> is awesome!"). | |
HTML(`<h1>HTML Ipsum Presents</h1> | |
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p> | |
<h2>Header Level 2</h2> | |
<ol> | |
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> | |
<li>Aliquam tincidunt mauris eu risus.</li> | |
</ol> | |
<blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote> | |
<h3>Header Level 3</h3> | |
<ul> | |
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> | |
<li>Aliquam tincidunt mauris eu risus.</li> | |
</ul> | |
<pre><code> | |
#header h1 a { | |
display: block; | |
width: 300px; | |
height: 80px; | |
} | |
</code></pre>`). | |
Send() | |
if err != nil { | |
panic(err) | |
} | |
} | |
// Sender is a Prototype Pattern supported interface for sending email | |
type Sender interface { | |
From(addr string) Sender | |
To(rcpt []string) Sender | |
Subject(subj string) Sender | |
Text(content string) Sender | |
HTML(content string) Sender | |
Send() error | |
} | |
// NoAuthSender is an SMTP client that isn't support the authentication | |
type NoAuthSender struct { | |
addr string | |
from string | |
to []string | |
subject string | |
content string | |
mime string | |
} | |
// NewNoAuthSender returns a new NoAuthSender instance | |
func NewNoAuthSender(addr string) NoAuthSender { | |
return NoAuthSender{addr: addr} | |
} | |
// From sets from email address | |
func (a NoAuthSender) From(addr string) Sender { | |
a.from = addr | |
return a | |
} | |
// To sets one or more recipient addresses | |
func (a NoAuthSender) To(rcpt []string) Sender { | |
a.to = rcpt | |
return a | |
} | |
// Subject sets subject | |
func (a NoAuthSender) Subject(subj string) Sender { | |
a.subject = subj | |
return a | |
} | |
// Text sets its content in plain text message | |
func (a NoAuthSender) Text(content string) Sender { | |
a.mime = strings.Join([]string{ | |
"MIME-version: 1.0;", | |
`Content-Type: text/plain; charset="UTF-8";`, | |
"", | |
}, "\n") | |
a.content = content | |
return a | |
} | |
// HTML sets its content in HTML message | |
func (a NoAuthSender) HTML(content string) Sender { | |
a.mime = strings.Join([]string{ | |
"MIME-version: 1.0;", | |
`Content-Type: text/html; charset="UTF-8";`, | |
"", | |
}, "\n") | |
a.content = content | |
return a | |
} | |
// Send executes smtp.SendMail function for sending an email message | |
func (a NoAuthSender) Send() error { | |
msg := strings.Join([]string{ | |
"Subject: " + a.subject, | |
a.mime, | |
a.content, | |
}, "\n") | |
return smtp.SendMail(a.addr, nil, a.from, a.to, []byte(msg)) | |
} |
This file contains hidden or 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
// NoAuthSender is an SMTP client that isn't support the authentication | |
type NoAuthSender struct { | |
addr string | |
from string | |
to []string | |
subject string | |
content string | |
mime string | |
} | |
// NewNoAuthSender returns a new NoAuthSender instance | |
func NewNoAuthSender(addr string) NoAuthSender { | |
return NoAuthSender{addr: addr} | |
} | |
// From sets from email address | |
func (a NoAuthSender) From(addr string) Sender { | |
a.from = addr | |
return a | |
} | |
// To sets one or more recipient addresses | |
func (a NoAuthSender) To(rcpt []string) Sender { | |
a.to = rcpt | |
return a | |
} | |
// Subject sets subject | |
func (a NoAuthSender) Subject(subj string) Sender { | |
a.subject = subj | |
return a | |
} | |
// Text sets its content in plain text message | |
func (a NoAuthSender) Text(content string) Sender { | |
a.mime = strings.Join([]string{ | |
"MIME-version: 1.0;", | |
`Content-Type: text/plain; charset="UTF-8";`, | |
"", | |
}, "\n") | |
a.content = content | |
return a | |
} | |
// HTML sets its content in HTML message | |
func (a NoAuthSender) HTML(content string) Sender { | |
a.mime = strings.Join([]string{ | |
"MIME-version: 1.0;", | |
`Content-Type: text/html; charset="UTF-8";`, | |
"", | |
}, "\n") | |
a.content = content | |
return a | |
} | |
// Send executes smtp.SendMail function for sending an email message | |
func (a NoAuthSender) Send() error { | |
msg := strings.Join([]string{ | |
"Subject: " + a.subject, | |
a.mime, | |
a.content, | |
}, "\n") | |
return smtp.SendMail(a.addr, nil, a.from, a.to, []byte(msg)) | |
} |
This file contains hidden or 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
// Sender is a Prototype Pattern supported interface for sending email | |
type Sender interface { | |
From(addr string) Sender | |
To(rcpt []string) Sender | |
Subject(subj string) Sender | |
Text(content string) Sender | |
HTML(content string) Sender | |
Send() error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment