• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1153
  • Last Modified:

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

0
MC_IT
Asked:
MC_IT
1 Solution
 
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
 
MC_ITAuthor Commented:
This works perfectly....

Thanks karoldvl
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now