Search code examples
phpimagesymfonyamazon-s3gmail

Google adds proxy to image link, image doesn't display


I am sending bulk emails from my organisation. I want to track who has opened the email. I know it's not 100% feasible but still we'll have some stats. When I send emails through Mailchimp or Postmark, I can see they correctly track the opened emails.

The image is located on Amazon S3 and the emails are sent through Amazon SES. I set the src of the image with our url which records the user and then returns the image. If I open that url in browser, it correctly logs the entry and downloads the image.

The problem is that Gmail adds a proxy in front of the image thus the image doesn't load at all. I am using Symfony2.8 and this is the response which I send.

    $response = new Response();

    $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'myimage.png');
    $response->headers->set("Content-Disposition", $disposition);
    $response->setPublic();
    $response->headers->set("Content-Transfer-Encoding", "binary");
    $response->setCache(array('max_age' => 0, 'public' => true));
    $response->mustRevalidate();
    $response->setExpires(0);
    $response->headers->set("Content-Type", "image/png");
    $response->headers->set("Content-Length", 10240);
    $response->setContent(file_get_contents("http://some-amazon-link/assets/logo/pressweb/myimage.png"));

    return $response;

P.S. I have read almost all the questions about it on Stack and other websites. There may be something wrong with my code. I dunno. Thank you.


Solution

  • This is what worked for me. Thanks to Luciano, I found the solution on his site. http://loige.co/transparent-pixel-response-with-symfony-how-to-track-email-opening/ The tracking is a personal choice, how you track your users i.e. using id etc. The whole code can be found on his site.

    class TransparentPixelResponse extends Response  
    {
    /**
     * Base 64 encoded contents for 1px transparent gif and png
     * @var string
     */
    const IMAGE_CONTENT = 
        'R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='
    ;
    
    /**
     * The response content type
     * @var string
     */
    const CONTENT_TYPE = 'image/gif';
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $content = base64_decode(self::IMAGE_CONTENT);
        parent::__construct($content);
        $this->headers->set('Content-Type', self::CONTENT_TYPE);
        $this->setPrivate();
        $this->headers->addCacheControlDirective('no-cache', true);
        $this->headers->addCacheControlDirective('must-revalidate', true);
    }
    

    }