Hello guys, I am facing this issue and I'm not sure how to tackle it.
I have a form, in this form you enter the link to an image that is somewhere on the web. On submit, the form is supposed to send that very image as an attachment in an email with phpmailer.
I think that I need to find:
a. a way to convert a link into php code
b. send that php code in the body of an email
c. change the headers of the email to trick phpmailer to think there is actually an attachment in the email, rather than just text in the body of the message
Am I on the right path?

I don't really know how to go about this so I was wondering if I am thinking the right way and if you can point me to the right PHP functions so I can follow some examples and learn about those functions to make this work for me.

Thanks a ton.

    you don't have the right to use others content in such a way.

    marcnyc;10990282 wrote:

    a. a way to convert a link into php code

    makes no sense.

    marcnyc;10990282 wrote:

    b. send that php code in the body of an email

    you can't, email is going to be text, not executable script.

    marcnyc;10990282 wrote:

    c. change the headers of the email to trick phpmailer to think there is actually an attachment in the email, rather than just text in the body of the message

    why not actully send an attachment ?

    marcnyc;10990282 wrote:

    Am I on the right path?

    based on the above observations - no.

      It sounds like a chocolate-covered banana. If this phpmailer thing doesn't do attachments, then replacing it with a mail library that does would appear to make more sense. If it does do attachments, then there wouldn't be any need to "trick" it.

        Phpmailer does do attachments but I would have to upload the image from the user's hard drive. Instead I need to be able to send as an attachment what the user inserted as a link to an image.
        I have see google image provide a php code for an image that was linked from another site so there must be a way

          marcnyc;10990286 wrote:

          Phpmailer does do attachments but I would have to upload the image from the user's hard drive.

          makes no sense you said you where scraping the image from a website.

          marcnyc;10990286 wrote:

          Instead I need to be able to send as an attachment what the user inserted as a link to an image.

          then you need to get the file from the remote site and add it as an attachment

          marcnyc;10990286 wrote:

          I have see google image provide a php code for an image that was linked from another site so there must be a way

          no idea what this means.

            dagon;10990287 wrote:

            makes no sense you said you where scraping the image from a website.

            then you need to get the file from the remote site and add it as an attachment

            no idea what this means.

            ok if this is possible can you tell me how to do it?

              You've been registered on this site for longer than I have, you have over 500 posts, and you still don't know about [man]file_get_contents[/man]?

                I had actually tried that but no luck...
                using file_get_contents just returns the words 'GIF89ax'... I've also tried readfile() but I still don't know how to turn the string into an attachment I can send via email...
                the phpmailer AddAttachment() returns an error if I give it a URL

                The last thing I tried was this:

                <?php
                
                include_once('main.inc.php');
                
                $image_url = 'http://www.dpreview.com/Learn/Articles/Glossary/Optical/images/123di_perspec_wide_far.jpg';
                
                function save_image($img,$fullpath) {
                    $ch = curl_init ($img);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
                    $rawdata=curl_exec($ch);
                    curl_close ($ch);
                    if(file_exists($fullpath)){
                        unlink($fullpath);
                    }
                    $fp = fopen($fullpath,'x');
                    fwrite($fp, $rawdata);
                    fclose($fp);
                }
                
                $image = save_image($image_url,getcwd().'/images');
                
                $body = 'body text';
                
                		$mail->Subject = 'attachment test';
                		$mail->AddReplyTo('myemail@gmail.com');
                		$mail->AddAddress('myemail@gmail.com');
                		$mail->MsgHTML($body);
                		$mail->AddAttachment('images/'.$image);
                		if(!$mail->Send()) {
                			$notification_results = 'ERROR';
                		} else {
                			$notification_results = 'EMAIL SENT';
                			$mail->ClearAddresses();
                		}
                		echo $notification_results;
                
                ?>

                however I get these errors:

                Warning: unlink(/home/chaindlk/public_html/reviews/admin/images) [function.unlink]: Is a directory in /home/chaindlk/public_html/reviews/admin/email.php on line 13

                Warning: fopen(/home/chaindlk/public_html/reviews/admin/images) [function.fopen]: failed to open stream: File exists in /home/chaindlk/public_html/reviews/admin/email.php on line 15

                Warning: fwrite(): supplied argument is not a valid stream resource in /home/chaindlk/public_html/reviews/admin/email.php on line 16

                Warning: fclose(): supplied argument is not a valid stream resource in /home/chaindlk/public_html/reviews/admin/email.php on line 17
                Could not access file: images/ EMAIL SENT

                any ideas?

                  I was just doing:

                  <?php 
                  
                  include_once('main.inc.php'); 
                  
                  $image_url = 'http://www.dpreview.com/Learn/Articles/Glossary/Optical/images/123di_perspec_wide_far.jpg'; 
                  
                  $image = file_get_contents($image_url); 
                  
                  $body = 'body text'; 
                  
                          $mail->Subject = 'attachment test'; 
                          $mail->AddReplyTo('myemail@gmail.com'); 
                          $mail->AddAddress('myemail@gmail.com'); 
                          $mail->MsgHTML($body); 
                          $mail->AddAttachment($image); 
                          if(!$mail->Send()) { 
                              $notification_results = 'ERROR'; 
                          } else { 
                              $notification_results = 'EMAIL SENT'; 
                              $mail->ClearAddresses(); 
                          } 
                          echo $notification_results; 
                  
                  ?>

                  but even if I remove AddAttachment and replace MsgHTML($body); with MsgHTML($image); I only get a few letters in the body

                    Read the docs for PHPMailer. The AddAttachment() method takes up to four arguments, and none of them is raw binary data.

                    Try saving the image data to a file first (e.g. using [man]file_put_contents/man) and then attaching that file (note you might want to [man]unlink/man the file after you're done using it, too).

                    marcnyc wrote:

                    even if I remove AddAttachment and replace MsgHTML($body); with MsgHTML($image); I only get a few letters in the body

                    Why would you try to use a method called MsgHTML and pass it something that isn't HTML? Seems obvious that that should not work.

                      thanks, that pointed me in the right direction

                        Write a Reply...