Nemo Caligo Nemo Caligo -4 years ago 156
PHP Question

Attaching a base64_encoded string in emails - Codeigniter

I've spent the last day creating a script that will create a PDF-receipt when a customer buys something from our site. When the PDF is created, I save the output to a variable using ob_get_clean()

I then make this variable into a base64_encoded string. When I'm done doing that, I save the string to a database. Now, what I want to do after that is get the string and in some way save it as an attachment to an email, so that the user can download it as a file. I've tried Google, but I didn't find anything helpful really.

I found this thread, but as far as I can see in the Codeigniter email-library (I might have missed it), the requested function wasn't implemented. Here is the request though, Email class: add attachment from string

Thanks in advance,

Answer Source

You can create your own library and send the email using the php mail function and the appropriate headers.

function send_email($to, $from, $subject, $body, $attachment_string)

$filename = "receipt.pdf";
$uid = md5(uniqid(time())); 

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "From: <".$from.">\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $body."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type: application/pdf; name=\"".basename($filename)."\"\r\n"; // use different content types here
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".basename($filename)."\"\r\n\r\n";
$headers .= $attachment."\r\n\r\n";
$headers .= "--".$uid."--";

if(mail($to, $subject, $body, $headers))
    echo "success";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download