├── README ├── LICENSE ├── ews.go └── createitem.go /README: -------------------------------------------------------------------------------- 1 | very dumb, hacky, possibly flaky package to send emails from an Exchange server via EWS (in the event yours doesn't expose a SMTP server) 2 | 3 | usage: 4 | 5 | b, err := ews.BuildTextEmail( 6 | "me@server.com", 7 | []string{"friend@example.com", "someone@else.com"}, 8 | "An email subject", 9 | []byte("The email body, as plain text")) 10 | if err != nil { 11 | // handle err 12 | } 13 | resp, err := ews.Issue( 14 | "https://exhange.server.com/ews/Exchange.asmx", 15 | "domain\\username", 16 | "password", 17 | b) 18 | if err != nil { 19 | // handle err 20 | } 21 | if resp.StatusCode != 200 { 22 | // read body and figure out what happened 23 | } 24 | // read or ignore body; the email was sent 25 | 26 | the other exported types are just the raw data structures for the request XML; you can ignore them 27 | 28 | I'm not sure if I'll develop this further; feel free to (warning: here be SOAP) 29 | some resources I used are in comments in the code 30 | 31 | TODOs 32 | - figure out why UTF-8 isn't used for email bodies, or how to force the encoding 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Pietro Gagliardi 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | 9 | (this is called the MIT License or Expat License; see http://www.opensource.org/licenses/MIT) 10 | -------------------------------------------------------------------------------- /ews.go: -------------------------------------------------------------------------------- 1 | // 26 august 2016 2 | package ews 3 | 4 | import ( 5 | "bytes" 6 | "net/http" 7 | ) 8 | 9 | // https://msdn.microsoft.com/en-us/library/office/dd877045(v=exchg.140).aspx 10 | // https://arvinddangra.wordpress.com/2011/09/29/send-email-using-exchange-smtp-and-ews-exchange-web-service/ 11 | // https://msdn.microsoft.com/en-us/library/office/dn789003(v=exchg.150).aspx 12 | 13 | var soapheader = ` 14 | 15 | 16 | 17 | 18 | 19 | ` 20 | 21 | func Issue(ewsAddr string, username string, password string, body []byte) (*http.Response, error) { 22 | b2 := []byte(soapheader) 23 | b2 = append(b2, body...) 24 | b2 = append(b2, "\n \n"...) 25 | req, err := http.NewRequest("POST", ewsAddr, bytes.NewReader(b2)) 26 | if err != nil { 27 | return nil, err 28 | } 29 | req.SetBasicAuth(username, password) 30 | req.Header.Set("Content-Type", "text/xml") 31 | client := new(http.Client) 32 | client.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse } 33 | return client.Do(req) 34 | } 35 | -------------------------------------------------------------------------------- /createitem.go: -------------------------------------------------------------------------------- 1 | // 26 august 2016 2 | package ews 3 | 4 | import ( 5 | "encoding/xml" 6 | ) 7 | 8 | // https://msdn.microsoft.com/en-us/library/office/aa563009(v=exchg.140).aspx 9 | 10 | type CreateItem struct { 11 | XMLName struct{} `xml:"m:CreateItem"` 12 | MessageDisposition string `xml:"MessageDisposition,attr"` 13 | SavedItemFolderId SavedItemFolderId `xml:"m:SavedItemFolderId"` 14 | Items Messages `xml:"m:Items"` 15 | } 16 | 17 | type Messages struct { 18 | Message []Message `xml:"t:Message"` 19 | } 20 | 21 | type SavedItemFolderId struct { 22 | DistinguishedFolderId DistinguishedFolderId `xml:"t:DistinguishedFolderId"` 23 | } 24 | 25 | type DistinguishedFolderId struct { 26 | Id string `xml:"Id,attr"` 27 | } 28 | 29 | type Message struct { 30 | ItemClass string `xml:"t:ItemClass"` 31 | Subject string `xml:"t:Subject"` 32 | Body Body `xml:"t:Body"` 33 | Sender OneMailbox `xml:"t:Sender"` 34 | ToRecipients XMailbox `xml:"t:ToRecipients"` 35 | } 36 | 37 | type Body struct { 38 | BodyType string `xml:"BodyType,attr"` 39 | Body []byte `xml:",chardata"` 40 | } 41 | 42 | type OneMailbox struct { 43 | Mailbox Mailbox `xml:"t:Mailbox"` 44 | } 45 | 46 | type XMailbox struct { 47 | Mailbox []Mailbox `xml:"t:Mailbox"` 48 | } 49 | 50 | type Mailbox struct { 51 | EmailAddress string `xml:"t:EmailAddress"` 52 | } 53 | 54 | func BuildTextEmail(from string, to []string, subject string, body []byte) ([]byte, error) { 55 | c := new(CreateItem) 56 | c.MessageDisposition = "SendAndSaveCopy" 57 | c.SavedItemFolderId.DistinguishedFolderId.Id = "sentitems" 58 | m := new(Message) 59 | m.ItemClass = "IPM.Note" 60 | m.Subject = subject 61 | m.Body.BodyType = "Text" 62 | m.Body.Body = body 63 | m.Sender.Mailbox.EmailAddress = from 64 | mb := make([]Mailbox, len(to)) 65 | for i, addr := range to { 66 | mb[i].EmailAddress = addr 67 | } 68 | m.ToRecipients.Mailbox = append(m.ToRecipients.Mailbox, mb...) 69 | c.Items.Message = append(c.Items.Message, *m) 70 | return xml.MarshalIndent(c, "", " ") 71 | } 72 | --------------------------------------------------------------------------------