SendGrid - Version 1.0

Version Description

  • Fixed issue: Add error message when PHP-curl extension is not enabled.

=

Download this release

Release Info

Developer team-rs
Plugin Icon 128x128 SendGrid
Version 1.0
Comparing to
See all releases

Version 1.0

assets/screenshot-1.png ADDED
Binary file
assets/screenshot-2.png ADDED
Binary file
assets/screenshot-3.png ADDED
Binary file
assets/screenshot-4.png ADDED
Binary file
assets/screenshot-5.png ADDED
Binary file
assets/screenshot-6.png ADDED
Binary file
lib/SendGridSettings.php ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class wpSendGridSettings
3
+ {
4
+ public function __construct()
5
+ {
6
+ add_action('admin_menu', array(__CLASS__, 'sendgridPluginMenu'));
7
+ }
8
+
9
+ /**
10
+ * Add settings page
11
+ */
12
+ public function sendgridPluginMenu()
13
+ {
14
+ add_options_page(__('SendGrid'), __('SendGrid'), 'manage_options', 'sendgrid-settings.php',
15
+ array(__CLASS__, 'show_settings_page'));
16
+ }
17
+
18
+ /**
19
+ * Check username/password
20
+ *
21
+ * @param string $username sendgrid username
22
+ * @param string $password sendgrid password
23
+ * @return bool
24
+ */
25
+ public static function checkUsernamePassword($username, $password)
26
+ {
27
+ $url = "https://sendgrid.com/api/profile.get.json?";
28
+ $url .= "api_user=". $username . "&api_key=" . $password;
29
+
30
+ $ch = curl_init();
31
+ curl_setopt($ch, CURLOPT_URL, $url);
32
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
33
+
34
+ $data = curl_exec($ch);
35
+ curl_close($ch);
36
+
37
+ $response = json_decode($data, true);
38
+
39
+ if (isset($response['error']))
40
+ {
41
+ return false;
42
+ }
43
+
44
+ return true;
45
+ }
46
+
47
+ /**
48
+ * Display settings page
49
+ */
50
+ public function show_settings_page()
51
+ {
52
+ if ($_SERVER['REQUEST_METHOD'] == 'POST')
53
+ {
54
+ if ($_POST['email_test'])
55
+ {
56
+ $to = $_POST['sendgrid_to'];
57
+ $subject = $_POST['sendgrid_subj'];
58
+ $body = $_POST['sendgrid_body'];
59
+ $headers = $_POST['sendgrid_headers'];
60
+ $sent = wp_mail($to, $subject, $body, $headers);
61
+ if (get_option('sendgrid_api') == 'api')
62
+ {
63
+ $sent = json_decode($sent);
64
+ if ($sent->message == "success")
65
+ {
66
+ $message = 'Email sent.';
67
+ $status = 'send-success';
68
+ }
69
+ else
70
+ {
71
+ $errors = ($sent->errors[0]) ? $sent->errors[0] : $sent;
72
+ $message = 'Email not sent. ' . $errors;
73
+ $status = 'send-failed';
74
+ }
75
+
76
+ }
77
+ elseif (get_option('sendgrid_api') == 'smtp')
78
+ {
79
+ if ($sent === true)
80
+ {
81
+ $message = 'Email sent.';
82
+ $status = 'send-success';
83
+ }
84
+ else
85
+ {
86
+ $message = 'Email not sent. ' . $sent;
87
+ $status = 'send-failed';
88
+ }
89
+ }
90
+ }
91
+ else
92
+ {
93
+ $message = 'Options saved.';
94
+ $status = 'save-success';
95
+
96
+ $user = $_POST['sendgrid_user'];
97
+ update_option('sendgrid_user', $user);
98
+
99
+ $password = $_POST['sendgrid_pwd'];
100
+ update_option('sendgrid_pwd', $password);
101
+
102
+ $method = $_POST['sendgrid_api'];
103
+ if ($method == 'smtp' and !class_exists('Swift'))
104
+ {
105
+ $message = 'You must have <a href="http://wordpress.org/plugins/swift-mailer/" target="_blank">' .
106
+ 'Swift-mailer plugin</a> installed and activated';
107
+ $status = 'save-error';
108
+ update_option('sendgrid_api', 'api');
109
+ }
110
+ else
111
+ {
112
+ update_option('sendgrid_api', $method);
113
+ }
114
+
115
+ $name = $_POST['sendgrid_name'];
116
+ update_option('sendgrid_from_name', $name);
117
+
118
+ $email = $_POST['sendgrid_email'];
119
+ update_option('sendgrid_from_email', $email);
120
+
121
+ $reply_to = $_POST['sendgrid_reply_to'];
122
+ update_option('sendgrid_reply_to', $reply_to);
123
+ }
124
+ }
125
+
126
+ $user = get_option('sendgrid_user');
127
+ $password = get_option('sendgrid_pwd');
128
+ $method = get_option('sendgrid_api');
129
+ $name = get_option('sendgrid_from_name');
130
+ $email = get_option('sendgrid_from_email');
131
+ $reply_to = get_option('sendgrid_reply_to');
132
+
133
+ if ($user and $password)
134
+ {
135
+ if (in_array('curl', get_loaded_extensions()))
136
+ {
137
+ $valid_credentials = self::checkUsernamePassword($user, $password);
138
+
139
+ if (!$valid_credentials)
140
+ {
141
+ $message = 'Invalid username/password';
142
+ $status = 'save-error';
143
+ }
144
+ }
145
+ else
146
+ {
147
+ $message = 'You must have PHP-curl extension enabled';
148
+ $status = 'save-error';
149
+ }
150
+ }
151
+
152
+ require_once dirname(__FILE__) . '/../view/sendgrid_settings.php';
153
+ }
154
+ }
lib/sendgrid-php/.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ Test/coverage/*
2
+ examples/*
3
+ dist/
4
+ vendor/*
lib/sendgrid-php/.travis.yml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ language: php
2
+ php:
3
+ - 5.4
4
+ - 5.3
5
+ before_install: composer install --prefer-source
6
+ script: make test
lib/sendgrid-php/MIT.LICENSE ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Copyright (c) 2011 SendGrid
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation
5
+ the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
6
+ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of
9
+ the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
12
+ THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
14
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
15
+ DEALINGS IN THE SOFTWARE.
lib/sendgrid-php/Makefile ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Twilio API helper library.
2
+ # See LICENSE file for copyright and license details.
3
+
4
+ define LICENSE
5
+ <?php
6
+
7
+ /**
8
+ * SendGrid API helper library.
9
+ *
10
+ * @category Services
11
+ * @package Services_SendGrid
12
+ * @license http://creativecommons.org/licenses/MIT/ MIT
13
+ * @link https://github.com/sendgrid/sendgrid-php
14
+ */
15
+ endef
16
+ export LICENSE
17
+
18
+ all: test
19
+
20
+ clean:
21
+ @rm -rf dist
22
+
23
+ PHP_FILES = `find dist -name \*.php`
24
+ dist: clean
25
+ @mkdir dist
26
+ @git archive master | (cd dist; tar xf -)
27
+ @for php in $(PHP_FILES); do\
28
+ echo "$$LICENSE" > $$php.new; \
29
+ tail -n+2 $$php >> $$php.new; \
30
+ mv $$php.new $$php; \
31
+ done
32
+
33
+ test:
34
+ @echo running tests
35
+ @phpunit --strict --colors --configuration Test/phpunit.xml
36
+
37
+ .PHONY: all clean dist test
lib/sendgrid-php/README.md ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # sendgrid-php
2
+ This library allows you to quickly and easily send emails through SendGrid using PHP.
3
+
4
+ ## Installation
5
+
6
+ There are a number of ways to install the SendGrid PHP helper library. Choose from the options outlined below:
7
+
8
+ ### Composer
9
+
10
+ The easier way to install the SendGrid php library is using [Composer](http://getcomposer.org/). Composer makes it easy
11
+ to install the library and all of its dependencies:
12
+
13
+ #### 1. Update your composer.json
14
+
15
+ If you already have a `composer.json`, just add the following to your require section:
16
+
17
+ ```json
18
+ {
19
+ "require": {
20
+ "sendgrid/sendgrid": "~1.0.0"
21
+ }
22
+ }
23
+ ```
24
+ *For more info on creating a `composer.json`, check out [this guide](http://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup).*
25
+
26
+ #### 2. Install from packagist
27
+
28
+ To install the library and it's dependencies, make sure you have [composer installed](http://getcomposer.org/doc/01-basic-usage.md#installation) and type the following:
29
+
30
+ ```bash
31
+ composer install
32
+ ```
33
+
34
+ #### 3. Include autoload.php
35
+
36
+ Now that we have everything installed, all we need to do is require it from our php script. Add the following to the top of your php script:
37
+
38
+ ```php
39
+ require 'vendor/autoload.php';
40
+ ```
41
+
42
+ This will include both the SendGrid library, and the SwiftMailer dependency.
43
+
44
+ ### Git
45
+
46
+ You can also install the package from github, although you will have to manually install the dependencies (see the section on installing dependencies below):
47
+
48
+ ```bash
49
+ git clone https://github.com/sendgrid/sendgrid-php.git
50
+ ```
51
+
52
+ And the require the autoloader from your php script:
53
+
54
+ ```php
55
+ require '../path/to/sendgrid-php/SendGrid_loader.php';
56
+ ```
57
+
58
+ ## Installing Dependenices
59
+
60
+ If you installed the library using composer or you're not planning on sending using SMTP, you can skip this section. Otherwise, you will need to install
61
+ SwiftMailer (which sendgrid-php depends on). You can install from pear using the following:
62
+
63
+ ```bash
64
+ pear channel-discover pear.swiftmailer.org
65
+ pear install swift/swift
66
+ ```
67
+
68
+
69
+ ## Testing ##
70
+
71
+ The existing tests in the `Test` directory can be run using [PHPUnit](https://github.com/sebastianbergmann/phpunit/) with the following command:
72
+
73
+ ````
74
+ phpunit Test/
75
+ ```
76
+
77
+ ## SendGrid APIs ##
78
+ SendGrid provides two methods of sending email: the Web API, and SMTP API. SendGrid recommends using the SMTP API for sending emails.
79
+ For an explanation of the benefits of each, refer to http://docs.sendgrid.com/documentation/get-started/integrate/examples/smtp-vs-rest/.
80
+
81
+ This library implements a common interface to make it very easy to use either API.
82
+
83
+ ## Mail Pre-Usage ##
84
+
85
+ Before we begin using the library, its important to understand a few things about the library architecture...
86
+
87
+ * The SendGrid Mail object is the means of setting mail data. In general, data can be set in three ways for most elements:
88
+ 1. set - reset the data, and initialize it to the given element. This will destroy previous data
89
+ 2. set (List) - for array based elements, we provide a way of passing the entire array in at once. This will also destroy previous data.
90
+ 3. add - append data to the list of elements.
91
+
92
+ * Sending an email is as simple as :
93
+ 1. Creating a SendGrid Instance
94
+ 1. Creating a SendGrid Mail object, and setting its data
95
+ 1. Sending the mail using either SMTP API or Web API.
96
+
97
+ ## Mail Usage ##
98
+
99
+ To begin using this library, initialize the SendGrid object with your SendGrid credentials
100
+
101
+ ```php
102
+ $sendgrid = new SendGrid('username', 'password');
103
+ ```
104
+
105
+ Create a new SendGrid Mail object and add your message details
106
+
107
+ ```php
108
+ $mail = new SendGrid\Mail();
109
+ $mail->addTo('foo@bar.com')->
110
+ setFrom('me@bar.com')->
111
+ setSubject('Subject goes here')->
112
+ setText('Hello World!')->
113
+ setHtml('<strong>Hello World!</strong>');
114
+ ```
115
+
116
+ Send it using the API of your choice (SMTP or Web)
117
+
118
+ ```php
119
+ $sendgrid->smtp->send($mail);
120
+ ```
121
+ Or
122
+
123
+ ```php
124
+ $sendgrid->web->send($mail);
125
+ ```
126
+
127
+ ### Using Categories ###
128
+
129
+ Categories are used to group email statistics provided by SendGrid.
130
+
131
+ To use a category, simply set the category name. Note: there is a maximum of 10 categories per email.
132
+
133
+ ```php
134
+ $mail = new SendGrid\Mail();
135
+ $mail->addTo('foo@bar.com')->
136
+ ...
137
+ addCategory("Category 1")->
138
+ addCategory("Category 2");
139
+ ```
140
+
141
+
142
+ ### Using Attachments ###
143
+
144
+ Attachments are currently file based only, with future plans for an in memory implementation as well.
145
+
146
+ File attachments are limited to 7 MB per file.
147
+
148
+ ```php
149
+ $mail = new SendGrid\Mail();
150
+ $mail->addTo('foo@bar.com')->
151
+ ...
152
+ addAttachment("../path/to/file.txt");
153
+ ```
154
+
155
+ ### Using From-Name and Reply-To
156
+
157
+ There are two handy helper methods for setting the From-Name and Reply-To for a
158
+ message
159
+
160
+ ```php
161
+ $mail = new SendGrid\Mail();
162
+ $mail->addTo('foo@bar.com')->
163
+ setReplyTo('someone.else@example.com')->
164
+ setFromName('John Doe')->
165
+ ...
166
+ ```
167
+
168
+ ### Using Substitutions ###
169
+
170
+ Substitutions can be used to customize multi-recipient emails, and tailor them for the user
171
+
172
+ ```php
173
+ $mail = new SendGrid\Mail();
174
+ $mail->addTo('john@somewhere.com')->
175
+ addTo("harry@somewhere.com")->
176
+ addTo("Bob@somewhere.com")->
177
+ ...
178
+ setHtml("Hey %name%, we've seen that you've been gone for a while")->
179
+ addSubstitution("%name%", array("John", "Harry", "Bob"));
180
+ ```
181
+
182
+ ### Using Sections ###
183
+
184
+ Sections can be used to further customize messages for the end users. A section is only useful in conjunction with a substition value.
185
+
186
+ ```php
187
+ $mail = new SendGrid\Mail();
188
+ $mail->addTo('john@somewhere.com')->
189
+ addTo("harry@somewhere.com")->
190
+ addTo("Bob@somewhere.com")->
191
+ ...
192
+ setHtml("Hey %name%, you work at %place%")->
193
+ addSubstitution("%name%", array("John", "Harry", "Bob"))->
194
+ addSubstitution("%place%", array("%office%", "%office%", "%home%"))->
195
+ addSection("%office%", "an office")->
196
+ addSection("%home%", "your house");
197
+ ```
198
+
199
+ ### Using Unique Arguments ###
200
+
201
+ Unique Arguments are used for tracking purposes
202
+
203
+ ```php
204
+ $mail = new SendGrid\Mail();
205
+ $mail->addTo('foo@bar.com')->
206
+ ...
207
+ addUniqueArgument("Customer", "Someone")->
208
+ addUniqueArgument("location", "Somewhere");
209
+ ```
210
+
211
+ ### Using Filter Settings ###
212
+
213
+ Filter Settings are used to enable and disable apps, and to pass parameters to those apps.
214
+
215
+ ```php
216
+ $mail = new SendGrid\Mail();
217
+ $mail->addTo('foo@bar.com')->
218
+ ...
219
+ addFilterSetting("gravatar", "enable", 1)->
220
+ addFilterSetting("footer", "enable", 1)->
221
+ addFilterSetting("footer", "text/plain", "Here is a plain text footer")->
222
+ addFilterSetting("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>");
223
+ ```
224
+
225
+ ### Using Headers ###
226
+
227
+ Headers can be used to add existing sendgrid functionality (such as for categories or filters), or custom headers can be added as necessary.
228
+
229
+ ```php
230
+ $mail = new SendGrid\Mail();
231
+ $mail->addTo('foo@bar.com')->
232
+ ...
233
+ addHeader("category", "My New Category");
234
+ ```
lib/sendgrid-php/SendGrid.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class SendGrid
4
+ {
5
+ const VERSION = "1.0.0";
6
+
7
+ protected $namespace = "SendGrid",
8
+ $username,
9
+ $password;
10
+
11
+ // Available transport mechanisms
12
+ protected $web,
13
+ $smtp;
14
+
15
+ public function __construct($username, $password)
16
+ {
17
+ $this->username = $username;
18
+ $this->password = $password;
19
+ }
20
+
21
+ public function __get($api)
22
+ {
23
+ $name = $api;
24
+
25
+ if($this->$name != null)
26
+ {
27
+ return $this->$name;
28
+ }
29
+
30
+ $api = $this->namespace . "\\" . ucwords($api);
31
+ $class_name = str_replace('\\', '/', "$api.php");
32
+ $file = __dir__ . DIRECTORY_SEPARATOR . $class_name;
33
+
34
+ if (!file_exists($file))
35
+ {
36
+ throw new Exception("Api '$class_name' not found.");
37
+ }
38
+ require_once $file;
39
+
40
+ $this->$name = new $api($this->username, $this->password);
41
+ return $this->$name;
42
+ }
43
+
44
+ }
lib/sendgrid-php/SendGrid/Api.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace SendGrid;
4
+
5
+ class Api
6
+ {
7
+
8
+ protected $username,
9
+ $password;
10
+
11
+ public function __construct($username, $password)
12
+ {
13
+ $this->username = $username;
14
+ $this->password = $password;
15
+ }
16
+
17
+ }
lib/sendgrid-php/SendGrid/Mail.php ADDED
@@ -0,0 +1,721 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace SendGrid;
4
+
5
+ class Mail
6
+ {
7
+
8
+ private $to_list,
9
+ $from,
10
+ $from_name,
11
+ $reply_to,
12
+ $cc_list,
13
+ $bcc_list,
14
+ $subject,
15
+ $text,
16
+ $html,
17
+ $attachment_list,
18
+ $header_list = array();
19
+
20
+ protected $use_headers;
21
+
22
+ public function __construct()
23
+ {
24
+ $this->from_name = false;
25
+ $this->reply_to = false;
26
+ }
27
+
28
+ /**
29
+ * _removeFromList
30
+ * Given a list of key/value pairs, removes the associated keys
31
+ * where a value matches the given string ($item)
32
+ * @param Array $list - the list of key/value pairs
33
+ * @param String $item - the value to be removed
34
+ */
35
+ private function _removeFromList(&$list, $item, $key_field = null)
36
+ {
37
+ foreach ($list as $key => $val)
38
+ {
39
+ if($key_field)
40
+ {
41
+ if($val[$key_field] == $item)
42
+ {
43
+ unset($list[$key]);
44
+ }
45
+ }
46
+ else
47
+ {
48
+ if ($val == $item)
49
+ {
50
+ unset($list[$key]);
51
+ }
52
+ }
53
+ }
54
+ //repack the indices
55
+ $list = array_values($list);
56
+ }
57
+
58
+ /**
59
+ * getTos
60
+ * Return the list of recipients
61
+ * @return list of recipients
62
+ */
63
+ public function getTos()
64
+ {
65
+ return $this->to_list;
66
+ }
67
+
68
+ /**
69
+ * setTos
70
+ * Initialize an array for the recipient 'to' field
71
+ * Destroy previous recipient 'to' data.
72
+ * @param Array $email_list - an array of email addresses
73
+ * @return the SendGrid\Mail object.
74
+ */
75
+ public function setTos(array $email_list)
76
+ {
77
+ $this->to_list = $email_list;
78
+ return $this;
79
+ }
80
+
81
+ /**
82
+ * setTo
83
+ * Initialize a single email for the recipient 'to' field
84
+ * Destroy previous recipient 'to' data.
85
+ * @param String $email - a list of email addresses
86
+ * @return the SendGrid\Mail object.
87
+ */
88
+ public function setTo($email)
89
+ {
90
+ $this->to_list = array($email);
91
+ return $this;
92
+ }
93
+
94
+ /**
95
+ * addTo
96
+ * append an email address to the existing list of addresses
97
+ * Preserve previous recipient 'to' data.
98
+ * @param String $email - a single email address
99
+ * @return the SendGrid\Mail object.
100
+ */
101
+ public function addTo($email, $name=null)
102
+ {
103
+ $this->to_list[] = ($name ? $name . "<" . $email . ">" : $email);
104
+
105
+ return $this;
106
+ }
107
+
108
+ /**
109
+ * removeTo
110
+ * remove an email address from the list of recipient addresses
111
+ * @param String $search_term - the regex value to be removed
112
+ * @return the SendGrid\Mail object.
113
+ */
114
+ public function removeTo($search_term)
115
+ {
116
+ $this->to_list = array_values(array_filter($this->to_list, function($item) use($search_term) {
117
+ return !preg_match("/" . $search_term . "/", $item);
118
+ }));
119
+ return $this;
120
+ }
121
+
122
+ /**
123
+ * getFrom
124
+ * get the from email address
125
+ * @param Boolean $as_array - return the from as an assocative array
126
+ * @return the from email address
127
+ */
128
+ public function getFrom($as_array = false)
129
+ {
130
+ if($as_array && ($name = $this->getFromName())) {
131
+ return array("$this->from" => $name);
132
+ } else {
133
+ return $this->from;
134
+ }
135
+ }
136
+
137
+ /**
138
+ * setFrom
139
+ * set the from email
140
+ * @param String $email - an email address
141
+ * @return the SendGrid\Mail object.
142
+ */
143
+ public function setFrom($email)
144
+ {
145
+ $this->from = $email;
146
+ return $this;
147
+ }
148
+
149
+ /**
150
+ * getFromName
151
+ * get the from name
152
+ * @return the from name
153
+ */
154
+ public function getFromName()
155
+ {
156
+ return $this->from_name;
157
+ }
158
+
159
+ /**
160
+ * setFromName
161
+ * set the name appended to the from email
162
+ * @param String $name - a name to append
163
+ * @return the SendGrid\Mail object.
164
+ */
165
+ public function setFromName($name)
166
+ {
167
+ $this->from_name = $name;
168
+ return $this;
169
+ }
170
+
171
+ /**
172
+ * getReplyTo
173
+ * get the reply-to address
174
+ * @return the reply to address
175
+ */
176
+ public function getReplyTo()
177
+ {
178
+ return $this->reply_to;
179
+ }
180
+
181
+ /**
182
+ * setReplyTo
183
+ * set the reply-to address
184
+ * @param String $email - the email to reply to
185
+ * @return the SendGrid\Mail object.
186
+ */
187
+ public function setReplyTo($email)
188
+ {
189
+ $this->reply_to = $email;
190
+ return $this;
191
+ }
192
+ /**
193
+ * getCc
194
+ * get the Carbon Copy list of recipients
195
+ * @return Array the list of recipients
196
+ */
197
+ public function getCcs()
198
+ {
199
+ return $this->cc_list;
200
+ }
201
+
202
+ /**
203
+ * setCcs
204
+ * Set the list of Carbon Copy recipients
205
+ * @param String $email - a list of email addresses
206
+ * @return the SendGrid\Mail object.
207
+ */
208
+ public function setCcs(array $email_list)
209
+ {
210
+ $this->cc_list = $email_list;
211
+ return $this;
212
+ }
213
+
214
+ /**
215
+ * setCc
216
+ * Initialize the list of Carbon Copy recipients
217
+ * destroy previous recipient data
218
+ * @param String $email - a list of email addresses
219
+ * @return the SendGrid\Mail object.
220
+ */
221
+ public function setCc($email)
222
+ {
223
+ $this->cc_list = array($email);
224
+ return $this;
225
+ }
226
+
227
+ /**
228
+ * addCc
229
+ * Append an address to the list of Carbon Copy recipients
230
+ * @param String $email - an email address
231
+ * @return the SendGrid\Mail object.
232
+ */
233
+ public function addCc($email)
234
+ {
235
+ $this->cc_list[] = $email;
236
+ return $this;
237
+ }
238
+
239
+ /**
240
+ * removeCc
241
+ * remove an address from the list of Carbon Copy recipients
242
+ * @param String $email - an email address
243
+ * @return the SendGrid\Mail object.
244
+ */
245
+ public function removeCc($email)
246
+ {
247
+ $this->_removeFromList($this->cc_list, $email);
248
+
249
+ return $this;
250
+ }
251
+
252
+ /**
253
+ * getBccs
254
+ * return the list of Blind Carbon Copy recipients
255
+ * @return Array - the list of Blind Carbon Copy recipients
256
+ */
257
+ public function getBccs()
258
+ {
259
+ return $this->bcc_list;
260
+ }
261
+
262
+ /**
263
+ * setBccs
264
+ * set the list of Blind Carbon Copy Recipients
265
+ * @param Array $email_list - the list of email recipients to
266
+ * @return the SendGrid\Mail object.
267
+ */
268
+ public function setBccs($email_list)
269
+ {
270
+ $this->bcc_list = $email_list;
271
+ return $this;
272
+ }
273
+
274
+ /**
275
+ * setBcc
276
+ * Initialize the list of Carbon Copy recipients
277
+ * destroy previous recipient Blind Carbon Copy data
278
+ * @param String $email - an email address
279
+ * @return the SendGrid\Mail object.
280
+ */
281
+ public function setBcc($email)
282
+ {
283
+ $this->bcc_list = array($email);
284
+ return $this;
285
+ }
286
+
287
+ /**
288
+ * addBcc
289
+ * Append an email address to the list of Blind Carbon Copy
290
+ * recipients
291
+ * @param String $email - an email address
292
+ */
293
+ public function addBcc($email)
294
+ {
295
+ $this->bcc_list[] = $email;
296
+ return $this;
297
+ }
298
+
299
+ /**
300
+ * removeBcc
301
+ * remove an email address from the list of Blind Carbon Copy
302
+ * addresses
303
+ * @param String $email - the email to remove
304
+ * @return the SendGrid\Mail object.
305
+ */
306
+ public function removeBcc($email)
307
+ {
308
+ $this->_removeFromList($this->bcc_list, $email);
309
+ return $this;
310
+ }
311
+
312
+ /**
313
+ * getSubject
314
+ * get the email subject
315
+ * @return the email subject
316
+ */
317
+ public function getSubject()
318
+ {
319
+ return $this->subject;
320
+ }
321
+
322
+ /**
323
+ * setSubject
324
+ * set the email subject
325
+ * @param String $subject - the email subject
326
+ * @return the SendGrid\Mail object
327
+ */
328
+ public function setSubject($subject)
329
+ {
330
+ $this->subject = $subject;
331
+ return $this;
332
+ }
333
+
334
+ /**
335
+ * getText
336
+ * get the plain text part of the email
337
+ * @return the plain text part of the email
338
+ */
339
+ public function getText()
340
+ {
341
+ return $this->text;
342
+ }
343
+
344
+ /**
345
+ * setText
346
+ * Set the plain text part of the email
347
+ * @param String $text - the plain text of the email
348
+ * @return the SendGrid\Mail object.
349
+ */
350
+ public function setText($text)
351
+ {
352
+ $this->text = $text;
353
+ return $this;
354
+ }
355
+
356
+ /**
357
+ * getHtml
358
+ * Get the HTML part of the email
359
+ * @param String $html - the HTML part of the email
360
+ * @return the HTML part of the email.
361
+ */
362
+ public function getHtml()
363
+ {
364
+ return $this->html;
365
+ }
366
+
367
+ /**
368
+ * setHTML
369
+ * Set the HTML part of the email
370
+ * @param String $html - the HTML part of the email
371
+ * @return the SendGrid\Mail object.
372
+ */
373
+ public function setHtml($html)
374
+ {
375
+ $this->html = $html;
376
+ return $this;
377
+ }
378
+
379
+ /**
380
+ * getAttachments
381
+ * Get the list of file attachments
382
+ * @return Array of indexed file attachments
383
+ */
384
+ public function getAttachments()
385
+ {
386
+ return $this->attachment_list;
387
+ }
388
+
389
+ /**
390
+ * setAttachments
391
+ * add multiple file attachments at once
392
+ * destroys previous attachment data.
393
+ * @param array $files - The list of files to attach
394
+ * @return the SendGrid\Mail object
395
+ */
396
+ public function setAttachments(array $files)
397
+ {
398
+ $this->attachment_list = array();
399
+ foreach($files as $file)
400
+ {
401
+ $this->addAttachment($file);
402
+ }
403
+
404
+ return $this;
405
+ }
406
+
407
+ /**
408
+ * setAttachment
409
+ * Initialize the list of attachments, and add the given file
410
+ * destroys previous attachment data.
411
+ * @param String $file - the file to attach
412
+ * @return the SendGrid\Mail object.
413
+ */
414
+ public function setAttachment($file)
415
+ {
416
+ $this->attachment_list = array($this->_getAttachmentInfo($file));
417
+ return $this;
418
+ }
419
+
420
+ /**
421
+ * addAttachment
422
+ * Add a new email attachment, given the file name.
423
+ * @param String $file - The file to attach.
424
+ * @return the SendGrid\Mail object.
425
+ */
426
+ public function addAttachment($file)
427
+ {
428
+ $this->attachment_list[] = $this->_getAttachmentInfo($file);
429
+ return $this;
430
+ }
431
+
432
+ /**
433
+ * removeAttachment
434
+ * Remove a previously added file attachment, given the file name.
435
+ * @param String $file - the file attachment to remove.
436
+ * @return the SendGrid\Mail object.
437
+ */
438
+ public function removeAttachment($file)
439
+ {
440
+ $this->_removeFromList($this->attachment_list, $file, "file");
441
+ return $this;
442
+ }
443
+
444
+ private function _getAttachmentInfo($file)
445
+ {
446
+ $info = pathinfo($file);
447
+ $info['file'] = $file;
448
+ return $info;
449
+ }
450
+
451
+ /**
452
+ * setCategories
453
+ * Set the list of category headers
454
+ * destroys previous category header data
455
+ * @param Array $category_list - the list of category values
456
+ * @return the SendGrid\Mail object.
457
+ */
458
+ public function setCategories($category_list)
459
+ {
460
+ $this->header_list['category'] = $category_list;
461
+ return $this;
462
+ }
463
+
464
+ /**
465
+ * setCategory
466
+ * Clears the category list and adds the given category
467
+ * @param String $category - the new category to append
468
+ * @return the SendGrid\Mail object.
469
+ */
470
+ public function setCategory($category)
471
+ {
472
+ $this->header_list['category'] = array($category);
473
+ return $this;
474
+ }
475
+
476
+ /**
477
+ * addCategory
478
+ * Append a category to the list of categories
479
+ * @param String $category - the new category to append
480
+ * @return the SendGrid\Mail object.
481
+ */
482
+ public function addCategory($category)
483
+ {
484
+ $this->header_list['category'][] = $category;
485
+ return $this;
486
+ }
487
+
488
+ /**
489
+ * removeCategory
490
+ * Given a category name, remove that category from the list
491
+ * of category headers
492
+ * @param String $category - the category to be removed
493
+ * @return the SendGrid\Mail object.
494
+ */
495
+ public function removeCategory($category)
496
+ {
497
+ $this->_removeFromList($this->header_list['category'], $category);
498
+ return $this;
499
+ }
500
+
501
+ /**
502
+ * SetSubstitutions
503
+ *
504
+ * Substitute a value for list of values, where each value corresponds
505
+ * to the list emails in a one to one relationship. (IE, value[0] = email[0],
506
+ * value[1] = email[1])
507
+ *
508
+ * @param array $key_value_pairs - key/value pairs where the value is an array of values
509
+ * @return the SendGrid\Mail object.
510
+ */
511
+ public function setSubstitutions($key_value_pairs)
512
+ {
513
+ $this->header_list['sub'] = $key_value_pairs;
514
+ return $this;
515
+ }
516
+
517
+ /**
518
+ * addSubstitution
519
+ * Substitute a value for list of values, where each value corresponds
520
+ * to the list emails in a one to one relationship. (IE, value[0] = email[0],
521
+ * value[1] = email[1])
522
+ *
523
+ * @param string $from_key - the value to be replaced
524
+ * @param array $to_values - an array of values to replace the $from_value
525
+ * @return the SendGrid\Mail object.
526
+ */
527
+ public function addSubstitution($from_value, array $to_values)
528
+ {
529
+ $this->header_list['sub'][$from_value] = $to_values;
530
+ return $this;
531
+ }
532
+
533
+ /**
534
+ * setSection
535
+ * Set a list of section values
536
+ * @param Array $key_value_pairs
537
+ * @return the SendGrid\Mail object.
538
+ */
539
+ public function setSections(array $key_value_pairs)
540
+ {
541
+ $this->header_list['section'] = $key_value_pairs;
542
+ return $this;
543
+ }
544
+
545
+ /**
546
+ * addSection
547
+ * append a section value to the list of section values
548
+ * @param String $from_value - the value to be replaced
549
+ * @param String $to_value - the value to replace
550
+ * @return the SendGrid\Mail object.
551
+ */
552
+ public function addSection($from_value, $to_value)
553
+ {
554
+ $this->header_list['section'][$from_value] = $to_value;
555
+ return $this;
556
+ }
557
+
558
+ /**
559
+ * setUniqueArguments
560
+ * Set a list of unique arguments, to be used for tracking purposes
561
+ * @param array $key_value_pairs - list of unique arguments
562
+ */
563
+ public function setUniqueArguments(array $key_value_pairs)
564
+ {
565
+ $this->header_list['unique_args'] = $key_value_pairs;
566
+ return $this;
567
+ }
568
+
569
+ /**
570
+ * addUniqueArgument
571
+ * Set a key/value pair of unique arguments, to be used for tracking purposes
572
+ * @param string $key - key
573
+ * @param string $value - value
574
+ */
575
+ public function addUniqueArgument($key, $value)
576
+ {
577
+ $this->header_list['unique_args'][$key] = $value;
578
+ return $this;
579
+ }
580
+
581
+ /**
582
+ * setFilterSettings
583
+ * Set filter/app settings
584
+ * @param array $filter_settings - array of fiter settings
585
+ */
586
+ public function setFilterSettings($filter_settings)
587
+ {
588
+ $this->header_list['filters'] = $filter_settings;
589
+ return $this;
590
+ }
591
+
592
+ /**
593
+ * addFilterSetting
594
+ * Append a filter setting to the list of filter settings
595
+ * @param string $filter_name - filter name
596
+ * @param string $parameter_name - parameter name
597
+ * @param string $parameter_value - setting value
598
+ */
599
+ public function addFilterSetting($filter_name, $parameter_name, $parameter_value)
600
+ {
601
+ $this->header_list['filters'][$filter_name]['settings'][$parameter_name] = $parameter_value;
602
+ return $this;
603
+ }
604
+
605
+ /**
606
+ * getHeaders
607
+ * return the list of headers
608
+ * @return Array the list of headers
609
+ */
610
+ public function getHeaders()
611
+ {
612
+ return $this->header_list;
613
+ }
614
+
615
+ /**
616
+ * getHeaders
617
+ * return the list of headers
618
+ * @return Array the list of headers
619
+ */
620
+ public function getHeadersJson()
621
+ {
622
+ if (count($this->getHeaders()) <= 0)
623
+ {
624
+ return "{}";
625
+ }
626
+ return json_encode($this->getHeaders(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
627
+ }
628
+
629
+ /**
630
+ * setHeaders
631
+ * Sets the list headers
632
+ * destroys previous header data
633
+ * @param Array $key_value_pairs - the list of header data
634
+ * @return the SendGrid\Mail object.
635
+ */
636
+ public function setHeaders($key_value_pairs)
637
+ {
638
+ $this->header_list = $key_value_pairs;
639
+ return $this;
640
+ }
641
+
642
+ /**
643
+ * addHeaders
644
+ * append the header to the list of headers
645
+ * @param String $key - the header key
646
+ * @param String $value - the header value
647
+ */
648
+ public function addHeader($key, $value)
649
+ {
650
+ $this->header_list[$key] = $value;
651
+ return $this;
652
+ }
653
+
654
+ /**
655
+ * removeHeaders
656
+ * remove a header key
657
+ * @param String $key - the key to remove
658
+ * @return the SendGrid\Mail object.
659
+ */
660
+ public function removeHeader($key)
661
+ {
662
+ unset($this->header_list[$key]);
663
+ return $this;
664
+ }
665
+
666
+ /**
667
+ * useHeaders
668
+ * Checks to see whether or not we can or should you headers. In most cases,
669
+ * we prefer to send our recipients through the headers, but in some cases,
670
+ * we actually don't want to. However, there are certain circumstances in
671
+ * which we have to.
672
+ */
673
+ public function useHeaders()
674
+ {
675
+ return !($this->_preferNotToUseHeaders() && !$this->_isHeadersRequired());
676
+ }
677
+
678
+ public function setRecipientsInHeader($preference)
679
+ {
680
+ $this->use_headers = $preference;
681
+
682
+ return $this;
683
+ }
684
+
685
+ /**
686
+ * isHeaderRequired
687
+ * determines whether or not we need to force recipients through the smtpapi headers
688
+ * @return boolean, if true headers are required
689
+ */
690
+ protected function _isHeadersRequired()
691
+ {
692
+ if(count($this->getAttachments()) > 0 || $this->use_headers )
693
+ {
694
+ return true;
695
+ }
696
+ return false;
697
+ }
698
+
699
+ /**
700
+ * _preferNotToUseHeaders
701
+ * There are certain cases in which headers are not a preferred choice
702
+ * to send email, as it limits some basic email functionality. Here, we
703
+ * check for any of those rules, and add them in to decide whether or
704
+ * not to use headers
705
+ * @return boolean, if true we don't
706
+ */
707
+ protected function _preferNotToUseHeaders()
708
+ {
709
+ if (count($this->getBccs()) > 0 || count($this->getCcs()) > 0)
710
+ {
711
+ return true;
712
+ }
713
+ if ($this->use_headers !== null && !$this->use_headers)
714
+ {
715
+ return true;
716
+ }
717
+
718
+ return false;
719
+ }
720
+
721
+ }
lib/sendgrid-php/SendGrid/MailInterface.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace SendGrid;
4
+
5
+ interface MailInterface
6
+ {
7
+ public function send(Mail $mail);
8
+
9
+
10
+ }
lib/sendgrid-php/SendGrid/Smtp.php ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace SendGrid;
4
+
5
+ class Smtp extends Api implements MailInterface
6
+ {
7
+ //the available ports
8
+ const TLS = 587;
9
+ const TLS_ALTERNATIVE = 25;
10
+ const SSL = 465;
11
+
12
+ //the list of port instances, to be recycled
13
+ private $swift_instances = array();
14
+ protected $port;
15
+
16
+ public function __construct($username, $password)
17
+ {
18
+ /* check for SwiftMailer,
19
+ * if it doesn't exist, try loading
20
+ * it from Pear
21
+ */
22
+ if (!class_exists('Swift')) {
23
+ require_once 'swift_required.php';
24
+ }
25
+ call_user_func_array("parent::__construct", func_get_args());
26
+
27
+ //set the default port
28
+ $this->port = Smtp::TLS;
29
+ }
30
+
31
+ /* setPort
32
+ * set the SMTP outgoing port number
33
+ * @param Int $port - the port number to use
34
+ * @return the SMTP object
35
+ */
36
+ public function setPort($port)
37
+ {
38
+ $this->port = $port;
39
+
40
+ return $this;
41
+ }
42
+
43
+ /* _getSwiftInstance
44
+ * initialize and return the swift transport instance
45
+ * @return the Swift_Mailer instance
46
+ */
47
+ private function _getSwiftInstance($port)
48
+ {
49
+ if (!isset($this->swift_instances[$port]))
50
+ {
51
+ $transport = \Swift_SmtpTransport::newInstance('smtp.sendgrid.net', $port);
52
+ $transport->setUsername($this->username);
53
+ $transport->setPassword($this->password);
54
+
55
+ $swift = \Swift_Mailer::newInstance($transport);
56
+
57
+ $this->swift_instances[$port] = $swift;
58
+ }
59
+
60
+ return $this->swift_instances[$port];
61
+ }
62
+
63
+ /* _mapToSwift
64
+ * Maps the SendGridMail Object to the SwiftMessage object
65
+ * @param Mail $mail - the SendGridMail object
66
+ * @return the SwiftMessage object
67
+ */
68
+ protected function _mapToSwift(Mail $mail)
69
+ {
70
+ $message = new \Swift_Message($mail->getSubject());
71
+
72
+ /*
73
+ * Since we're sending transactional email, we want the message to go to one person at a time, rather
74
+ * than a bulk send on one message. In order to do this, we'll have to send the list of recipients through the headers
75
+ * but Swift still requires a 'to' address. So we'll falsify it with the from address, as it will be
76
+ * ignored anyway.
77
+ */
78
+ $message->setTo($mail->getFrom());
79
+ $message->setFrom($mail->getFrom(true));
80
+ $message->setCc($mail->getCcs());
81
+ $message->setBcc($mail->getBccs());
82
+
83
+ if ($mail->getHtml())
84
+ {
85
+ $message->setBody($mail->getHtml(), 'text/html');
86
+ if ($mail->getText()) $message->addPart($mail->getText(), 'text/plain');
87
+ }
88
+ else
89
+ {
90
+ $message->setBody($mail->getText(), 'text/plain');
91
+ }
92
+
93
+ if(($replyto = $mail->getReplyTo())) {
94
+ $message->setReplyTo($replyto);
95
+ }
96
+
97
+ // determine whether or not we can use SMTP recipients (non header based)
98
+ if($mail->useHeaders())
99
+ {
100
+ //send header based email
101
+ $message->setTo($mail->getFrom());
102
+
103
+ //here we'll add the recipients list to the headers
104
+ $headers = $mail->getHeaders();
105
+ $headers['to'] = $mail->getTos();
106
+ $mail->setHeaders($headers);
107
+ }
108
+ else
109
+ {
110
+ $recipients = array();
111
+ foreach ($mail->getTos() as $recipient)
112
+ {
113
+ if(preg_match("/(.*)<(.*)>/", $recipient, $results))
114
+ {
115
+ $recipients[trim($results[2])] = trim($results[1]);
116
+ }
117
+ else
118
+ {
119
+ $recipients[] = $recipient;
120
+ }
121
+ }
122
+
123
+ $message->setTo($recipients);
124
+ }
125
+
126
+ $attachments = $mail->getAttachments();
127
+
128
+ //add any attachments that were added
129
+ if ($attachments)
130
+ {
131
+ foreach ($attachments as $attachment)
132
+ {
133
+ $message->attach(\Swift_Attachment::fromPath($attachment['file']));
134
+ }
135
+ }
136
+
137
+ //add all the headers
138
+ $headers = $message->getHeaders();
139
+ $headers->addTextHeader('X-SMTPAPI', $mail->getHeadersJson());
140
+
141
+ return $message;
142
+ }
143
+
144
+ /* send
145
+ * Send the Mail Message
146
+ * @param Mail $mail - the SendGridMailMessage to be sent
147
+ * @return true if mail was sendable (not necessarily sent)
148
+ */
149
+ public function send(Mail $mail)
150
+ {
151
+ $swift = $this->_getSwiftInstance($this->port);
152
+
153
+ $message = $this->_mapToSwift($mail);
154
+
155
+ $swift->send($message, $failures);
156
+
157
+ return true;
158
+ }
159
+ }
lib/sendgrid-php/SendGrid/Web.php ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace SendGrid;
4
+
5
+ class Web extends Api implements MailInterface
6
+ {
7
+
8
+ protected $domain = "http://sendgrid.com/";
9
+ protected $endpoint = "api/mail.send.json";
10
+
11
+ /**
12
+ * __construct
13
+ * Create a new Web instance
14
+ */
15
+ public function __construct($username, $password)
16
+ {
17
+ call_user_func_array("parent::__construct", func_get_args());
18
+ }
19
+
20
+ /**
21
+ * _prepMessageData
22
+ * Takes the mail message and returns a url friendly querystring
23
+ * @param Mail $mail [description]
24
+ * @return String - the data query string to be posted
25
+ */
26
+ protected function _prepMessageData(Mail $mail)
27
+ {
28
+
29
+ /* the api expects a 'to' parameter, but this parameter will be ignored
30
+ * since we're sending the recipients through the header. The from
31
+ * address will be used as a placeholder.
32
+ */
33
+ $params =
34
+ array(
35
+ 'api_user' => $this->username,
36
+ 'api_key' => $this->password,
37
+ 'subject' => $mail->getSubject(),
38
+ 'from' => $mail->getFrom(),
39
+ 'to' => $mail->getFrom(),
40
+ 'x-smtpapi' => $mail->getHeadersJson()
41
+ );
42
+
43
+ if($mail->getHtml()) {
44
+ $params['html'] = $mail->getHtml();
45
+ }
46
+
47
+ if($mail->getText()) {
48
+ $params['text'] = $mail->getText();
49
+ }
50
+
51
+ if(($fromname = $mail->getFromName())) {
52
+ $params['fromname'] = $fromname;
53
+ }
54
+
55
+ if(($replyto = $mail->getReplyTo())) {
56
+ $params['replyto'] = $replyto;
57
+ }
58
+
59
+ // determine if we should send our recipients through our headers,
60
+ // and set the properties accordingly
61
+ if($mail->useHeaders())
62
+ {
63
+ // workaround for posting recipients through SendGrid headers
64
+ $headers = $mail->getHeaders();
65
+ $headers['to'] = $mail->getTos();
66
+ $mail->setHeaders($headers);
67
+
68
+ $params['x-smtpapi'] = $mail->getHeadersJson();
69
+ }
70
+ else
71
+ {
72
+ $params['to'] = $mail->getTos();
73
+ }
74
+
75
+
76
+ if($mail->getAttachments())
77
+ {
78
+ foreach($mail->getAttachments() as $attachment)
79
+ {
80
+ $params['files['.$attachment['filename'].'.'.$attachment['extension'].']'] = '@'.$attachment['file'];
81
+ }
82
+ }
83
+
84
+ return $params;
85
+ }
86
+
87
+ /**
88
+ * _arrayToUrlPart
89
+ * Converts an array to a url friendly string
90
+ * @param array $array - the array to convert
91
+ * @param String $token - the name of parameter
92
+ * @return String - a url part that can be concatenated to a url request
93
+ */
94
+ protected function _arrayToUrlPart($array, $token)
95
+ {
96
+ $string = "";
97
+
98
+ if ($array)
99
+ {
100
+ foreach ($array as $value)
101
+ {
102
+ $string.= "&" . $token . "[]=" . urlencode($value);
103
+ }
104
+ }
105
+
106
+ return $string;
107
+ }
108
+
109
+ /**
110
+ * send
111
+ * Send an email
112
+ * @param Mail $mail - The message to send
113
+ * @return String the json response
114
+ */
115
+ public function send(Mail $mail)
116
+ {
117
+ $data = $this->_prepMessageData($mail);
118
+
119
+ //if we're not using headers, we need to send a url friendly post
120
+ if(!$mail->useHeaders())
121
+ {
122
+ $data = http_build_query($data);
123
+ }
124
+
125
+ $request = $this->domain . $this->endpoint;
126
+
127
+ // we'll append the Bcc and Cc recipients to the url endpoint (GET)
128
+ // so that we can still post attachments (via cURL array).
129
+ $request.= "?" .
130
+ substr($this->_arrayToUrlPart($mail->getBccs(), "bcc"), 1) .
131
+ $this->_arrayToUrlPart($mail->getCcs(), "cc");
132
+
133
+ $session = curl_init($request);
134
+ curl_setopt($session, CURLOPT_POST, true);
135
+ curl_setopt($session, CURLOPT_POSTFIELDS, $data);
136
+ curl_setopt($session, CURLOPT_HEADER, false);
137
+ curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
138
+ curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5);
139
+ curl_setopt($session, CURLOPT_TIMEOUT, 30);
140
+
141
+ // obtain response
142
+ $response = curl_exec($session);
143
+ curl_close($session);
144
+
145
+ return $response;
146
+ }
147
+ }
lib/sendgrid-php/SendGrid_loader.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ define("ROOT_DIR", __dir__ . DIRECTORY_SEPARATOR);
4
+
5
+ function sendGridLoader($string)
6
+ {
7
+ if(preg_match("/SendGrid/", $string))
8
+ {
9
+ $file = str_replace('\\', '/', "$string.php");
10
+ require_once ROOT_DIR . $file;
11
+ }
12
+ }
13
+
14
+ spl_autoload_register("sendGridLoader");
lib/sendgrid-php/Test/Mock/Mock_loader.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ define("MOCK_ROOT", __dir__ . DIRECTORY_SEPARATOR);
4
+
5
+ function mockLoader($string)
6
+ {
7
+ if(preg_match("/Mock/", $string))
8
+ {
9
+ $file = str_replace('\\', '/', "$string.php");
10
+ require_once MOCK_ROOT . $file;
11
+ }
12
+ }
13
+
14
+ spl_autoload_register("mockLoader");
lib/sendgrid-php/Test/Mock/SmtpMock.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class SmtpMock extends SendGrid\Smtp
4
+ {
5
+ public function __construct($username, $password)
6
+ {
7
+ parent::__construct($username, $password);
8
+ }
9
+
10
+ public function getPort()
11
+ {
12
+ return $this->port;
13
+ }
14
+ }
lib/sendgrid-php/Test/Mock/WebMock.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class WebMock extends SendGrid\Web
4
+ {
5
+ public function __construct($username, $password)
6
+ {
7
+ parent::__construct($username, $password);
8
+ }
9
+
10
+ public function testPrepMessageData(SendGrid\Mail $mail)
11
+ {
12
+ return $this->_prepMessageData($mail);
13
+ }
14
+
15
+ public function testArrayToUrlPart($array, $token)
16
+ {
17
+ return $this->_arrayToUrlPart($array, $token);
18
+ }
19
+ }
lib/sendgrid-php/Test/SendGrid/ApiTest.php ADDED
File without changes
lib/sendgrid-php/Test/SendGrid/MailTest.php ADDED
@@ -0,0 +1,549 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+ class MailTest extends PHPUnit_Framework_TestCase
5
+ {
6
+
7
+ public function testToAccessors()
8
+ {
9
+ $message = new SendGrid\Mail();
10
+
11
+ // setTo instanciates and overrides existing data
12
+ $message->setTo('bar');
13
+ $message->setTo('foo');
14
+
15
+ $this->assertEquals(1, count($message->getTos()));
16
+
17
+ $to_list = $message->getTos();
18
+
19
+ $this->assertEquals('foo', $to_list[0]);
20
+
21
+
22
+ // setTos instanciates and overrides existing data
23
+ $message->setTos(array('raz', 'ber'));
24
+
25
+ $this->assertEquals(2, count($message->getTos()));
26
+
27
+ $to_list = $message->getTos();
28
+
29
+ $this->assertEquals('raz', $to_list[0]);
30
+ $this->assertEquals('ber', $to_list[1]);
31
+
32
+ // addTo appends to existing data
33
+ $message->addTo('foo');
34
+ $message->addTo('raz');
35
+
36
+ $this->assertEquals(4, count($message->getTos()));
37
+
38
+ $to_list = $message->getTos();
39
+
40
+ $this->assertEquals('raz', $to_list[0]);
41
+ $this->assertEquals('ber', $to_list[1]);
42
+ $this->assertEquals('foo', $to_list[2]);
43
+ $this->assertEquals('raz', $to_list[3]);
44
+
45
+ // removeTo removes all occurences of data
46
+ $message->removeTo('raz');
47
+
48
+ $this->assertEquals(2, count($message->getTos()));
49
+
50
+ $to_list = $message->getTos();
51
+
52
+ $this->assertEquals('ber', $to_list[0]);
53
+ $this->assertEquals('foo', $to_list[1]);
54
+ }
55
+
56
+ public function testFromAccessors()
57
+ {
58
+ $message = new SendGrid\Mail();
59
+
60
+ $message->setFrom("foo@bar.com");
61
+ $message->setFromName("John Doe");
62
+
63
+ $this->assertEquals("foo@bar.com", $message->getFrom());
64
+ $this->assertEquals(array("foo@bar.com" => "John Doe"), $message->getFrom(true));
65
+ }
66
+
67
+ public function testFromNameAccessors()
68
+ {
69
+ $message = new SendGrid\Mail();
70
+
71
+ // Defaults to false
72
+ $this->assertFalse($message->getFromName());
73
+
74
+ $message->setFromName("Swift");
75
+
76
+ $this->assertEquals("Swift", $message->getFromName());
77
+ }
78
+
79
+ public function testReplyToAccessors()
80
+ {
81
+ $message = new SendGrid\Mail();
82
+
83
+ // Defaults to false
84
+ $this->assertFalse($message->getReplyTo());
85
+
86
+ $message->setReplyTo("swift@sendgrid.com");
87
+
88
+ $this->assertEquals("swift@sendgrid.com", $message->getReplyTo());
89
+ }
90
+
91
+ public function testCcAccessors()
92
+ {
93
+ $message = new SendGrid\Mail();
94
+
95
+ // setTo instanciates and overrides existing data
96
+ $message->setCc('bar');
97
+ $message->setCc('foo');
98
+
99
+ $this->assertEquals(1, count($message->getCcs()));
100
+
101
+ $cc_list = $message->getCcs();
102
+
103
+ $this->assertEquals('foo', $cc_list[0]);
104
+
105
+
106
+ // setTos instanciates and overrides existing data
107
+ $message->setCcs(array('raz', 'ber'));
108
+
109
+ $this->assertEquals(2, count($message->getCcs()));
110
+
111
+ $cc_list = $message->getCcs();
112
+
113
+ $this->assertEquals('raz', $cc_list[0]);
114
+ $this->assertEquals('ber', $cc_list[1]);
115
+
116
+ // addTo appends to existing data
117
+ $message->addCc('foo');
118
+ $message->addCc('raz');
119
+
120
+ $this->assertEquals(4, count($message->getCcs()));
121
+
122
+ $cc_list = $message->getCcs();
123
+
124
+ $this->assertEquals('raz', $cc_list[0]);
125
+ $this->assertEquals('ber', $cc_list[1]);
126
+ $this->assertEquals('foo', $cc_list[2]);
127
+ $this->assertEquals('raz', $cc_list[3]);
128
+
129
+ // removeTo removes all occurences of data
130
+ $message->removeCc('raz');
131
+
132
+ $this->assertEquals(2, count($message->getCcs()));
133
+
134
+ $cc_list = $message->getCcs();
135
+
136
+ $this->assertEquals('ber', $cc_list[0]);
137
+ $this->assertEquals('foo', $cc_list[1]);
138
+ }
139
+
140
+ public function testBccAccessors()
141
+ {
142
+ $message = new SendGrid\Mail();
143
+
144
+ // setTo instanciates and overrides existing data
145
+ $message->setBcc('bar');
146
+ $message->setBcc('foo');
147
+
148
+ $this->assertEquals(1, count($message->getBccs()));
149
+
150
+ $bcc_list = $message->getBccs();
151
+
152
+ $this->assertEquals('foo', $bcc_list[0]);
153
+
154
+
155
+ // setTos instanciates and overrides existing data
156
+ $message->setBccs(array('raz', 'ber'));
157
+
158
+ $this->assertEquals(2, count($message->getBccs()));
159
+
160
+ $bcc_list = $message->getBccs();
161
+
162
+ $this->assertEquals('raz', $bcc_list[0]);
163
+ $this->assertEquals('ber', $bcc_list[1]);
164
+
165
+ // addTo appends to existing data
166
+ $message->addBcc('foo');
167
+ $message->addBcc('raz');
168
+
169
+ $this->assertEquals(4, count($message->getBccs()));
170
+
171
+ $bcc_list = $message->getBccs();
172
+
173
+ $this->assertEquals('raz', $bcc_list[0]);
174
+ $this->assertEquals('ber', $bcc_list[1]);
175
+ $this->assertEquals('foo', $bcc_list[2]);
176
+ $this->assertEquals('raz', $bcc_list[3]);
177
+
178
+ // removeTo removes all occurences of data
179
+ $message->removeBcc('raz');
180
+
181
+ $this->assertEquals(2, count($message->getBccs()));
182
+
183
+ $bcc_list = $message->getBccs();
184
+
185
+ $this->assertEquals('ber', $bcc_list[0]);
186
+ $this->assertEquals('foo', $bcc_list[1]);
187
+ }
188
+
189
+ public function testSubjectAccessors()
190
+ {
191
+ $message = new SendGrid\Mail();
192
+
193
+ $message->setSubject("Test Subject");
194
+
195
+ $this->assertEquals("Test Subject", $message->getSubject());
196
+ }
197
+
198
+ public function testTextAccessors()
199
+ {
200
+ $message = new SendGrid\Mail();
201
+
202
+ $text = "sample plain text";
203
+
204
+ $message->setText($text);
205
+
206
+ $this->assertEquals($text, $message->getText());
207
+ }
208
+
209
+ public function testHTMLAccessors()
210
+ {
211
+ $message = new SendGrid\Mail();
212
+
213
+ $html = "<p style = 'color:red;'>Sample HTML text</p>";
214
+
215
+ $message->setHtml($html);
216
+
217
+ $this->assertEquals($html, $message->getHtml());
218
+ }
219
+
220
+ public function testAttachmentAccessors()
221
+ {
222
+ $message = new SendGrid\Mail();
223
+
224
+ $attachments =
225
+ array(
226
+ "path/to/file/file_1.txt",
227
+ "../file_2.txt",
228
+ "../file_3.txt"
229
+ );
230
+
231
+ $message->setAttachments($attachments);
232
+
233
+ $msg_attachments = $message->getAttachments();
234
+
235
+ $this->assertEquals(count($attachments), count($msg_attachments));
236
+
237
+ for($i = 0; $i < count($attachments); $i++)
238
+ {
239
+ $this->assertEquals($attachments[$i], $msg_attachments[$i]['file']);
240
+ }
241
+
242
+ //ensure that addAttachment appends to the list of attachments
243
+ $message->addAttachment("../file_4.png");
244
+
245
+ $attachments[] = "../file_4.png";
246
+
247
+ $msg_attachments = $message->getAttachments();
248
+ $this->assertEquals($attachments[count($attachments) - 1], $msg_attachments[count($msg_attachments) - 1]['file']);
249
+
250
+
251
+ //Setting an attachment removes all other files
252
+ $message->setAttachment("only_attachment.sad");
253
+
254
+ $this->assertEquals(1, count($message->getAttachments()));
255
+
256
+ //Remove an attachment
257
+ $message->removeAttachment("only_attachment.sad");
258
+ $this->assertEquals(0, count($message->getAttachments()));
259
+ }
260
+
261
+ public function testCategoryAccessors()
262
+ {
263
+ $message = new SendGrid\Mail();
264
+
265
+ $message->setCategory('category_0');
266
+ $this->assertEquals("{\"category\":[\"category_0\"]}", $message->getHeadersJson());
267
+
268
+ $categories = array(
269
+ "category_1",
270
+ "category_2",
271
+ "category_3",
272
+ "category_4"
273
+ );
274
+
275
+ $message->setCategories($categories);
276
+
277
+ $header = $message->getHeaders();
278
+
279
+ // ensure that the array is the same
280
+ $this->assertEquals($categories, $header['category']);
281
+
282
+ // uses valid json
283
+ $this->assertEquals("{\"category\":[\"category_1\",\"category_2\",\"category_3\",\"category_4\"]}", $message->getHeadersJson());
284
+
285
+ // ensure that addCategory appends to the list of categories
286
+ $category = "category_5";
287
+ $message->addCategory($category);
288
+
289
+ $header = $message->getHeaders();
290
+
291
+ $this->assertEquals(5, count($header['category']));
292
+
293
+ $categories[] = $category;
294
+
295
+ $this->assertEquals($categories, $header['category']);
296
+
297
+
298
+ // removeCategory removes all occurrences of a category
299
+ $message->removeCategory("category_3");
300
+
301
+ $header = $message->getHeaders();
302
+
303
+ unset($categories[2]);
304
+ $categories = array_values($categories);
305
+
306
+ $this->assertEquals(4, count($header['category']));
307
+
308
+ $this->assertEquals($categories, $header['category']);
309
+ }
310
+
311
+ public function testSubstitutionAccessors()
312
+ {
313
+ $message = new SendGrid\Mail();
314
+
315
+ $substitutions = array(
316
+ "sub_1" => array("val_1.1", "val_1.2", "val_1.3"),
317
+ "sub_2" => array("val_2.1", "val_2.2"),
318
+ "sub_3" => array("val_3.1", "val_3.2", "val_3.3", "val_3.4"),
319
+ "sub_4" => array("val_4.1", "val_4.2", "val_4.3")
320
+ );
321
+
322
+ $message->setSubstitutions($substitutions);
323
+
324
+ $header = $message->getHeaders();
325
+
326
+ $this->assertEquals($substitutions, $header['sub']);
327
+
328
+ $this->assertEquals("{\"sub\":{\"sub_1\":[\"val_1.1\",\"val_1.2\",\"val_1.3\"],\"sub_2\":[\"val_2.1\",\"val_2.2\"],\"sub_3\":[\"val_3.1\",\"val_3.2\",\"val_3.3\",\"val_3.4\"],\"sub_4\":[\"val_4.1\",\"val_4.2\",\"val_4.3\"]}}", $message->getHeadersJson());
329
+
330
+ // ensure that addSubstitution appends to the list of substitutions
331
+
332
+ $sub_vals = array("val_5.1", "val_5.2", "val_5.3", "val_5.4");
333
+ $message->addSubstitution("sub_5", $sub_vals);
334
+
335
+ $substitutions["sub_5"] = $sub_vals;
336
+
337
+ $header = $message->getHeaders();
338
+
339
+ $this->assertEquals(5, count($header['sub']));
340
+ $this->assertEquals($substitutions, $header['sub']);
341
+ }
342
+
343
+ public function testSectionAccessors()
344
+ {
345
+ $message = new SendGrid\Mail();
346
+
347
+ $sections = array(
348
+ "sub_1" => array("val_1.1", "val_1.2", "val_1.3"),
349
+ "sub_2" => array("val_2.1", "val_2.2"),
350
+ "sub_3" => array("val_3.1", "val_3.2", "val_3.3", "val_3.4"),
351
+ "sub_4" => array("val_4.1", "val_4.2", "val_4.3")
352
+ );
353
+
354
+ $message->setSections($sections);
355
+
356
+ $header = $message->getHeaders();
357
+
358
+ $this->assertEquals($sections, $header['section']);
359
+
360
+ $this->assertEquals("{\"section\":{\"sub_1\":[\"val_1.1\",\"val_1.2\",\"val_1.3\"],\"sub_2\":[\"val_2.1\",\"val_2.2\"],\"sub_3\":[\"val_3.1\",\"val_3.2\",\"val_3.3\",\"val_3.4\"],\"sub_4\":[\"val_4.1\",\"val_4.2\",\"val_4.3\"]}}", $message->getHeadersJson());
361
+
362
+ // ensure that addSubstitution appends to the list of substitutions
363
+
364
+ $section_vals = array("val_5.1", "val_5.2", "val_5.3", "val_5.4");
365
+ $message->addSection("sub_5", $section_vals);
366
+
367
+ $sections["sub_5"] = $section_vals;
368
+
369
+ $header = $message->getHeaders();
370
+
371
+ $this->assertEquals(5, count($header['section']));
372
+ $this->assertEquals($sections, $header['section']);
373
+ }
374
+
375
+ public function testUniqueArgumentsAccessors()
376
+ {
377
+ $message = new SendGrid\Mail();
378
+
379
+ $unique_arguments = array(
380
+ "sub_1" => array("val_1.1", "val_1.2", "val_1.3"),
381
+ "sub_2" => array("val_2.1", "val_2.2"),
382
+ "sub_3" => array("val_3.1", "val_3.2", "val_3.3", "val_3.4"),
383
+ "sub_4" => array("val_4.1", "val_4.2", "val_4.3")
384
+ );
385
+
386
+ $message->setUniqueArguments($unique_arguments);
387
+
388
+ $header = $message->getHeaders();
389
+
390
+ $this->assertEquals($unique_arguments, $header['unique_args']);
391
+
392
+ $this->assertEquals("{\"unique_args\":{\"sub_1\":[\"val_1.1\",\"val_1.2\",\"val_1.3\"],\"sub_2\":[\"val_2.1\",\"val_2.2\"],\"sub_3\":[\"val_3.1\",\"val_3.2\",\"val_3.3\",\"val_3.4\"],\"sub_4\":[\"val_4.1\",\"val_4.2\",\"val_4.3\"]}}", $message->getHeadersJson());
393
+
394
+ // ensure that addSubstitution appends to the list of substitutions
395
+
396
+ $unique_vals = array("val_5.1", "val_5.2", "val_5.3", "val_5.4");
397
+ $message->addUniqueArgument("sub_5", $unique_vals);
398
+
399
+ $unique_arguments["sub_5"] = $unique_vals;
400
+
401
+ $header = $message->getHeaders();
402
+
403
+ $this->assertEquals(5, count($header['unique_args']));
404
+ $this->assertEquals($unique_arguments, $header['unique_args']);
405
+ }
406
+
407
+ public function testFilterSettingsAccessors()
408
+ {
409
+ $message = new SendGrid\Mail();
410
+
411
+ $filters =
412
+ array(
413
+ "filter_1" =>
414
+ array(
415
+ "settings" =>
416
+ array(
417
+ "enable" => 1,
418
+ "setting_1" => "setting_val_1"
419
+ )
420
+ ),
421
+ "filter_2" =>
422
+ array(
423
+ "settings" =>
424
+ array(
425
+ "enable" => 0,
426
+ "setting_2" => "setting_val_2",
427
+ "setting_3" => "setting_val_3"
428
+ )
429
+ ),
430
+ "filter_3" =>
431
+ array(
432
+ "settings" =>
433
+ array(
434
+ "enable" => 0,
435
+ "setting_4" => "setting_val_4",
436
+ "setting_5" => "setting_val_5"
437
+ )
438
+ ),
439
+ );
440
+
441
+ $message->setFilterSettings($filters);
442
+
443
+ $header = $message->getHeaders();
444
+
445
+ $this->assertEquals(count($filters), count($header['filters']));
446
+
447
+ $this->assertEquals($filters, $header['filters']);
448
+
449
+
450
+ //the addFilter appends to the filter list
451
+ $message->addFilterSetting("filter_4", "enable", 0);
452
+ $message->addFilterSetting("filter_4", "setting_6", "setting_val_6");
453
+ $message->addFilterSetting("filter_4", "setting_7", "setting_val_7");
454
+
455
+ $filters["filter_4"] =
456
+ array(
457
+ "settings" =>
458
+ array(
459
+ "enable" => 0,
460
+ "setting_6" => "setting_val_6",
461
+ "setting_7" => "setting_val_7"
462
+ )
463
+ );
464
+
465
+ $header = $message->getHeaders();
466
+
467
+ $this->assertEquals($filters, $header['filters']);
468
+ }
469
+
470
+ public function testHeaderAccessors()
471
+ {
472
+ $message = new SendGrid\Mail();
473
+
474
+ $this->assertEquals("{}", $message->getHeadersJson());
475
+
476
+
477
+ $headers =
478
+ array(
479
+ "header_1" =>
480
+ array(
481
+ "item_1" => "value_1",
482
+ "item_2" => "value_2",
483
+ "item_3" => "value_3"
484
+ ),
485
+ "header_2" => "value_4",
486
+ "header_3" => "value_4",
487
+ "header_4" =>
488
+ array(
489
+ "item_4" =>
490
+ array(
491
+ "sub_item_1" => "sub_value_1",
492
+ "sub_item_2" => "sub_value_2"
493
+ )
494
+ )
495
+ );
496
+
497
+
498
+ $message->setHeaders($headers);
499
+
500
+
501
+ $this->assertEquals($headers, $message->getHeaders());
502
+
503
+ $message->addHeader("simple_header", "simple_value");
504
+
505
+ $headers["simple_header"] = "simple_value";
506
+
507
+ $this->assertEquals($headers, $message->getHeaders());
508
+ $this->assertEquals("{\"header_1\":{\"item_1\":\"value_1\",\"item_2\":\"value_2\",\"item_3\":\"value_3\"},\"header_2\":\"value_4\",\"header_3\":\"value_4\",\"header_4\":{\"item_4\":{\"sub_item_1\":\"sub_value_1\",\"sub_item_2\":\"sub_value_2\"}},\"simple_header\":\"simple_value\"}", $message->getHeadersJson());
509
+
510
+ //remove a header
511
+ $message->removeHeader("simple_header");
512
+
513
+ unset($headers["simple_header"]);
514
+
515
+ $this->assertEquals($headers, $message->getHeaders());
516
+ }
517
+
518
+ public function testUseHeaders()
519
+ {
520
+ $mail = new SendGrid\Mail();
521
+
522
+ $mail->addTo('foo@bar.com')->
523
+ addBcc('baa@bar.com')->
524
+ setFrom('boo@foo.com')->
525
+ setSubject('Subject')->
526
+ setHtml('Hello You');
527
+
528
+ $this->assertFalse($mail->useHeaders());
529
+
530
+ $mail->removeBcc('baa@bar.com');
531
+ $this->assertTrue($mail->useHeaders());
532
+
533
+ $mail->addCc('bot@bar.com');
534
+ $this->assertFalse($mail->useHeaders());
535
+
536
+ $mail->removeCc('bot@bar.com')->
537
+ setRecipientsinHeader(true);
538
+ $this->assertTrue($mail->useHeaders());
539
+
540
+ $mail->setRecipientsinHeader(false);
541
+ $this->assertFalse($mail->useHeaders());
542
+
543
+ $mail->
544
+ addBcc('baa@bar.com')->
545
+ addAttachment('attachment.ext');
546
+
547
+ $this->assertTrue($mail->useHeaders());
548
+ }
549
+ }
lib/sendgrid-php/Test/SendGrid/SmtpTest.php ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class SmtpTest extends PHPUnit_Framework_TestCase
4
+ {
5
+ public function testConstruction()
6
+ {
7
+ $sendgrid = new SendGrid("foo", "bar");
8
+
9
+ $smtp = $sendgrid->smtp;
10
+
11
+ $this->assertEquals(new SendGrid\Smtp("foo", "bar"), $smtp);
12
+
13
+ $message = new SendGrid\Mail();
14
+ $message->
15
+ setFrom('bar@foo.com')->
16
+ setFromName('John Doe')->
17
+ setSubject('foobar subject')->
18
+ setText('foobar text')->
19
+ addTo('foo@bar.com')->
20
+ addAttachment("mynewattachment.jpg");
21
+
22
+ $this->assertEquals(get_class($smtp), 'SendGrid\Smtp');
23
+
24
+ $this->setExpectedException('Swift_TransportException');
25
+ $smtp->send($message);
26
+ }
27
+
28
+ public function testPorts()
29
+ {
30
+ $this->assertEquals(587, SendGrid\Smtp::TLS);
31
+ $this->assertEquals(25, SendGrid\Smtp::TLS_ALTERNATIVE);
32
+ $this->assertEquals(465, SendGrid\Smtp::SSL);
33
+
34
+ $sendgrid = new SendGrid("foo", "bar");
35
+
36
+ //we can't check that the port works, but we can check that it doesn't throw an exception
37
+ $object = $sendgrid->smtp->setPort(SendGrid\Smtp::TLS);
38
+
39
+ $this->assertEquals($sendgrid->smtp, $object);
40
+ $this->assertEquals(get_class($object), 'SendGrid\Smtp');
41
+
42
+
43
+ $mock = new SmtpMock('foo', 'bar');
44
+
45
+ $mock->setPort('52');
46
+ $this->assertEquals('52', $mock->getPort());
47
+ }
48
+
49
+ public function testEmailBodyAttachments()
50
+ {
51
+ $_mapToSwift = new ReflectionMethod('SendGrid\Smtp', '_mapToSwift');
52
+ $_mapToSwift->setAccessible(true);
53
+
54
+ $sendgrid = new SendGrid("foo", "bar");
55
+ $message = new SendGrid\Mail();
56
+ $message->
57
+ setFrom('bar@foo.com')->
58
+ setFromName('John Doe')->
59
+ setSubject('foobar subject')->
60
+ setHtml('foobar html')->
61
+ addTo('foo@bar.com');
62
+
63
+ $swift_message = $_mapToSwift->invoke($sendgrid->smtp, $message);
64
+ $this->assertEquals(count($swift_message->getChildren()), 0);
65
+
66
+ $message->setText('foobar text');
67
+
68
+ $swift_message = $_mapToSwift->invoke($sendgrid->smtp, $message);
69
+ $this->assertEquals(count($swift_message->getChildren()), 1);
70
+ $body_attachments = $swift_message->getChildren();
71
+ $this->assertEquals($body_attachments[0]->getContentType(), 'text/plain');
72
+ }
73
+
74
+ public function testEmailTextBodyAttachments()
75
+ {
76
+ $_mapToSwift = new ReflectionMethod('SendGrid\Smtp', '_mapToSwift');
77
+ $_mapToSwift->setAccessible(true);
78
+
79
+ $sendgrid = new SendGrid("foo", "bar");
80
+ $message = new SendGrid\Mail();
81
+ $message->
82
+ setFrom('bar@foo.com')->
83
+ setFromName('John Doe')->
84
+ setSubject('foobar subject')->
85
+ setText('foobar text')->
86
+ addTo('foo@bar.com');
87
+
88
+ $swift_message = $_mapToSwift->invoke($sendgrid->smtp, $message);
89
+ $this->assertEquals(count($swift_message->getChildren()), 0);
90
+ }
91
+ }
lib/sendgrid-php/Test/SendGrid/WebTest.php ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class WebTest extends PHPUnit_Framework_TestCase
4
+ {
5
+ public function testConstruction()
6
+ {
7
+ $sendgrid = new SendGrid("foo", "bar");
8
+
9
+ $web = $sendgrid->web;
10
+
11
+ $this->assertEquals(new SendGrid\Web("foo", "bar"), $web);
12
+ $this->assertEquals(get_class($web), "SendGrid\Web");
13
+ }
14
+
15
+ public function testMockFunctions()
16
+ {
17
+ $message = new SendGrid\Mail();
18
+
19
+ $message->
20
+ setFrom('bar@foo.com')->
21
+ setSubject('foobar subject')->
22
+ setText('foobar text')->
23
+ setHtml('foobar html')->
24
+ addTo('foo@bar.com')->
25
+ addAttachment("mynewattachment.jpg");
26
+
27
+ $mock = new WebMock("foo", "bar");
28
+ $data = $mock->testPrepMessageData($message);
29
+
30
+ $expected =
31
+ array(
32
+ 'api_user' => 'foo',
33
+ 'api_key' => 'bar',
34
+ 'subject' => 'foobar subject',
35
+ 'html' => 'foobar html',
36
+ 'text' => 'foobar text',
37
+ 'from' => 'bar@foo.com',
38
+ 'to' => 'bar@foo.com',
39
+ 'x-smtpapi' => '{"to":["foo@bar.com"]}',
40
+ 'files[mynewattachment.jpg]' => '@mynewattachment.jpg'
41
+ );
42
+
43
+ $this->assertEquals($expected, $data);
44
+
45
+
46
+ $array =
47
+ array(
48
+ "foo",
49
+ "bar",
50
+ "car",
51
+ "doo"
52
+ );
53
+
54
+ $url_part = $mock->testArrayToUrlPart($array, "param");
55
+
56
+ $this->assertEquals("&param[]=foo&param[]=bar&param[]=car&param[]=doo", $url_part);
57
+ }
58
+
59
+ public function testOptionalParamters()
60
+ {
61
+ $message = new SendGrid\Mail();
62
+ $mock = new WebMock("foo", "bar");
63
+
64
+ // Default Values
65
+ $actual_without_optional_params = $mock->testPrepMessageData($message);
66
+
67
+ $this->assertArrayNotHasKey('html', $actual_without_optional_params);
68
+ $this->assertArrayNotHasKey('text', $actual_without_optional_params);
69
+ $this->assertArrayNotHasKey('fromname', $actual_without_optional_params);
70
+ $this->assertArrayNotHasKey('replyto', $actual_without_optional_params);
71
+
72
+ // Set optional params
73
+ $message->setFromName('John Doe');
74
+ $message->setReplyTo('swift@sendgrid.com');
75
+
76
+ $actual_with_optional_params = $mock->testPrepMessageData($message);
77
+
78
+ $this->assertArrayHasKey('fromname', $actual_with_optional_params);
79
+ $this->assertEquals('John Doe', $actual_with_optional_params['fromname']);
80
+
81
+ $this->assertArrayHasKey('replyto', $actual_with_optional_params);
82
+ $this->assertEquals('swift@sendgrid.com', $actual_with_optional_params['replyto']);
83
+ }
84
+
85
+ public function testSendResponse()
86
+ {
87
+ $sendgrid = new SendGrid("foo", "bar");
88
+
89
+ $message = new SendGrid\Mail();
90
+
91
+ $message->
92
+ setFrom('bar@foo.com')->
93
+ setSubject('foobar subject')->
94
+ setText('foobar text')->
95
+ addTo('foo@bar.com');
96
+
97
+ $response = $sendgrid->web->send($message);
98
+
99
+ $this->assertEquals("{\"message\": \"error\", \"errors\": [\"Bad username / password\"]}", $response);
100
+ }
101
+ }
lib/sendgrid-php/Test/SendGridTest.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class SendGridTest extends PHPUnit_Framework_TestCase
4
+ {
5
+
6
+ public function testConstruction()
7
+ {
8
+ $sendgrid = new SendGrid("fake_username", "fake_password");
9
+
10
+ $this->assertEquals("SendGrid", get_class($sendgrid));
11
+ }
12
+
13
+ public function testInitializers()
14
+ {
15
+ $sendgrid = new SendGrid("fake_username", "fake_password");
16
+
17
+ // test the working initializers that we currently have
18
+ $smtp = $sendgrid->smtp;
19
+ $web = $sendgrid->web;
20
+
21
+ $this->assertEquals("SendGrid\Smtp", get_class($smtp));
22
+ $this->assertEquals("SendGrid\Web", get_class($web));
23
+
24
+ try
25
+ {
26
+ $sendgrid->notanapi;
27
+ }
28
+ catch (Exception $e)
29
+ {
30
+ return;
31
+ }
32
+
33
+ $this->fail('A non object was instanciated');
34
+
35
+ }
36
+ }
lib/sendgrid-php/Test/a_loaderTest.php ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ //we'll use this class to autoload the appropriate files
4
+ require_once __dir__ . "/../vendor/autoload.php";
5
+
6
+ //include any mock classes
7
+ require_once __dir__ . "/Mock/Mock_loader.php";
lib/sendgrid-php/Test/phpunit.xml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <phpunit bootstrap="./a_loaderTest.php">
2
+ <testsuites>
3
+ <testsuite name="Services SendGrid Test Suite">
4
+ <directory>./</directory>
5
+ </testsuite>
6
+ </testsuites>
7
+ </phpunit>
lib/sendgrid-php/composer.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "sendgrid/sendgrid",
3
+ "description": "This library allows you to quickly and easily send emails through SendGrid using PHP.",
4
+ "version": "1.0.0",
5
+ "homepage": "http://sendgrid.com",
6
+ "license": "MIT",
7
+ "autoload": {
8
+ "files": ["SendGrid_loader.php"]
9
+ },
10
+ "require": {
11
+ "swiftmailer/swiftmailer": "v4.3.0"
12
+ },
13
+ "replace": {
14
+ "sendgrid/sendgrid-php": "*"
15
+ }
16
+ }
lib/sendgrid-php/composer.lock ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "hash": "59c1f81ac7d6ed6c902c71c92db62d84",
3
+ "packages": [
4
+ {
5
+ "name": "swiftmailer/swiftmailer",
6
+ "version": "v4.3.0",
7
+ "source": {
8
+ "type": "git",
9
+ "url": "git://github.com/swiftmailer/swiftmailer.git",
10
+ "reference": "v4.3.0"
11
+ },
12
+ "dist": {
13
+ "type": "zip",
14
+ "url": "https://github.com/swiftmailer/swiftmailer/archive/v4.3.0.zip",
15
+ "reference": "v4.3.0",
16
+ "shasum": ""
17
+ },
18
+ "require": {
19
+ "php": ">=5.2.4"
20
+ },
21
+ "type": "library",
22
+ "extra": {
23
+ "branch-alias": {
24
+ "dev-master": "4.3-dev"
25
+ }
26
+ },
27
+ "autoload": {
28
+ "files": [
29
+ "lib/swift_required.php"
30
+ ]
31
+ },
32
+ "notification-url": "https://packagist.org/downloads/",
33
+ "license": [
34
+ "LGPL"
35
+ ],
36
+ "authors": [
37
+ {
38
+ "name": "Fabien Potencier",
39
+ "email": "fabien@symfony.com"
40
+ },
41
+ {
42
+ "name": "Chris Corbyn"
43
+ }
44
+ ],
45
+ "description": "Swiftmailer, free feature-rich PHP mailer",
46
+ "homepage": "http://swiftmailer.org",
47
+ "keywords": [
48
+ "mail",
49
+ "mailer"
50
+ ],
51
+ "time": "2013-01-08 15:50:34"
52
+ }
53
+ ],
54
+ "packages-dev": [
55
+
56
+ ],
57
+ "aliases": [
58
+
59
+ ],
60
+ "minimum-stability": "stable",
61
+ "stability-flags": [
62
+
63
+ ],
64
+ "platform": [
65
+
66
+ ],
67
+ "platform-dev": [
68
+
69
+ ]
70
+ }
readme.txt ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === SendGrid ===
2
+ Contributors: team-rs
3
+ Donate link: http://sendgrid.com/
4
+ Tags: email, email reliability, email templates, sendgrid, smtp, transactional email, wp_mail,email infrastructure, email marketing, marketing email, deliverability, email deliverability, email delivery, email server, mail server, email integration, cloud email
5
+ Requires at least: 3.3
6
+ Tested up to: 3.5.1
7
+ Stable tag: 1.1
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Email Delivery. Simplified.
12
+
13
+ == Description ==
14
+
15
+ SendGrid's cloud-based email infrastructure relieves businesses of the cost and complexity of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time analytics along with flexible APIs that make custom integration a breeze.
16
+
17
+ The SendGrid plugin uses SMTP or API integration to send outgoing emails from your WordPress installation. It replaces the wp_mail function included with WordPress.
18
+
19
+ First, you need to have PHP-curl extension enabled. To send emails through SMTP you need to install also the 'Swift Mailer' plugin. After installing 'Swift Mailer' plugin, you must have PHP-short_open_tag setting enabled in your php.ini file.
20
+
21
+ To have the SendGrid plugin running after you have activated it, go to the plugin's settings page and set the SendGrid credentials, and how your email will be sent - either through SMTP or API.
22
+
23
+ You can also set default values for the "Name", "Sending Address" and the "Reply Address", so that you don't need to set these headers every time you want to send an email from your application.
24
+
25
+ Emails are tracked and automatically tagged for statistics within the SendGrid Dashboard. You can also add general tags to every email sent, as well as particular tags based on selected emails defined by your requirements.
26
+
27
+ There are a couple levels of integration between your WordPress installation and the SendGrid plugin:
28
+
29
+ * The simplest option is to Install it, Configure it, and the SendGrid plugin for WordPress will start sending your emails through SendGrid.
30
+ * We amended wp_mail() function so all email sends from wordpress should go through SendGrid. The wp_mail function is sending text emails as default, but you have an option of sending an email with HTML content.
31
+
32
+ How to use `wp_mail()` function:
33
+
34
+ We amended `wp_mail()` function so all email sends from wordpress should go through SendGrid.
35
+
36
+ You can send emails using the following function: `wp_mail($to, $subject, $message, $headers = '', $attachments = array())`
37
+
38
+ Where:
39
+
40
+ * `$to` - Array or comma-separated list of email addresses to send message.
41
+ * `$subject` - Email subject
42
+ * `$message` - Message contents
43
+ * `$headers` - Array or "\n" separated list of additional headers. Optional.
44
+ * `$attachments` - Array or "\n"/"," separated list of files to attach. Optional.
45
+
46
+ The wp_mail function is sending text emails as default. If you want to send an email with HTML content you have to set the content type to 'text/html' running `add_filter('wp_mail_content_type', 'set_html_content_type');` function before to `wp_mail()` one.
47
+
48
+ After wp_mail function you need to run the `remove_filter('wp_mail_content_type', 'set_html_content_type');` to remove the 'text/html' filter to avoid conflicts --http://core.trac.wordpress.org/ticket/23578
49
+
50
+ Example about how to send an HTML email using different headers:
51
+
52
+ `$subject = 'test plugin';
53
+ $message = 'testing wordpress plugin';
54
+ $to = 'address1@sendgrid.com, Address2 <address2@sendgrid.com@>, address3@sendgrid.com';
55
+ or
56
+ $to = array('address1@sendgrid.com', 'Address2 <address2@sendgrid.com>', 'address3@sendgrid.com');
57
+
58
+ $headers = array();
59
+ $headers[] = 'From: Me Myself <me@example.net>';
60
+ $headers[] = 'Cc: address4@sendgrid.com';
61
+ $headers[] = 'Bcc: address5@sendgrid.com';
62
+
63
+ $attachments = array('/tmp/img1.jpg', '/tmp/img2.jpg');
64
+
65
+ add_filter('wp_mail_content_type', 'set_html_content_type');
66
+ $mail = wp_mail($to, $subject, $message, $headers, $attachments);
67
+
68
+ remove_filter('wp_mail_content_type', 'set_html_content_type');`
69
+
70
+ == Installation ==
71
+
72
+ To upload the SendGrid Plugin .ZIP file:
73
+
74
+ 1. Upload the WordPress SendGrid Plugin to the /wp-contents/plugins/ folder.
75
+ 2. Activate the plugin from the "Plugins" menu in WordPress.
76
+ 3. Navigate to "Settings" -> "SendGrid Settings" and enter your SendGrid credentials
77
+
78
+ To auto install the SendGrid Plugin from the WordPress admin:
79
+
80
+ 1. Navigate to "Plugins" -> "Add New"
81
+ 2. Search for "SendGrid Plugin" and click "Install Now" for the "SendGrid Plugin" listing
82
+ 3. Activate the plugin from the "Plugins" menu in WordPress, or from the plugin installation screen.
83
+ 4. Navigate to "Settings" -> "SendGrid Settings" and enter your SendGrid credentials
84
+
85
+ == Frequently asked questions ==
86
+
87
+ = What credentials do I need to add on settings page =
88
+
89
+ SendGrid account credentials.
90
+
91
+ == Screenshots ==
92
+
93
+ 1. Go to Admin Panel, section Plugins and activate the SendGrid plugin. If you want to send emails through SMTP you need to install also the 'Swift Mailer' plugin.
94
+ 2. After activation "Settings" link will appear.
95
+ 3. Go to settings page and provide your SendGrid credentials. On this page you can set also the default "Name", "Sending Address" and "Reply Address".
96
+ 4. If you provide valid credentials, a form which can be used to send test emails will appear. Here you can test the plugin sending some emails.
97
+ 5. Header provided in the send test email form.
98
+ 6. If you click in the right corner from the top of the page on the "Help" button, a popup window with more information will appear.
99
+
100
+ == Changelog ==
101
+
102
+ = 1.0 =
103
+ * Fixed issue: Add error message when PHP-curl extension is not enabled.
104
+
105
+ == Upgrade notice ==
106
+
107
+ = 1.0 =
view/css/sendgrid.css ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ root {
2
+ display: block;
3
+ }
4
+
5
+ h2.title {
6
+ text-align: center;
7
+ }
8
+
9
+ .stuffbox {
10
+ margin: 10px;
11
+ width: 740px;
12
+ display: block;
13
+ clear: both;
14
+ }
15
+
16
+ .stuffbox .button {
17
+ margin-top: 10px;
18
+ }
19
+
20
+ .submit-button {
21
+ width: 100%;
22
+ text-align: center;
23
+ }
24
+
25
+ .stuffbox p {
26
+ padding: 7px 7px 7px 7px;
27
+ }
28
+ .stuffbox input, select, textarea {
29
+ width: 60%;
30
+ padding: 5px 0 5px 0;
31
+ }
32
+
33
+ .stuffbox h3 {
34
+ cursor: default !important;
35
+ padding: 3px 3px 3px 10px;
36
+ }
37
+
38
+ .pull-left {
39
+ float: left;
40
+ }
41
+ .pull-right {
42
+ float: right;
43
+ }
44
+
45
+ .padding5 {
46
+ padding: 10px 10px 10px 10px;
47
+ }
48
+ .clearfix:before, .clearfix:after {
49
+ content: ".";
50
+ display: block;
51
+ height: 0;
52
+ overflow: hidden;
53
+ }
54
+
55
+ .clearfix:after {
56
+ clear: both;
57
+ }
58
+
59
+ .clearfix {
60
+ zoom: 1; /* IE < 8 */
61
+ }
62
+
63
+ .send-failed, .save-error {
64
+ text-align: center;
65
+ padding: 7px 0 7px 0;
66
+ background-color: #ffebe8;
67
+ border-color: #c00;
68
+ margin: 5px 0 15px;
69
+ -webkit-border-radius: 3px;
70
+ border-radius: 3px;
71
+ border-width: 1px;
72
+ border-style: solid;
73
+ }
74
+
75
+ .send-success, .save-success {
76
+ text-align: center;
77
+ padding: 7px 0 7px 0;
78
+ background-color: #ffffe0;
79
+ border-color: #e6db55;
80
+ margin: 5px 0 15px;
81
+ -webkit-border-radius: 3px;
82
+ border-radius: 3px;
83
+ border-width: 1px;
84
+ border-style: solid;
85
+ }
86
+
87
+ .code {
88
+ background-color: #F8F8F8;
89
+ border: 1px solid #DDDDDD;
90
+ border-radius: 3px 3px 3px 3px;
91
+ font-size: 13px;
92
+ line-height: 19px;
93
+ overflow: auto;
94
+ padding: 6px 10px;
95
+ }
view/images/logo.png ADDED
Binary file
view/sendgrid_settings.php ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <link rel="stylesheet" href="<?php echo plugin_dir_url(__FILE__) . 'css/sendgrid.css'; ?>" type="text/css">
2
+
3
+ <div class="wrap">
4
+ <div class="stuffbox">
5
+ <a href="http://sendgrid.com" target="_blank">
6
+ <img src="<?php echo plugins_url('/images/logo.png', __FILE__) ?>" width="100" alt="" />
7
+ </a>
8
+ <h2 class="title"><?php echo _e('SendGrid Options') ?></h2>
9
+ <?php if ($status == 'save-error' or $status == 'save-success'): ?>
10
+ <div id="message" class="<?php echo $status ?>">
11
+ <strong><?php echo $message ?></strong>
12
+ </div>
13
+ <?php endif; ?>
14
+ <h3><?php echo _e('SendGrid credentials') ?></h3>
15
+ <form class="form-table" name="sendgrid_form" method="POST" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>">
16
+ <table class="form-table">
17
+ <tr class="top">
18
+ <th scope="row"><?php _e("Username: "); ?></th>
19
+ <td>
20
+ <div class="inside">
21
+ <input type="text" required="true" name="sendgrid_user" value="<?php echo $user; ?>" size="20">
22
+ </div>
23
+ </td>
24
+ </tr>
25
+ <tr class="top">
26
+ <th scope="row"><?php _e("Password: "); ?></th>
27
+ <td>
28
+ <div class="inside">
29
+ <input type="password" required="true" name="sendgrid_pwd" value="<?php echo $password; ?>" size="20">
30
+ </div>
31
+ </td>
32
+ </tr>
33
+ <tr class="top">
34
+ <th scope="row"><?php _e("Send Mail with: "); ?></th>
35
+ <td>
36
+ <div class="inside">
37
+ <select name="sendgrid_api">
38
+ <option value="api" id="api" <?php echo ($method == 'api') ? 'selected' : '' ?>><?php _e('API') ?></option>
39
+ <option value="smtp" id="smtp" <?php echo ($method == 'smtp') ? 'selected' : '' ?>><?php _e('SMTP') ?></option>
40
+ </select>
41
+ </div>
42
+ </td>
43
+ </tr>
44
+ </table>
45
+ <br />
46
+ <h3><?php _e('Mail settings') ?></h3>
47
+ <table class="form-table">
48
+ <tr class="top">
49
+ <th scope="row"><?php _e("Name: "); ?></th>
50
+ <td>
51
+ <div class="inside">
52
+ <?php _e('Name as it will appear in recipient clients.') ?>
53
+ <br />
54
+ <input type="text" name="sendgrid_name" value="<?php echo $name; ?>" size="20">
55
+ </div>
56
+ </td>
57
+ </tr>
58
+ <tr class="top">
59
+ <th scope="row"><?php _e("Sending Address: "); ?></th>
60
+ <td>
61
+ <div class="inside">
62
+ <?php _e('Email address from which message will be sent,') ?>
63
+ <br />
64
+ <input type="email" name="sendgrid_email" value="<?php echo $email; ?>" size="20">
65
+ </div>
66
+ </td>
67
+ </tr>
68
+ <tr class="top">
69
+ <th scope="row"><?php _e("Reply Address: "); ?></th>
70
+ <td>
71
+ <div class="inside">
72
+ <?php _e('Email address where replies will be returned.') ?>
73
+ <br />
74
+ <input type="email" name="sendgrid_reply_to" value="<?php echo $reply_to; ?>" size="20">
75
+ <br />
76
+ <span>
77
+ <small>
78
+ <em>
79
+ <?php _e('Leave blank to use Sending Address.') ?>
80
+ </em>
81
+ </small>
82
+ </span>
83
+ </div>
84
+ </td>
85
+ </tr>
86
+ </table>
87
+ <div class="submit-button">
88
+ <p class="submit">
89
+ <input class="button button-primary" type="submit" name="Submit" value="<?php _e('Update Settings') ?>" />
90
+ </p>
91
+ </div>
92
+ </form>
93
+ </div>
94
+ <br />
95
+ <?php if ($valid_credentials): ?>
96
+ <div class="stuffbox">
97
+ <h2 class="title"><?php _e('SendGrid Test') ?></h2>
98
+ <?php if ($status == 'send-failed' or $status == 'send-success'): ?>
99
+ <div id="message" class="<?php echo $status ?>">
100
+ <strong><?php echo $message ?></strong>
101
+ </div>
102
+ <?php endif; ?>
103
+ <h3><?php _e('Send a test email with these settings') ?></h3>
104
+ <form name="sendgrid_test" method="POST" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>">
105
+ <table class="form-table">
106
+ <tr class="top">
107
+ <th scope="row"><?php _e("To: "); ?></th>
108
+ <td>
109
+ <div class="inside">
110
+ <input type="email" name="sendgrid_to" required="true" value="<?php echo $success ? '' : $to; ?>" size="20">
111
+ </div>
112
+ </td>
113
+ </tr>
114
+ <tr class="top">
115
+ <th scope="row"><?php _e("Subject: "); ?></th>
116
+ <td>
117
+ <div class="inside">
118
+ <input type="text" name="sendgrid_subj" required="true" value="<?php echo $success ? '' : $subject; ?>" size="20">
119
+ </div>
120
+ </td>
121
+ </tr>
122
+ <tr class="top">
123
+ <th scope="row"><?php _e("Body: "); ?></th>
124
+ <td>
125
+ <div class="inside">
126
+ <textarea name="sendgrid_body" rows="5"><?php echo $success ? '' : $body; ?></textarea>
127
+ </div>
128
+ </td>
129
+ </tr>
130
+ <tr class="top">
131
+ <th scope="row"><?php _e("Headers: "); ?></th>
132
+ <td>
133
+ <div class="inside">
134
+ <textarea name="sendgrid_headers" rows="3"><?php echo $success ? '' : $headers; ?></textarea>
135
+ </div>
136
+ </td>
137
+ </tr>
138
+ </table>
139
+ <input type="hidden" name="email_test" value="true"/>
140
+ <div class="submit-button">
141
+ <p class="submit">
142
+ <input class="button button-primary" type="submit" name="Submit" value="<?php _e('Send') ?>" />
143
+ </p>
144
+ </div>
145
+ </form>
146
+ </div>
147
+ <?php endif; ?>
148
+ </div>
wpsendgrid.php ADDED
@@ -0,0 +1,418 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: SendGrid
4
+ Plugin URI: http://sendgrid.com
5
+ Description: Email Delivery. Simplified. SendGrid's cloud-based email infrastructure relieves businesses of the cost and complexity of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time analytics along with flexible APIs that make custom integration a breeze.
6
+ Version: 1.0
7
+ Author: SendGrid
8
+ Author URI: http://sendgrid.com
9
+ License: GPLv2
10
+ */
11
+
12
+ require_once plugin_dir_path( __FILE__ ) . '/lib/SendGridSettings.php';
13
+ require_once plugin_dir_path( __FILE__ ) . '/lib/sendgrid-php/SendGrid_loader.php';
14
+
15
+ $sendgridSettings = new wpSendGridSettings();
16
+
17
+ if (!function_exists('wp_mail'))
18
+ {
19
+ /**
20
+ * Send mail, similar to PHP's mail
21
+ *
22
+ * A true return value does not automatically mean that the user received the
23
+ * email successfully. It just only means that the method used was able to
24
+ * process the request without any errors.
25
+ *
26
+ * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
27
+ * creating a from address like 'Name <email@address.com>' when both are set. If
28
+ * just 'wp_mail_from' is set, then just the email address will be used with no
29
+ * name.
30
+ *
31
+ * The default content type is 'text/plain' which does not allow using HTML.
32
+ * However, you can set the content type of the email by using the
33
+ * 'wp_mail_content_type' filter.
34
+ *
35
+ * The default charset is based on the charset used on the blog. The charset can
36
+ * be set using the 'wp_mail_charset' filter.
37
+ *
38
+ * @since 1.2.1
39
+ * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
40
+ * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
41
+ * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
42
+ * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
43
+ * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
44
+ *
45
+ * @param string|array $to Array or comma-separated list of email addresses to send message.
46
+ * @param string $subject Email subject
47
+ * @param string $message Message contents
48
+ * @param string|array $headers Optional. Additional headers.
49
+ * @param string|array $attachments Optional. Files to attach.
50
+ * @return bool Whether the email contents were sent successfully.
51
+ */
52
+ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
53
+ {
54
+ $sendgrid = new SendGrid(get_option('sendgrid_user'), get_option('sendgrid_pwd'));
55
+ $mail = new SendGrid\Mail();
56
+ $method = get_option('sendgrid_api');
57
+ // Compact the input, apply the filters, and extract them back out
58
+ extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
59
+
60
+ // prepare attachments
61
+ $attached_files = array();
62
+ if (!empty($attachments))
63
+ {
64
+ if (!is_array($attachments))
65
+ {
66
+ $pos = strpos(',', $attachments);
67
+ if ($pos !== false)
68
+ {
69
+ $attachments = preg_split('/,\s*/', $attachments);
70
+ }
71
+ else
72
+ {
73
+ $attachments = explode("\n", str_replace("\r\n", "\n", $attachments));
74
+ }
75
+ }
76
+
77
+ if (is_array($attachments)) {
78
+ foreach ($attachments as $attachment) {
79
+ if (file_exists($attachment)) {
80
+ $attached_files[] = $attachment;
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ // Headers
87
+ if (empty($headers)) {
88
+ $headers = array();
89
+ } else {
90
+ if (!is_array($headers)) {
91
+ // Explode the headers out, so this function can take both
92
+ // string headers and an array of headers.
93
+ $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
94
+ } else {
95
+ $tempheaders = $headers;
96
+ }
97
+ $headers = array();
98
+ $cc = array();
99
+ $bcc = array();
100
+
101
+ // If it's actually got contents
102
+ if ( !empty( $tempheaders ) ) {
103
+ // Iterate through the raw headers
104
+ foreach ( (array) $tempheaders as $header ) {
105
+ if ( strpos($header, ':') === false ) {
106
+ if ( false !== stripos( $header, 'boundary=' ) ) {
107
+ $parts = preg_split('/boundary=/i', trim( $header ) );
108
+ $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
109
+ }
110
+ continue;
111
+ }
112
+ // Explode them out
113
+ list( $name, $content ) = explode( ':', trim( $header ), 2 );
114
+
115
+ // Cleanup crew
116
+ $name = trim( $name );
117
+ $content = trim( $content );
118
+
119
+ switch ( strtolower( $name ) ) {
120
+ // Mainly for legacy -- process a From: header if it's there
121
+ case 'from':
122
+ if ( strpos($content, '<' ) !== false ) {
123
+ // So... making my life hard again?
124
+ $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
125
+ $from_name = str_replace( '"', '', $from_name );
126
+ $from_name = trim( $from_name );
127
+
128
+ $from_email = substr( $content, strpos( $content, '<' ) + 1 );
129
+ $from_email = str_replace( '>', '', $from_email );
130
+ $from_email = trim( $from_email );
131
+ } else {
132
+ $from_email = trim( $content );
133
+ }
134
+ break;
135
+ case 'content-type':
136
+ if ( strpos( $content, ';' ) !== false ) {
137
+ list( $type, $charset ) = explode( ';', $content );
138
+ $content_type = trim( $type );
139
+ if ( false !== stripos( $charset, 'charset=' ) ) {
140
+ $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
141
+ } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
142
+ $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
143
+ $charset = '';
144
+ }
145
+ } else {
146
+ $content_type = trim( $content );
147
+ }
148
+ break;
149
+ case 'cc':
150
+ $cc = array_merge( (array) $cc, explode( ',', $content ) );
151
+ foreach ($cc as $key => $recipient)
152
+ {
153
+ $cc[$key] = trim($recipient);
154
+ }
155
+ break;
156
+ case 'bcc':
157
+ $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
158
+ foreach ($bcc as $key => $recipient)
159
+ {
160
+ $bcc[$key] = trim($recipient);
161
+ }
162
+ break;
163
+ default:
164
+ // Add it to our grand headers array
165
+ $headers[trim( $name )] = trim( $content );
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ }
171
+
172
+ // From email and name
173
+ // If we don't have a name from the input headers
174
+ if ( !isset( $from_name ) )
175
+ $from_name = get_option('sendgrid_from_name');
176
+
177
+ /* If we don't have an email from the input headers default to wordpress@$sitename
178
+ * Some hosts will block outgoing mail from this address if it doesn't exist but
179
+ * there's no easy alternative. Defaulting to admin_email might appear to be another
180
+ * option but some hosts may refuse to relay mail from an unknown domain. See
181
+ * http://trac.wordpress.org/ticket/5007.
182
+ */
183
+
184
+ if ( !isset( $from_email ) ) {
185
+ $from_email = trim(get_option('sendgrid_from_email'));
186
+ if (!$from_email)
187
+ {
188
+ // Get the site domain and get rid of www.
189
+ $sitename = strtolower( $_SERVER['SERVER_NAME'] );
190
+ if ( substr( $sitename, 0, 4 ) == 'www.' ) {
191
+ $sitename = substr( $sitename, 4 );
192
+ }
193
+
194
+ $from_email = 'wordpress@' . $sitename;
195
+ }
196
+ }
197
+
198
+ // Plugin authors can override the potentially troublesome default
199
+ $from_email = apply_filters( 'wp_mail_from' , $from_email );
200
+ $from_name = apply_filters( 'wp_mail_from_name', $from_name );
201
+
202
+ // Set destination addresses
203
+ if ( !is_array( $to ) )
204
+ $to = explode( ',', $to );
205
+
206
+ // Add any CC and BCC recipients
207
+ if (!empty( $cc ))
208
+ {
209
+ foreach ((array) $cc as $key => $recipient)
210
+ {
211
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
212
+ if (preg_match('/(.*)<(.+)>/', $recipient, $matches))
213
+ {
214
+ if ( count( $matches ) == 3 )
215
+ {
216
+ $cc[$key] = trim($matches[2]);
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ if ( !empty( $bcc ) ) {
223
+ foreach ( (array) $bcc as $key => $recipient) {
224
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
225
+ if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
226
+ if ( count( $matches ) == 3 )
227
+ {
228
+ $bcc[$key] = trim($matches[2]);
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ if (($method == 'api') and (count($cc) or count($bcc)))
235
+ {
236
+ foreach ((array) $to as $key => $recipient)
237
+ {
238
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
239
+ if (preg_match( '/(.*)<(.+)>/', $recipient, $matches ) )
240
+ {
241
+ if ( count( $matches ) == 3 ) {
242
+ $to[$key] = trim($matches[2]);
243
+ }
244
+ }
245
+ }
246
+ }
247
+ // Set Content-Type and charset
248
+ // If we don't have a content-type from the input headers
249
+ if ( !isset( $content_type ) )
250
+ $content_type = 'text/plain';
251
+
252
+ $content_type = apply_filters( 'wp_mail_content_type', $content_type );
253
+
254
+ $mail->setTos($to)
255
+ ->setSubject($subject)
256
+ ->setText($message)
257
+ ->setFrom($from_email);
258
+
259
+ // send HTML content
260
+ if ($content_type !== 'text/plain')
261
+ {
262
+ $mail->setHtml($message);
263
+ }
264
+ // set from name
265
+ if ($from_email)
266
+ {
267
+ $mail->setFromName($from_name);
268
+ }
269
+ // set from cc
270
+ if (count($cc))
271
+ {
272
+ $mail->setCcs($cc);
273
+ }
274
+ // set from bcc
275
+ if (count($bcc))
276
+ {
277
+ $mail->setBccs($bcc);
278
+ }
279
+ $reply_to = trim(get_option('sendgrid_reply_to'));
280
+ if ($reply_to)
281
+ {
282
+ $mail->setReplyTo($reply_to);
283
+ }
284
+ // add attachemnts
285
+ if (count($attached_files))
286
+ {
287
+ $mail->setAttachments($attached_files);
288
+ }
289
+
290
+ // Send!
291
+ try
292
+ {
293
+ if ($method == 'api')
294
+ {
295
+ return $sendgrid->web->send($mail);
296
+ }
297
+ elseif ($method == 'smtp')
298
+ {
299
+ if (class_exists('Swift'))
300
+ {
301
+ return $sendgrid->smtp->send($mail);
302
+ }
303
+ else
304
+ {
305
+ return 'Error: Swift Class not loaded. Please activate Swift plugin or use API.';
306
+ }
307
+ }
308
+ }
309
+ catch (Exception $e)
310
+ {
311
+ return $e->getMessage();
312
+ }
313
+
314
+ return false;
315
+ }
316
+ }
317
+ else
318
+ {
319
+ // wp_mail has been declared by another process or plugin, so you won't be able to use SENDGRID until the problem is solved.
320
+ add_action('admin_notices', 'adminNotices');
321
+ }
322
+
323
+ $plugin = plugin_basename(__FILE__);
324
+ add_filter("plugin_action_links_$plugin", 'sendgrid_settings_link' );
325
+ add_filter('contextual_help', 'showContextualHelp', 10, 2);
326
+
327
+ /**
328
+ * Add settings link on the plugin page
329
+ *
330
+ * @param mixed $links links
331
+ * @return mixed links
332
+ */
333
+ function sendgrid_settings_link($links)
334
+ {
335
+ $settings_link = '<a href="options-general.php?page=sendgrid-settings.php">Settings</a>';
336
+ array_unshift($links, $settings_link);
337
+
338
+ return $links;
339
+ }
340
+
341
+ /**
342
+ * Generates source of contextual help panel.
343
+ *
344
+ * @param mixed $contextual_help contextual help
345
+ * @param integer $screen_id screen id
346
+ * @param integer $screen screen
347
+ * @return string
348
+ */
349
+ function showContextualHelp($contextual_help, $screen_id, $screen)
350
+ {
351
+ $text = '<p>' . __('Email Delivery. Simplified.') . '</p>' .
352
+ '<p>' . __("SendGrid's cloud-based email infrastructure relieves businesses of the cost and complexity " .
353
+ "of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time " .
354
+ "analytics along with flexible APIs that make custom integration a breeze.") . '</p>' .
355
+ '<p><br />' . __('To have the SendGrid plugin running after you activated it, please go to plugin\'s ' .
356
+ 'settings page and set the SendGrid credentials, and the way your email will be sent through SMTP or API.') .
357
+ '<br />' . __('You can also set default values for the \'Name\', \'Sending Address\' and the \'Reply Address\' ' .
358
+ ' in this page, so that you don\'t need to set these headers every time you want to send an email from your ' .
359
+ 'application.') . '</p>' .
360
+ '<p>' . __('After you have done these configurations, all your emails sent from your WordPress installation will ' .
361
+ 'go through SendGrid.') . '</p><p>' . __('Now let see how simple is to send a text email:') . '<br />' .
362
+ '<div class="code">' . __('&lt;?php wp_mail(\'to@address.com\', \'Email Subject\', \'Email Body\'); ?&gt;') . '</div><br />' .
363
+
364
+ __('If you want to use additional headers, here you have a more complex example:') . '<br />' .
365
+
366
+ '<div class="code">$subject = \'test plugin\'<br />' .
367
+ '$message = \'testing wordpress plugin\'<br />' .
368
+ '$to = array(\'address1@sendgrid.com\', \'Address2 <address2@sendgrid.com>\', \'address3@sendgrid.com\');<br /><br />' .
369
+
370
+ '$headers = array();<br />' .
371
+ '$headers[] = \'From: Me Myself <me@example.net>\';<br />' .
372
+ '$headers[] = \'Cc: address4@sendgrid.com\';<br />' .
373
+ '$headers[] = \'Bcc: address5@sendgrid.com\';<br /><br />' .
374
+
375
+ '$attachments = array(\'/tmp/img1.jpg\', \'/tmp/img2.jpg\');<br /><br />' .
376
+
377
+ 'add_filter(\'wp_mail_content_type\', \'set_html_content_type\');<br />' .
378
+ '$mail = wp_mail($to, $subject, $message, $headers, $attachments);<br />' .
379
+
380
+ 'remove_filter(\'wp_mail_content_type\', \'set_html_content_type\');</div><br /><br />' .
381
+
382
+ 'Where:<br />' .
383
+ '<ul>' .
384
+ '<li>$to - ' . __('Array or comma-separated list of email addresses to send message.') . '</li>' .
385
+ '<li>$subject - ' . __('Email subject') . '</li>' .
386
+ '<li>$message - ' . __('Message contents') . '</li>' .
387
+ '<li>$headers - ' . __('Array or "\n" separated list of additional headers. Optional.') . '</li>' .
388
+ '<li>$attachments - ' . __('Array or "\n"/"," separated list of files to attach. Optional.') . '</li>' .
389
+ '</ul>' .
390
+ __('The wp_mail function is sending text emails as default. If you want to send an email with HTML content you have ' .
391
+ 'to set the content type to \'text/html\' running') . ' <span class="code">add_filter(\'wp_mail_content_type\', ' .
392
+ '\'set_html_content_type\');</span> ' . __('function before to wp_mail() one') . '.<br /><br />' .
393
+ __('After wp_mail function you need to run the ') . '<span class="code">remove_filter(\'wp_mail_content_type\', ' .
394
+ '\'set_html_content_type\');</span>' . __(' to remove the \'text/html\' filter to avoid conflicts') .
395
+ ' -- http://core.trac.wordpress.org/ticket/23578';
396
+
397
+ return $text;
398
+ }
399
+
400
+ /**
401
+ * Return the content type used to send html emails
402
+ *
403
+ * return string Conteny-type needed to send HTML emails
404
+ */
405
+ function set_html_content_type()
406
+ {
407
+ return 'text/html';
408
+ }
409
+
410
+ /**
411
+ * Display the notice that wp_mail function was declared by another plugin
412
+ *
413
+ * return void
414
+ */
415
+ function adminNotices()
416
+ {
417
+ echo '<div class="error"><p>'.__('SendGrid: wp_mail has been declared by another process or plugin, so you won\'t be able to use SendGrid until the conflict is solved.') . '</p></div>';
418
+ }