arsenalftw067 Posted July 24, 2017 Share Posted July 24, 2017 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted July 24, 2017 Share Posted July 24, 2017 Depends on the code: maybe the code is only capable of attaching something if it's an actual file. What is the code? What are you using to send the email? Quote Link to comment Share on other sites More sharing options...
Sepodati Posted July 24, 2017 Share Posted July 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 24, 2017 Share Posted July 24, 2017 Kind of hard to attach a file if you're not going to, uh, create the file, though. Not at all. Quote Link to comment Share on other sites More sharing options...
Sepodati Posted July 24, 2017 Share Posted July 24, 2017 Not at all.Ok, ya got me there. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 24, 2017 Share Posted July 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 24, 2017 Share Posted July 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 24, 2017 Share Posted July 24, 2017 ??? 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. Quote Link to comment Share on other sites More sharing options...
arsenalftw067 Posted July 24, 2017 Author Share Posted July 24, 2017 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); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 24, 2017 Share Posted July 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 24, 2017 Share Posted July 24, 2017 What's more important, though: Your code is wide open to SQL injection attacks. Either learn how to use mysqli correctly (which means spending a lot of time on the PHP manual) or switch to PDO. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 25, 2017 Share Posted July 25, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 25, 2017 Share Posted July 25, 2017 You can attach the QR code as a string with PHPMailer::addStringAttachment() and QrCode::get(). We already had that. Twice. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 25, 2017 Share Posted July 25, 2017 We already had that. Twice.His QrCode doesn't have writeString(). My post had a secondary purpose too. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.