ryanC Posted November 10, 2017 Share Posted November 10, 2017 (edited) Hi, I am getting close to finishing the form I've been working on. My goal is to have users upload an image in the form, and have that image be sent along with the rest of the email content. Taking code from a suggested thread, I am able to get the user uploaded image into my assets folder (which is in the code below). So I can at least receive the image... but I would like for that image to be sent in the content of the email instead. Would I be able to modify my code to do that? The section of the script that sends the image to the assets folder starts with //image upload begin: <?php $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); $email .= $_REQUEST['email']; if (isset($_POST['submit']) and ($_POST['email']) and ($_POST['comments']) ) { //image upload begin $upload_path = $config->paths->assets . "files/useruploads/"; $user_image = new WireUpload('user_image'); // References the name of the field in the HTML form that uploads the photo $user_image->setMaxFiles(5); $user_image->setOverwrite(false); $user_image->setDestinationPath($upload_path); $user_image->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); // execute upload and check for errors $files = $user_image->execute(); //image upload end $to = 'me@email.com'; $subject = 'Feedback from my site'; $message = 'Name: ' . $sanitizer->text($input->post->name) . "\r\n\r\n"; $message .='Email: ' . $sanitizer->email($input->post->email) . "\r\n\r\n"; $comments = $sanitizer->entities($sanitizer->textarea($input->post->comments)); $message .= 'Comments: ' . $comments; $success = $email; $success .=$message; $mail = wireMail(); $mail->to($to)->from('me@email.com'); $mail->subject($subject); $mail->body($message); $mail->send(); } else { header("location: error-page"); exit; } ?> <form id="option3form" action="./" method="post" enctype="multipart/form-data" target="_blank"> <label for="name">Name: </label> <input type="text" name="name" id="name"> <label for="email">Email: </label> <input type="email" name="email" id="email"><br /><br /> <label for="comments">Comments: </label> <textarea name="comments" id="comments"></textarea><br /><br /> <p>Click the "Choose File" button below to upload your image.</p> <input type="file" name="user_image" /> <input type="submit" name="submit" value="Submit"> </form> Thanks! Edited November 14, 2017 by ryanC solved my issue Link to comment Share on other sites More sharing options...
flydev Posted November 12, 2017 Share Posted November 12, 2017 Hi, sorry for the fast answer here, take a look at the WireMail's doc : https://processwire.com/api/ref/wire-mail/attachment/ 1 Link to comment Share on other sites More sharing options...
ryanC Posted November 13, 2017 Author Share Posted November 13, 2017 Hi flydev, I tried that a little last week but I'm missing something. I'm still pretty new to php so I know there is a step I'm leaving out. I had already tried these: $mail->attachment($config->paths->assets . "files/useruploads/"); $mail->attachment($user_image->$config->paths->assets . "files/useruploads/"); $mail->attachment($files); $mail->attachment($user_image); then... $mail->send(); But the image isn't included with the form content. Is it being canceled out by the wireUpload code that precedes it? I would actually much prefer the $mail->attachment method, but since that wasn't doing anything I settled for the wireUpload putting the image on the backend. Link to comment Share on other sites More sharing options...
ryanC Posted November 13, 2017 Author Share Posted November 13, 2017 documentation for wireMail attachment says: $m->attachment(string $value); string = Full path and filename of file attachment. But I get lost here. In my form, $value is $user_image. But how do I find out the path when it's just someone's computer desktop? Am I anywhere near coding this correctly? I am trying a hybrid of the attachment documentation and posts from this thread:https://processwire.com/talk/topic/5704-wiremailsmtp/?page=7&tab=comments#comment-142333 my form image field is called user_image. This is my script: $user_image = 'user_image'; $tempname = $user_image->$filename; $mail = wireMail(); $mail->attachment($tempname); $mail->send(); Link to comment Share on other sites More sharing options...
ryanC Posted November 13, 2017 Author Share Posted November 13, 2017 So, taking my code from earlier that makes a temporary upload path, I have: $upload_path = $config->paths->assets . "files/useruploads/"; then $userimage = 'user_image'; $mail->attachment($upload_path->$userimage); $mail->send(); what am I leaving out? Link to comment Share on other sites More sharing options...
ryanC Posted November 14, 2017 Author Share Posted November 14, 2017 Does anyone have ideas on this? I guess there is way more to it than just the wireMail attachment code above. I've been looking at swiftmailer, php mailer...of those two, which would be easiest to integrate into ProcessWire for attachments? Link to comment Share on other sites More sharing options...
adrian Posted November 14, 2017 Share Posted November 14, 2017 Hi @ryanC - sorry for the silence on this one. I don't have much time to help with this today, but I wanted to get the ball rolling for you. I think you are misunderstanding PHP objects/variables/strings etc a bit here. You define $upload_path as a string and they try to use it like an object with a $userimage property, which just doesn't work. I would suggest stepping back and using: $mail->attachment('known/path/to/image.jpg'); That will help confirm that the attachment process is working. Then make sure that the path you are linking to the user uploaded image is the correct full path to that image. 2 Link to comment Share on other sites More sharing options...
bernhard Posted November 14, 2017 Share Posted November 14, 2017 ...and please don't double post in different topics 1 Link to comment Share on other sites More sharing options...
ryanC Posted November 14, 2017 Author Share Posted November 14, 2017 Thanks adrian, yeah I know I'm way out of my element here. I appreciate the help given with my site by you guys. As of now, I have form that works and sends emails. If I have to leave it at that, I can live with it for now until I learn more about PHP. Regarding the "known path to image", that's what I can't really get my head around... I don't know what that path is. All I know is the "path" is the actual field itself is "user-image". I'm trying to speak in full sentences with this stuff and I'm still learning basic vocabulary. As of now, I can LITERALLY do anything in processwire I would want for a website EXCEPT put an image in an email. So that is something! I'm re-reading posts in these forums over and over again, trying to get it... I will figure it out eventually. Link to comment Share on other sites More sharing options...
ryanC Posted November 14, 2017 Author Share Posted November 14, 2017 Bernhard, I had this post up for a few days, and didn't get a response so I figured that was because it was in the wrong area. Link to comment Share on other sites More sharing options...
bernhard Posted November 14, 2017 Share Posted November 14, 2017 49 minutes ago, ryanC said: Bernhard, I had this post up for a few days, and didn't get a response so I figured that was because it was in the wrong area. i just mentioned that because the quality of help also depends on how one posts his questions. in that case it would be nice to reference one topic to the other so that everybody is following in the right context. lecture off and good luck with your attachments 1 Link to comment Share on other sites More sharing options...
ryanC Posted November 14, 2017 Author Share Posted November 14, 2017 Ok guys, this is going to be my last question for awhile. I am really trying here, and I'm getting really frustrated. I just went to stack overflow and asked a question hoping someone would helpfully explain to me how image attachment works, and I instantly got a bunch of down-votes. If at some point, someone could just in simple english give me a paragraph or two of the theory behind image attachment... not even the code to make it happen, just an overview of how the process works for someone with limited PHP knowledge. From that, I can probably do some forum searches on updating my script. In the mean time I believe I will see what PHPMailer is all about, I can do a simple contact form now through processwire so I am happy about that. Link to comment Share on other sites More sharing options...
adrian Posted November 14, 2017 Share Posted November 14, 2017 I don't think you are doing anything wrong other than having the path to the attachment incorrect. This is the code I use successfully for sending an attachment. I am using horst's WireSMTP module. $mailer = $mail->new(); $mailer->sendSingle(true)->to($to_address); $mailer->from($from_address); $mailer->attachment($page->files->last()->filename); foreach($page->files as $file) { $mailer->attachment($file->filename); } $mailer->subject("Email Subject")->body(strip_tags($message))->bodyHTML($message); $mailer->send(); This attaches all the files in the "files" field on the current page. You can remove that foreach loop and just attach the user's uploaded file. You just need to get the full path to the file on the server disk. Based on your code above, it should be at: $config->paths->assets . "files/useruploads/" So you just need to add the filename to the end of that. I think you should specify the full path to an image that is already on your server so you can first see for yourself that attachments are correctly sending. Then you can make it grab the user's uploaded file. 2 Link to comment Share on other sites More sharing options...
ryanC Posted November 14, 2017 Author Share Posted November 14, 2017 Thank you Adrian! I will get started with this. Link to comment Share on other sites More sharing options...
flydev Posted November 14, 2017 Share Posted November 14, 2017 If I am not dumb, WireUpload() doesn't create the destination dir in the given path if it doesn't exist and here is the first problem, you should create the dir before uploading the file (are you sure your files is uploaded in $config->paths->assets . "files/useruploads/" ?) , second - as adrian pointed out - is the attachment path. Check below your corrected code, it should work: <?php namespace ProcessWire; $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); $email .= $_REQUEST['email']; if (isset($_POST['submit']) and isset($_POST['email']) and isset($_POST['comments']) and isset($_FILES) ) { //image upload begin $upload_path = $config->paths->assets . "files/useruploads/"; // create the dir if it doesn't exist if(!is_dir($upload_path)) wireMkdir($upload_path); $user_image = new WireUpload('user_image'); // References the name of the field in the HTML form that uploads the photo $user_image->setMaxFiles(5); $user_image->setOverwrite(false); $user_image->setDestinationPath($upload_path); $user_image->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); // execute upload and check for errors $files = $user_image->execute(); //image upload end $to = 'me@email.com'; $subject = 'Feedback from my site'; $message = 'Name: ' . $sanitizer->text($input->post->name) . "\r\n\r\n"; $message .='Email: ' . $sanitizer->email($input->post->email) . "\r\n\r\n"; $comments = $sanitizer->entities($sanitizer->textarea($input->post->comments)); $message .= 'Comments: ' . $comments; $success = $email; $success .= $message; $mail = wireMail(); $mail->to($to)->from('me@email.com'); $mail->subject($subject); $mail->body($message); $mail->attachment($upload_path . $files[0]); $mail->send(); } ?> <html> <head> <meta charset="utf-8" /> </head> <body> <form id="option3form" action="./email-with-attachment" method="post" enctype="multipart/form-data"> <label for="name">Name: </label> <input type="text" name="name" id="name"> <label for="email">Email: </label> <input type="email" name="email" id="email"><br /><br /> <label for="comments">Comments: </label> <textarea name="comments" id="comments"></textarea><br /><br /> <p>Click the "Choose File" button below to upload your image.</p> <input type="file" name="user_image" /> <input type="submit" name="submit" value="Submit"> </form> </body> </html> 3 Link to comment Share on other sites More sharing options...
ryanC Posted November 14, 2017 Author Share Posted November 14, 2017 Thanks flydev! I pasted your code into my template, and I got the attachment in the email! I owe you a beer. I've been slowly reading the php documentation on file uploading, to try to understand the theory behind all this stuff, so I can better understand the tips you guys are giving me. Thanks again! 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now