Created
July 17, 2013 10:25
-
-
Save sebmih/6019419 to your computer and use it in GitHub Desktop.
A complete script that sends emails with attachments (built within the script itself) using the Web API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // this is where file.txt is created and content is added. | |
| $my_file = 'file.txt'; | |
| $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); | |
| $data = 'This is the actual content of the text file.'; | |
| fwrite($handle, $data); | |
| $url = 'http://sendgrid.com/'; | |
| $user = '******'; | |
| $pass = '******'; | |
| $to_addr = '[email protected]'; | |
| $message = 'This is a message with an attachment.'; | |
| $subject = 'Testare 4'; | |
| $from = '[email protected]'; | |
| $html = '<p>his is a message with an attachment.</p>'; | |
| $params = array( | |
| 'api_user' => $user, | |
| 'api_key' => $pass, | |
| 'to' => $to_addr, | |
| 'subject' => $subject, | |
| 'text' => $message, | |
| 'html' => $html, | |
| 'from' => $from, | |
| //The @ sign tells cURL that the file needs to be uploaded in the 'files' parameter | |
| 'files[file.txt]' => '@file.txt' | |
| ); | |
| $request = $url . 'api/mail.send.json'; | |
| // Generate curl request | |
| $session = curl_init($request); | |
| // Tell curl to use HTTP POST | |
| curl_setopt ($session, CURLOPT_POST, true); | |
| // Tell curl that this is the body of the POST | |
| curl_setopt ($session, CURLOPT_POSTFIELDS, $params); | |
| // Tell curl not to return headers, but do return the response | |
| curl_setopt($session, CURLOPT_HEADER, 0); | |
| curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
| // obtain response | |
| $response = curl_exec($session); | |
| curl_close($session); | |
| print_r($response); | |
| exit(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment