Send image attachment WITHOUT local save

Hey everyone. I’m creating a script which generates a QR code and sends the customer an image of the Qr code as an email attachment. The script works fine when I test it on my own. However, the problem is that the image is saved locally and then sent as an attachment. I obviously can’t do this when the script is ran on a web server.

How can I send an image without saving it locally? Can I save it on the database as a blob, send it, and remove it from the database?

Try using a Google API, here is a function I am currently working on… This should get you started

[php]function qr_code($data, $type = “TXT”, $size =‘150’, $ec=‘L’, $margin=‘0’) {
$types = array(‘URL’, ‘EMAIL’, ‘TEL’, ‘MECARD’, ‘VCARD’, ‘SMS’, ‘MMS’, ‘GEO’, ‘PLAY’, ‘WIFI’, ‘YOUTUBE’, ‘FACEBOOK’, ‘TWITTER’, ‘INSTAGRAM’, ‘TXT’);
$errorimg = ‘’;

if (in_array(strtoupper($type), $types)){
	switch(strtoupper($type)){
		case 'URL':{
			$data = (preg_match('/^http:\/\//', $data) || preg_match('/^https:\/\//', $data) ? $data : 'http://'.$data);
			break;
		}
		case 'EMAIL':{
			$data = (preg_match('/^mailto:/', $data) ? $data : 'mailto:'.$data);
			break;
		}
		case 'TEL':{
			$data = (preg_match('/^tel:/', $data) ? $data : 'tel:'.$data);
			break;
		}
		case 'MECARD':{
			if (is_array($data)){

			} else {
				return $errorimg;
			}
			break;
		}
		case 'VCARD':{
			if (is_array($data)){
				$data = 'BEGIN:VCARD'."\n".'VERSION:3.0'."\n".'N:'.$data[0]." ".$data[1]."\n".'ORG:'.$data[2]."\n".'TITLE:'.$data[3]."\n".'TEL:'.$data[4]."\n".'URL:'.$data[5]."\n".'EMAIL:'.$data[6]."\n".'ADR:'.$data[7]."\n".'NOTE:'.$data[8]."\n"."END:VCARD";
			} else {
				return $errorimg;
			}
			break;
		}
		case 'CAL':{
			if (is_array($data)){
				$data = 'BEGIN:VEVENT'."\n".'VERSION:3.0'."\n".'SUMMARY:'.$data[0]."\n".'DTSTART:'.$data[2]."\n".'DTEND:'.$data[4]."\n".'LOCATION:'.$data[3]."\n".'DESCRIPTION:'.$data[5]."\n"."END:VEVENT";
				/*
					BEGIN:VEVENT
					SUMMARY:Test
					DTSTART:20170219T230000Z
					DTEND:20170221T030000Z
					LOCATION:Visalia, California
					DESCRIPTION:Meeting with someone
					END:VEVENT
				*/
			} else {
				return $errorimg;
			}
			break;
		}
		case 'SMS':{
			if (is_array($data)){
				$data = 'smsto:'.$data[0].':'.$data[1];
			} else {
				return $errorimg;
			}
			break;
		}
		case 'MMS':{
			if (is_array($data)){
				$data = 'mmsto:'.$data[0].':'.$data[1];
			} else {
				return $errorimg;
			}
			break;
		}
		case 'GEO':{
			if (is_array($data)){

			} else {
				return $errorimg;
			}
			break;
		}
		case 'WIFI':{
			if (is_array($data)){

			} else {
				return $errorimg;
			}
			break;
		}
		case 'PLAY':{
			if (is_array($data)){

			} else {
				return $errorimg;
			}
			break;
		}
		case 'YOUTUBE':{
			$data = (preg_match('/^youtube:/', $data) ? 'https://www.youtube.com/watch?v='.preg_replace("/^youtube:/i", '', $data) : 'https://www.youtube.com/watch?v='.$data);
			break;
		}
		case 'FACEBOOK':{
			$data = (preg_match('/^facebook:/', $data) ? 'https://www.facebook.com/'.preg_replace("/^facebook:/i", '', $data) : 'https://www.facebook.com/'.$data);
			break;
		}
		case 'TWITTER':{
			$data = (preg_match('/^twitter:/', $data) ? 'https://www.twitter.com/'.preg_replace("/^twitter:/i", '', $data) : 'https://www.twitter.com/'.$data);
			break;
		}
		case 'INSTAGRAM':{
			$data = (preg_match('/^instagram:/', $data) ? 'https://www.instagram.com/watch?v='.preg_replace("/^instagram:/i", '', $data) : 'https://www.instagram.com/'.$data);
			break;
		}
		case 'TXT':{
			if (is_array($data)){

			} else {
				return $errorimg;
			}
			break;
		}
	}
}
#$types = array("URL" => "http://", "TEL" => "TEL:", "TXT"=>"", "EMAIL" => "MAILTO:");
#if(!in_array($type, array("URL", "TEL", "TXT", "EMAIL"))) {
#    $type = "TXT";
#}
#if (!preg_match('/^'.$types[$type].'/', $data)) {
#    $data = str_replace("\\", "", $types[$type]).$data;
#}

$data = urlencode($data);
$response = 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$ec.'|'.$margin.'&chl='.$data;
return $response;

}[/php]

Why can’t you save it as a temp file, send the mail and delete the file?

Anyway PHPMailer (and probably most other) mailers support adding attachments from strings. If you decide to use PHPMailer then look up AddStringAttachment

Sponsor our Newsletter | Privacy Policy | Terms of Service