Created
November 6, 2017 16:16
-
-
Save xplodwild/73c6b60753e36dd503b338c0f3cc7fd1 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"io" | |
"log" | |
"mime" | |
"mime/multipart" | |
"net/mail" | |
"strings" | |
"net/textproto" | |
"bufio" | |
"io/ioutil" | |
"fmt" | |
) | |
const mailHeader = "Return-Path: <[email protected]>\r\n" + | |
"Delivered-To: [email protected]\r\n" + | |
"To: [email protected]\r\n" + | |
"Subject: =?utf-8?Q?=5bSlack=5d_Notifications_from_the_redacted_Team_team_for_Mar_=32nd=2c_=32=30=31=37_at__=37=3a=35=39_PM?=\r\n" + | |
"From: \"Slack\" <[email protected]>\r\n" + | |
"Reply-To: [email protected]\r\n" + | |
"MIME-Version: 1.0\r\n" + | |
"Content-Type: multipart/alternative; boundary=\"__slack_1715508436__\"\r\n" + | |
"Date: Thu, 2 Mar 2017 10:59:08 -0800 (PST)\r\n" + | |
"\r\n" + | |
"\r\n" | |
const mailBody = "--__slack_1715508436__\r\n" + | |
"Content-Type: text/plain; charset=\"utf-8\"\r\n" + | |
"Content-Transfer-Encoding: quoted-printable\r\n" + | |
"Content-Disposition: inline\r\n" + | |
"\r\n" + | |
"Cat Fact 20: Cats are often lactose intolerant, so stop givin’ them\r\n" + | |
"milk!\r\n" + | |
"\r\n" + | |
":cat: :cat: :cat:\r\n" + | |
"\r\n" + | |
"--__slack_1715508436__\r\n" + | |
"Content-Type: text/html; charset=\"utf-8\"\r\n" + | |
"Content-Disposition: inline\r\n" + | |
"\r\n" + | |
"Cat Fact 20: Cats are often lactose intolerant, so stop givin’ them milk!<br><br><img src=\"https://a.slack-edge.com/d4bf/img/emoji_2015_2/apple/1f431.png\" title=\":cat:\" width=\"20\" height=\"20\" style=\"-ms-interpolation-mode: bicubic; outline: none; text-decoration: none; vertical-align: bottom;\"><img src=\"https://a.slack-edge.com/d4bf/img/emoji_2015_2/apple/1f431.png\" title=\":cat:\" width=\"20\" height=\"20\" style=\"-ms-interpolation-mode: bicubic; outline: none; text-decoration: none; vertical-align: bottom;\"><img src=\"https://a.slack-edge.com/d4bf/img/emoji_2015_2/apple/1f431.png\" title=\":cat:\" width=\"20\" height=\"20\" style=\"-ms-interpolation-mode: bicubic; outline: none; text-decoration: none; vertical-align: bottom;\"></div>\r\n\r\n" + | |
"--__slack_1715508436__--\r\n" | |
func main() { | |
tp := textproto.NewReader(bufio.NewReader(strings.NewReader(mailHeader + mailBody))) | |
mimeHeader, err := tp.ReadMIMEHeader() | |
if err != nil { | |
panic(err) | |
} | |
msg := &mail.Message{ | |
Header: map[string][]string(mimeHeader), | |
Body: strings.NewReader(mailHeader + mailBody), | |
} | |
mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type")) | |
if err != nil { | |
log.Fatalf("Failed to parse mediaType: %s", err) | |
} | |
if strings.HasPrefix(mediaType, "multipart/") { | |
mr := multipart.NewReader(msg.Body, params["boundary"]) | |
for { | |
p, err := mr.NextPart() | |
if err == io.EOF { | |
return | |
} | |
if err != nil { | |
log.Fatalf("Error parsing NextPart: %s", err) | |
} | |
slurp, err := ioutil.ReadAll(p) | |
if err != nil { | |
log.Fatalf("Failed to read all part: %s", err) | |
} | |
fmt.Printf("Part %q: %q\n", p.Header.Get("Foo"), slurp) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment