103 |
- |
1 |
<?php
|
|
|
2 |
namespace SendGrid;
|
|
|
3 |
|
|
|
4 |
// If you are using Composer
|
|
|
5 |
require __DIR__ . '<PATH_TO>/vendor/autoload.php';
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
function helloEmail()
|
|
|
9 |
{
|
|
|
10 |
$from = new Email(null, "test@example.com");
|
|
|
11 |
$subject = "Hello World from the SendGrid PHP Library";
|
|
|
12 |
$to = new Email(null, "test@example.com");
|
|
|
13 |
$content = new Content("text/plain", "some text here");
|
|
|
14 |
$mail = new Mail($from, $subject, $to, $content);
|
|
|
15 |
$to = new Email(null, "test2@example.com");
|
|
|
16 |
$mail->personalization[0]->addTo($to);
|
|
|
17 |
|
|
|
18 |
//echo json_encode($mail, JSON_PRETTY_PRINT), "\n";
|
|
|
19 |
return $mail;
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
function kitchenSink()
|
|
|
23 |
{
|
|
|
24 |
$mail = new Mail();
|
|
|
25 |
|
|
|
26 |
$email = new Email("DX", "test@example.com");
|
|
|
27 |
$mail->setFrom($email);
|
|
|
28 |
|
|
|
29 |
$mail->setSubject("Hello World from the SendGrid PHP Library");
|
|
|
30 |
|
|
|
31 |
$personalization = new Personalization();
|
|
|
32 |
$email1 = new Email("Example User", "test1@example.com");
|
|
|
33 |
$personalization->addTo($email1);
|
|
|
34 |
$email2 = new Email("Example User", "test2@example.com");
|
|
|
35 |
$personalization->addTo($email2);
|
|
|
36 |
$email3 = new Email("Example User", "test3@example.com");
|
|
|
37 |
$personalization->addCc($email3);
|
|
|
38 |
$email4 = new Email("Example User", "test4@example.com");
|
|
|
39 |
$personalization->addCc($email4);
|
|
|
40 |
$email5 = new Email("Example User", "test5@example.com");
|
|
|
41 |
$personalization->addBcc($email5);
|
|
|
42 |
$email6 = new Email("Example User", "test6@example.com");
|
|
|
43 |
$personalization->addBcc($email6);
|
|
|
44 |
$personalization->setSubject("Hello World from the SendGrid PHP Library");
|
|
|
45 |
$personalization->addHeader("X-Test", "test");
|
|
|
46 |
$personalization->addHeader("X-Mock", "true");
|
|
|
47 |
$personalization->addSubstitution("%name%", "Example User");
|
|
|
48 |
$personalization->addSubstitution("%city%", "Denver");
|
|
|
49 |
$personalization->addCustomArg("user_id", "343");
|
|
|
50 |
$personalization->addCustomArg("type", "marketing");
|
|
|
51 |
$personalization->setSendAt(1443636843);
|
|
|
52 |
$mail->addPersonalization($personalization);
|
|
|
53 |
|
|
|
54 |
$personalization2 = new Personalization();
|
|
|
55 |
$email7 = new Email("Example User", "test7@example.com");
|
|
|
56 |
$personalization2->addTo($email7);
|
|
|
57 |
$email8 = new Email("Example User", "test8@example.com");
|
|
|
58 |
$personalization2->addTo($email8);
|
|
|
59 |
$email9 = new Email("Example User", "test9@example.com");
|
|
|
60 |
$personalization2->addCc($email9);
|
|
|
61 |
$email10 = new Email("Example User", "test10@example.com");
|
|
|
62 |
$personalization2->addCc($email10);
|
|
|
63 |
$email11 = new Email("Example User", "test11@example.com");
|
|
|
64 |
$personalization2->addBcc($email11);
|
|
|
65 |
$email12 = new Email("Example User", "test12@example.com");
|
|
|
66 |
$personalization2->addBcc($email12);
|
|
|
67 |
$personalization2->setSubject("Hello World from the SendGrid PHP Library");
|
|
|
68 |
$personalization2->addHeader("X-Test", "test");
|
|
|
69 |
$personalization2->addHeader("X-Mock", "true");
|
|
|
70 |
$personalization2->addSubstitution("%name%", "Example User");
|
|
|
71 |
$personalization2->addSubstitution("%city%", "Denver");
|
|
|
72 |
$personalization2->addCustomArg("user_id", "343");
|
|
|
73 |
$personalization2->addCustomArg("type", "marketing");
|
|
|
74 |
$personalization2->setSendAt(1443636843);
|
|
|
75 |
$mail->addPersonalization($personalization2);
|
|
|
76 |
|
|
|
77 |
$content = new Content("text/plain", "some text here");
|
|
|
78 |
$mail->addContent($content);
|
|
|
79 |
$content = new Content("text/html", "<html><body>some text here</body></html>");
|
|
|
80 |
$mail->addContent($content);
|
|
|
81 |
|
|
|
82 |
$attachment = new Attachment();
|
|
|
83 |
$attachment->setContent("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12");
|
|
|
84 |
$attachment->setType("application/pdf");
|
|
|
85 |
$attachment->setFilename("balance_001.pdf");
|
|
|
86 |
$attachment->setDisposition("attachment");
|
|
|
87 |
$attachment->setContentId("Balance Sheet");
|
|
|
88 |
$mail->addAttachment($attachment);
|
|
|
89 |
|
|
|
90 |
$attachment2 = new Attachment();
|
|
|
91 |
$attachment2->setContent("BwdW");
|
|
|
92 |
$attachment2->setType("image/png");
|
|
|
93 |
$attachment2->setFilename("banner.png");
|
|
|
94 |
$attachment2->setDisposition("inline");
|
|
|
95 |
$attachment2->setContentId("Banner");
|
|
|
96 |
$mail->addAttachment($attachment2);
|
|
|
97 |
|
|
|
98 |
$mail->setTemplateId("439b6d66-4408-4ead-83de-5c83c2ee313a");
|
|
|
99 |
|
|
|
100 |
# This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work
|
|
|
101 |
# $mail->setBatchID("sengrid_batch_id");
|
|
|
102 |
|
|
|
103 |
$mail->addSection("%section1%", "Substitution Text for Section 1");
|
|
|
104 |
$mail->addSection("%section2%", "Substitution Text for Section 2");
|
|
|
105 |
|
|
|
106 |
$mail->addHeader("X-Test1", "1");
|
|
|
107 |
$mail->addHeader("X-Test2", "2");
|
|
|
108 |
|
|
|
109 |
$mail->addCategory("May");
|
|
|
110 |
$mail->addCategory("2016");
|
|
|
111 |
|
|
|
112 |
$mail->addCustomArg("campaign", "welcome");
|
|
|
113 |
$mail->addCustomArg("weekday", "morning");
|
|
|
114 |
|
|
|
115 |
$mail->setSendAt(1443636842);
|
|
|
116 |
|
|
|
117 |
$asm = new ASM();
|
|
|
118 |
$asm->setGroupId(99);
|
|
|
119 |
$asm->setGroupsToDisplay([4,5,6,7,8]);
|
|
|
120 |
$mail->setASM($asm);
|
|
|
121 |
|
|
|
122 |
$mail->setIpPoolName("23");
|
|
|
123 |
|
|
|
124 |
$mail_settings = new MailSettings();
|
|
|
125 |
$bcc_settings = new BccSettings();
|
|
|
126 |
$bcc_settings->setEnable(true);
|
|
|
127 |
$bcc_settings->setEmail("test@example.com");
|
|
|
128 |
$mail_settings->setBccSettings($bcc_settings);
|
|
|
129 |
$sandbox_mode = new SandBoxMode();
|
|
|
130 |
$sandbox_mode->setEnable(true);
|
|
|
131 |
$mail_settings->setSandboxMode($sandbox_mode);
|
|
|
132 |
$bypass_list_management = new BypassListManagement();
|
|
|
133 |
$bypass_list_management->setEnable(true);
|
|
|
134 |
$mail_settings->setBypassListManagement($bypass_list_management);
|
|
|
135 |
$footer = new Footer();
|
|
|
136 |
$footer->setEnable(true);
|
|
|
137 |
$footer->setText("Footer Text");
|
|
|
138 |
$footer->setHtml("<html><body>Footer Text</body></html>");
|
|
|
139 |
$mail_settings->setFooter($footer);
|
|
|
140 |
$spam_check = new SpamCheck();
|
|
|
141 |
$spam_check->setEnable(true);
|
|
|
142 |
$spam_check->setThreshold(1);
|
|
|
143 |
$spam_check->setPostToUrl("https://spamcatcher.sendgrid.com");
|
|
|
144 |
$mail_settings->setSpamCheck($spam_check);
|
|
|
145 |
$mail->setMailSettings($mail_settings);
|
|
|
146 |
|
|
|
147 |
$tracking_settings = new TrackingSettings();
|
|
|
148 |
$click_tracking = new ClickTracking();
|
|
|
149 |
$click_tracking->setEnable(true);
|
|
|
150 |
$click_tracking->setEnableText(true);
|
|
|
151 |
$tracking_settings->setClickTracking($click_tracking);
|
|
|
152 |
$open_tracking = new OpenTracking();
|
|
|
153 |
$open_tracking->setEnable(true);
|
|
|
154 |
$open_tracking->setSubstitutionTag("Optional tag to replace with the open image in the body of the message");
|
|
|
155 |
$tracking_settings->setOpenTracking($open_tracking);
|
|
|
156 |
$subscription_tracking = new SubscriptionTracking();
|
|
|
157 |
$subscription_tracking->setEnable(true);
|
|
|
158 |
$subscription_tracking->setText("text to insert into the text/plain portion of the message");
|
|
|
159 |
$subscription_tracking->setHtml("<html><body>html to insert into the text/html portion of the message</body></html>");
|
|
|
160 |
$subscription_tracking->setSubstitutionTag("Optional tag to replace with the open image in the body of the message");
|
|
|
161 |
$tracking_settings->setSubscriptionTracking($subscription_tracking);
|
|
|
162 |
$ganalytics = new Ganalytics();
|
|
|
163 |
$ganalytics->setEnable(true);
|
|
|
164 |
$ganalytics->setCampaignSource("some source");
|
|
|
165 |
$ganalytics->setCampaignTerm("some term");
|
|
|
166 |
$ganalytics->setCampaignContent("some content");
|
|
|
167 |
$ganalytics->setCampaignName("some name");
|
|
|
168 |
$ganalytics->setCampaignMedium("some medium");
|
|
|
169 |
$tracking_settings->setGanalytics($ganalytics);
|
|
|
170 |
$mail->setTrackingSettings($tracking_settings);
|
|
|
171 |
|
|
|
172 |
$reply_to = new ReplyTo("test@example.com");
|
|
|
173 |
$mail->setReplyTo($reply_to);
|
|
|
174 |
|
|
|
175 |
//echo json_encode($mail, JSON_PRETTY_PRINT), "\n";
|
|
|
176 |
return $mail;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
function sendHelloEmail()
|
|
|
180 |
{
|
|
|
181 |
$apiKey = getenv('SENDGRID_API_KEY');
|
|
|
182 |
$sg = new \SendGrid($apiKey);
|
|
|
183 |
|
|
|
184 |
$request_body = helloEmail();
|
|
|
185 |
$response = $sg->client->mail()->send()->post($request_body);
|
|
|
186 |
echo $response->statusCode();
|
|
|
187 |
echo $response->body();
|
|
|
188 |
echo $response->headers();
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
function sendKitchenSink()
|
|
|
192 |
{
|
|
|
193 |
$apiKey = getenv('SENDGRID_API_KEY');
|
|
|
194 |
$sg = new \SendGrid($apiKey);
|
|
|
195 |
|
|
|
196 |
$request_body = kitchenSink();
|
|
|
197 |
$response = $sg->client->mail()->send()->post($request_body);
|
|
|
198 |
echo $response->statusCode();
|
|
|
199 |
echo $response->body();
|
|
|
200 |
echo $response->headers();
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
sendHelloEmail(); // this will actually send an email
|
|
|
204 |
sendKitchenSink(); // this will only send an email if you set SandBox Mode to false
|
|
|
205 |
?>
|
|
|
206 |
|
|
|
207 |
|