Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-php/issues) or make a pull request for any use cases you would like us to document here. Thank you!
2
 
3
# Table of Contents
4
* [Attachments](#attachments)
5
* [Transactional Templates](#transactional_templates)
6
 
7
<a name="attachments"></a>
8
# Attachments
9
 
10
Here is an example of attaching a text file to your email, assuming that text file `my_file.txt` is located in the same directory.
11
 
12
```php
13
<?php
14
// using SendGrid's PHP Library
15
// https://github.com/sendgrid/sendgrid-php
16
 
17
// If you are using Composer (recommended)
18
require 'vendor/autoload.php';
19
 
20
// If you are not using Composer
21
// require("path/to/sendgrid-php/sendgrid-php.php");
22
 
23
$from = new SendGrid\Email("Example User", "test@example.com");
24
$subject = "Sending with SendGrid is Fun";
25
$to = new SendGrid\Email("Example User", "test@example.com");
26
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
27
$file = 'my_file.txt';
28
$file_encoded = base64_encode(file_get_contents($file));
29
$attachment = new SendGrid\Attachment();
30
$attachment->setContent($file_encoded);
31
$attachment->setType("application/text");
32
$attachment->setDisposition("attachment");
33
$attachment->setFilename("my_file.txt");
34
 
35
$mail = new SendGrid\Mail($from, $subject, $to, $content);
36
$mail->addAttachment($attachment);
37
 
38
$apiKey = getenv('SENDGRID_API_KEY');
39
$sg = new \SendGrid($apiKey);
40
 
41
$response = $sg->client->mail()->send()->post($mail);
42
echo $response->statusCode();
43
echo $response->headers();
44
echo $response->body();
45
 
46
?>
47
```
48
 
49
<a name="transactional_templates"></a>
50
# Transactional Templates
51
 
52
For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
53
 
54
Template ID (replace with your own):
55
 
56
```text
57
13b8f94f-bcae-4ec6-b752-70d6cb59f932
58
```
59
 
60
Email Subject:
61
 
62
```text
63
<%subject%>
64
```
65
 
66
Template Body:
67
 
68
```html
69
<html>
70
<head>
71
	<title></title>
72
</head>
73
<body>
74
Hello -name-,
75
<br /><br/>
76
I'm glad you are trying out the template feature!
77
<br /><br/>
78
<%body%>
79
<br /><br/>
80
I hope you are having a great day in -city- :)
81
<br /><br/>
82
</body>
83
</html>
84
```
85
 
86
## With Mail Helper Class
87
 
88
```php
89
<?php
90
// If you are using Composer
91
require 'vendor/autoload.php';
92
 
93
// If you are not using Composer (recommended)
94
// require("path/to/sendgrid-php/sendgrid-php.php");
95
 
96
$from = new SendGrid\Email(null, "test@example.com");
97
$subject = "I'm replacing the subject tag";
98
$to = new SendGrid\Email(null, "test@example.com");
99
$content = new SendGrid\Content("text/html", "I'm replacing the <strong>body tag</strong>");
100
$mail = new SendGrid\Mail($from, $subject, $to, $content);
101
$mail->personalization[0]->addSubstitution("-name-", "Example User");
102
$mail->personalization[0]->addSubstitution("-city-", "Denver");
103
$mail->setTemplateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932");
104
 
105
$apiKey = getenv('SENDGRID_API_KEY');
106
$sg = new \SendGrid($apiKey);
107
 
108
try {
109
    $response = $sg->client->mail()->send()->post($mail);
110
} catch (Exception $e) {
111
    echo 'Caught exception: ',  $e->getMessage(), "\n";
112
}
113
 
114
echo $response->statusCode();
115
echo $response->headers();
116
echo $response->body();
117
```
118
 
119
## Without Mail Helper Class
120
 
121
```php
122
<?php
123
// If you are using Composer
124
require 'vendor/autoload.php';
125
 
126
// If you are not using Composer (recommended)
127
// require("path/to/sendgrid-php/sendgrid-php.php");
128
 
129
$request_body = json_decode('{
130
  "personalizations": [
131
    {
132
      "to": [
133
        {
134
          "email": "dx@sendgrid.com"
135
        }
136
      ],
137
      "substitutions": {
138
        "-name-": "Example User",
139
        "-city-": "Denver"
140
      },
141
      "subject": "I\'m replacing the subject tag"
142
    }
143
  ],
144
  "from": {
145
    "email": "elmer@sendgrid.com"
146
  },
147
  "content": [
148
    {
149
      "type": "text/html",
150
      "value": "I\'m replacing the <strong>body tag</strong>"
151
    }
152
  ],
153
  "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
154
}');
155
 
156
$apiKey = getenv('SENDGRID_API_KEY');
157
$sg = new \SendGrid($apiKey);
158
 
159
try {
160
    $response = $sg->client->mail()->send()->post($request_body);
161
} catch (Exception $e) {
162
    echo 'Caught exception: ',  $e->getMessage(), "\n";
163
}
164
 
165
echo $response->statusCode();
166
echo $response->body();
167
echo $response->headers();
168
```