Module: EWS::Mail

Defined in:
lib/ews_mail.rb

Overview

Mail module

Constant Summary collapse

WSDL =

URL of EWS WSDL

'https://outlook.office365.com/EWS/Services.wsdl'
ENDPOINT =

Endpoint against which we want to run requests

'https://outlook.office365.com/EWS/Exchange.asmx'
NAMESPACES =

Namespaces to add to the request

{
	'xmlns:t' =>
		'http://schemas.microsoft.com/exchange/services/2006/types'
}

Class Method Summary collapse

Class Method Details

.send(username:, password:, rcpts:, subject:, body:) ⇒ Object

Send mail via EWS.

Senders is determined automatically by EWS based on your username.

Example

EWS::Mail.send( username: ‘<USERNAME>’, password: ‘<PASSWORD>’, rcpts: ‘<RECIPIENT>’, subject: ‘<SUBJECT>’, body: ‘<PLAINTEXT-BODY>’ ) EWS::Mail.send( username: ‘<USERNAME>’, password: ‘<PASSWORD>’, rcpts: [‘<RECIPIENT1>’, ‘<RECIPIENT2>’], subject: ‘<SUBJECT>’, body: ‘<PLAINTEXT-BODY>’ )



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ews_mail.rb', line 39

def self.send(username:, password:, rcpts:, subject:, body:)
	c = Savon.client({
		wsdl: WSDL,
		endpoint: ENDPOINT,
		basic_auth: [username, password],
		namespaces: NAMESPACES,
	})

	message = {
		'tns:Items' => {
			't:Message' => {
				't:Subject' => subject,
				't:Body' => body,
				't:ToRecipients' => {
					't:Mailbox' => [*rcpts].map do |v|
						{ 't:EmailAddress' => v }
					end
				},
				attributes!: {
					't:Body' => { 'BodyType' => 'Text' }
				}
			}
		},
	}
	attributes = { 'MessageDisposition' => 'Send' }

	c.call(:create_item, message: message, attributes: attributes)
end