Skip to content

Instantly share code, notes, and snippets.

@pdfcrowd
Last active February 15, 2020 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdfcrowd/1649109 to your computer and use it in GitHub Desktop.
Save pdfcrowd/1649109 to your computer and use it in GitHub Desktop.
Generate PDF using the Pdfcrowd API v1 and send it as an email attachment
// uses the PHP PEAR modules for sending MIME email
// http://pear.php.net/package/Mail
// http://pear.php.net/package/Mail_Mime
<?php
require 'pdfcrowd.php';
require_once('Mail.php');
require_once('Mail/mime.php');
$client = new Pdfcrowd($username, $apikey);
$pdf = $client->convertHtml('Hello World');
$to = "recipient@example.com";
$from = "sender@example.com";
$subject = "A test email with a PDF attachment";
$txtbody = "Please find the pdf attached.";
// create a new instance of the Mail_Mime class
$mime = new Mail_Mime();
$mime->setTXTBody($txtbody);
$mime->addAttachment($pdf, 'application/pdf', 'helloworld.pdf', false, 'base64');
$smtpinfo["host"] = "your smtp server";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtp-username";
$smtpinfo["password"] = "smtp-password";
$smtpinfo["debug"] = false;
// build email message and save it in $body
$headers = array(
'From' => $from,
'Subject' => $subject);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail = &Mail::factory("smtp", $smtpinfo);
$mail->send($to, $hdrs, $body);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment