0

I have a Swiftmailer PHP script that is running as below.

At the minute it's echoing the image path but I'd like to pull through the image within the email itself.

I have managed to get a separate version working where I include the attachment from Path of a live URL working but can't seem to work out how to get it to pull through the $media version instead.

Any help would be gratefully appreciated. Thanks.

message = \Swift_Message::newInstance();
$message->attach(\Swift_Attachment::fromPath('https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'))

        ->setSubject('Form')
        ->setFrom('test@test.com', 'Test')
        ->setTo('test@test.com', 'Test Name')
        ->setContentType("text/html")

        ->setBody(
            $this->renderView(
                'Contact:Contact:contact.html.twig',
                array(
                    'ip' => $request->getClientIp(),
                    'name' => $form->get('name')->getData(),
                    'email' => $form->get('email')->getData(),
                    'media' => $form->get('media')->getData()
                )
            )
        );

    # Send message

    $this->get('mailer')
        ->send($message);
kenz
  • 112
  • 12
Bradley
  • 79
  • 7

1 Answers1

0

That function is for a local path. So you have to save that image first on your hdd. You can use file_get_contents for example to fetch the image and save them to your temp path. Then you can use that path and send the email attachment. After sending you can remove the image with unlink.

Here is an perfect example how to set your form field directly into your attachment.

$message->attach(
    Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);

fileatt is the name of the form field. So post your form to the php page and use that attachment.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • Hi, thank you for your response. Please, do you have an example at all? I'm quite stuck! – Bradley Jun 29 '17 at 20:27
  • Hi, not sure my request way too clear. Basically I referenced the Swift_Attachment::fromPath('') as when I send the form the image is attached which is fine. But what I'm trying to achieve is include the image that I actually upload in the form through the $media input file field. At the minute all it pulls through is the image path through the server. I hope this helps? – Bradley Jun 29 '17 at 20:35
  • Then you should update your question that are different things... so with form fiel you have to move the image to a directory with `move_uploaded_file`. – René Höhle Jun 29 '17 at 20:37
  • Hmmm I'm struggling to understand your response in truth. I discovered this which I think might help but it's trying to embed this code within mine to ensure it functions is where I'm getting stuck. New to Symfony and Swiftmailer so not ideal. `code` $message->attach( Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('myfilename.jpg') ); – Bradley Jun 29 '17 at 20:45
  • I found this post also, but again it's trying to include the $media actual file to the mail rather than the path. I'd be massively grateful if you have any advise. https://stackoverflow.com/questions/11319878/swiftmailer-and-email-form-with-attachment-beginner – Bradley Jun 29 '17 at 20:49