Jump to content

Send image attachment WITHOUT local save


arsenalftw067

Recommended Posts

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?

Link to comment
Share on other sites

Why can't you generate the file on the webserver? You'll want it in a folder outside of the webroot or at least protected from browsing, probably.

 

Kind of hard to attach a file if you're not going to, uh, create the file, though. :)

Link to comment
Share on other sites

Yes, you can "attach" a photo without actually having a physical file. But, from my experience, that can cause other problems - primarily due to email clients that do not handle those types of messages well.

 

You could always opt to not "attach" the photo and instead display the image in the email using an image tag. E.g. the image src could look something like this:

http://mydomain.com/createqr.php?id=JI7DB7SWJA89DW43

 

The ID on the url references the unique code for that user. The src page will then use the code on the query string to dynamically generate the image and return to the user (in this case the email client).

W

 

hy can't you generate the file on the webserver? You'll want it in a folder outside of the webroot or at least protected from browsing, probably.

Kind of hard to attach a file if you're not going to, uh, create the file, though.  :)

 
Link to comment
Share on other sites

Yes, you can "attach" a photo without actually having a physical file. But, from my experience, that can cause other problems - primarily due to email clients that do not handle those types of messages well.

 

???

 

All attachments must be embedded as strings into a multipart message. The concept of "attaching files" only exists on the server, and it just means that the server has to do the extra work of reading the file content into a string.

 

Once the mail has been sent out, it's the same mail. It's not like the mail contains "real" file references, so that the mail client could browse around in the server's filesystem -- that would be disturbing.

Link to comment
Share on other sites

???

 

All attachments must be embedded as strings into a multipart message. The concept of "attaching files" only exists on the server, and it just means that the server has to do the extra work of reading the file content into a string.

 

Once the mail has been sent out, it's the same mail. It's not like the mail contains "real" file references, so that the mail client could browse around in the server's filesystem -- that would be disturbing.

 

The problem I was thinking of is where the email displays an image in the body of the message by including the image content as part of the message - instead of using an <image> tag with a reference to the image on a remote server.

 

Most email clients will not display images that have to be downloaded without the user explicitly accepting them - for security purposes. However, our product management decided that the header images in our application emails (which had no functional value) were too important and decided we had to ensure the user would see them w/o having to accept them. So, messages are much bigger than they need to be and we have had some problems in the past of the emails getting 'caught' in spam filters.

Link to comment
Share on other sites

Wow thanks for everyones response! I was also thinking about just showing the image on the body instead of an attachment. Okay just to clarify, here is exactly what I'm doing:

My script generates a QR code using the Endroid library. My script displays the QR code & emails the code as an image attachment to the customer, using phpmailer and gmail SMTP.

Here is my code:

My code currently saves the image on the current directory and unlinks it after it sends. However, if this script was ran live and put on a web server, would this script still work? Would the image still be saved on the web server or on the clients directory?

$qrcode = randNumb();
$qr = new Endroid\QrCode\QrCode(); 
$qr->setText($qrcode);
$qr->setSize(200);
$qr->setPadding(10);
$qr->setImageType('png');

$firstName = $_POST['First'];
$lastName = $_POST['Last'];
$showName = $_POST['Show'];
$userEmail = $_POST['Email'];
$saveNamePic = $firstName ." ". $lastName .".png"; 
$saveName = $firstName ." ". $lastName;
$qr->save($saveNamePic);
$conn = openDB();

$insert = mysqli_query($conn, "INSERT INTO presale(SID,PID,CustName) VALUES ('1','".$qrcode."','".$saveName."')");

if ($insert)
{
		//$qr->render();
	//send email attachment of qr code
	$mail = new PHPMailer; 
	$mail->isSMTP(); //set mailer to SMTP 
	$mail->Host ='smtp.gmail.com';
	$mail->SMTPAuth = true;
	$mail->Username = 'foolish067@gmail.com';
	$mail->Password = 'papers067';
	$mail->SMTPSecure = 'tls';
	$mail->Port = 587;

	$mail->setFrom('noreply@admin.com', 'Focus OC');
	$mail->addAddress($userEmail, $saveName);
	$mail->addAttachment($saveNamePic);
	$mail->Subject ='Here is your presale ticket';
	$mail->Body = 'test test';
	$mail->isHTML(true);
Link to comment
Share on other sites

Would the image still be saved on the web server or on the clients directory?

 

Think about this: How is a script running on a webserver supposed to write files to your PC? If that were possible, we would be screwed, don't you think?

 

But there's absolutely no reason for saving the file anywhere. If you simply read the QrCode example, you'll see a writeString() method. And as I said above, PHPMailer has an addStringAttachment() method. Surely you can figure out how to combine the two.

Link to comment
Share on other sites

You can attach the QR code as a string with PHPMailer::addStringAttachment() and QrCode::get().

$mail->addStringAttachment(base64_encode($qr->get()), $saveNamePic);
And your QrCode library is old - they're on 2.2 now, you're on 1.x.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.