Skip to content

Instantly share code, notes, and snippets.

@sebmih
Created July 17, 2013 10:25
Show Gist options
  • Select an option

  • Save sebmih/6019419 to your computer and use it in GitHub Desktop.

Select an option

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
<?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