Send PHP email with chart.apis.google.com generated image as attachment

I am using PHP to automatically generate a QRCode through the chart.apis.google.com system. This works fine and I can display the image on the browser.

What would be the next step that I need to take to send this image as an attachment in a PHP generated email (and what tips do you have to avoid it from being misrepresented as spam)?

Thanks for the help. See below for sample the code I am using to generate the image and display it in the browser.


$qr = $_GET['number'];

function google_qr($qr,$size ='250',$EC_level='L',$margin='0') {


$image = '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$qr.'" alt="QR code" width="'.$size.'" height="'.$size.'"/>';

echo $image; 
}

google_qr($qr,250);

Open in new window

MC_ITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

karoldvlCommented:
You could either paste the image as an <img /> in the message body (HTML) or send it as attachment. Both ways here:

<?php

$qr = $_GET['number'];

// Check if numeric
if (!is_numeric($qr)) $qr = 0;
// or validate somehow the input

function google_qr($qr,$size ='250',$EC_level='L',$margin='0') {

$imageSrc = 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$qr;
$image = '<img src="'.$imageSrc.'" alt="QR code" width="'.$size.'" height="'.$size.'"/>';

echo $image;

// --- Send as remote image
$to       = 'user@example.com';
$subject  = 'QR mail test';
$message  = "Message header comes here...<br />$image<br />...message footer comes here";
$headers  = "From: noreply@example.com\r\n";
$headers  = "Reply-To: noreply@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers);

// --- Send as attachment
$to       = 'user@example.com';
$subject  = 'QR mail test with attachment';
$headers  = "From: noreply@example.com\r\n";
$headers  = "Reply-To: noreply@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$boundary = md5(date('r', time()));
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-$boundary\"\r\n";

$rawImage = chunk_split(base64_encode(file_get_contents($imageSrc)));

$message = "
--mixed-$boundary
Content-Type: multipart/alternative; boundary=\"alt-$boundary\"

--alt-$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

Hello!
See the attached image.

--alt-$boundary
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

<h2>Hello!</h2>
See the attached <strong>image</strong>.

--alt-$boundary--

--mixed-$boundary
Content-Type: image/png; name=\"QR.png\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$rawImage
--mixed-$boundary--";

mail($to, $subject, $message, $headers);

}

google_qr($qr,250);

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MC_ITAuthor Commented:
This works perfectly....

Thanks karoldvl
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.