Wednesday, March 20, 2013

Re: Retrieve attachment from a Post - mailgun

I just responded, but I  should have reiterated the flow the the application.   It's basically as follows:

1.  I send an email to mailgun
2.  Mailgun parses the message and POSTS it to my web page/controller over HTTP
3.  My application has a single controller (no models or views) with one method that currently only does this:
$this->log($this->request->data);
4.  I found a Ruby sample that seems to deal with this, but Cakephp is not.  I am wondering if the cake request object is not handling the FILES correctly. Here is that sample:

    # enumerate through all attachments in the message and save      # them to disk with their original filenames:      for attachment in request.files.values():          attachment.filename          data = attachment.stream.read()          with open(attachment.filename, "w") as f:              f.write(data)
5.  Here is some additional information on the attachment fields from mailgun.

attachment-xstringattached file ('x' stands for number of the attachment). Attachments are handled as file uploads, encoded as multipart/form-data.
content-id-mapstringJSON-encoded dictionary which maps Content-ID (CID) of each attachment to the corresponding attachment-x parameter. This allows you to map posted attachments to tags like <img src='cid'> in the message body.

6.   Here is a dump of the request object received when a new message is received.  Note that the [content-id-map] and [attachment-count] are both there.   The [attachment-1] is also referenced in the [content-id-map]

*******
    [recipient] => testreceiver@ducksoup.mailgun.org

    [sender] => mdaytrades@gmail.com

    [subject] => SUBJECT OF MESSAGE

    [from] => mdaytrades <mdaytrades@gmail.com>

    [Received] => from [192.168.0.12] (CPE0026f336fc30-CM0026f336fc2d.cpe.net.cable.rogers.com. [99.233.238.184]) by mx.google.com with ESMTPS id wn10sm4453782igb.2.2013.03.20.03.01.59 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 20 Mar 2013 03:02:00 -0700 (PDT)

    [X-Envelope-From] => <mdaytrades@gmail.com>

    [Dkim-Signature] => v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:content-type:subject:date:references:to:message-id :mime-version:x-mailer; bh=z2IoCinGNl4lOcyJgDRZOTqqTbEmOXG+ftlhXIfSivI=; b=G2IaZ6gPn9GtugTTrIErSWuIyo4Ad9uiUcsp/XpoxXQFBJ5T0CEwNznldIXI15vQ4Q ND8G6wf1PGRWKURTD8nSb0TRSFInBqImAW5t3KnhXKt/WGEVNOajwfwTR3m2OiryZkG8 C5qA8YHNWQHS8j9rzXP0RNxY3T8MN7YmtrWEdcybMpfgxFFjFdyB0IEF5+KDhaJ0Nc/u oCtdrsXU1rVZNp52uqdiCHrR/7Ca7rAwMP3rxQx6X7yXiBcSqu2Lp9pSmwkHhWnFoJcn WnLWbEVRQgsg+qKy8OJHIexEvdJa7Bwo/nSuPs+IIWyFKhQzLOqb8dXgDcDsuqVV2oPt 8BKw==

    [X-Received] => by 10.42.88.145 with SMTP id c17mr13130362icm.47.1363773721513; Wed, 20 Mar 2013 03:02:01 -0700 (PDT)

    [Return-Path] => <mdaytrades@gmail.com>

    [From] => mdaytrades <mdaytrades@gmail.com>

    [Content-Type] => multipart/alternative; boundary="Apple-Mail=_EC882749-B764-43DB-9769-3F4850B978F3"

    [Subject] => SUBJECT OF MESSAGE

    [Date] => Wed, 20 Mar 2013 06:02:00 -0400

    [References] => <8A01B27E-CD7D-4599-B1C5-F87BF874624C@gmail.com>

    [To] => "testreceiver@ducksoup.mailgun.org" <testreceiver@ducksoup.mailgun.org>

    [Message-Id] => <9385EC89-5462-4BA1-93B9-9E4E8BEC1BDA@gmail.com>

    [Mime-Version] => 1.0 (Mac OS X Mail 6.3 \(1503\))

    [X-Mailer] => Apple Mail (2.1503)

    [X-Mailgun-Incoming] => Yes

    [message-headers] => [["Received", "by luna.mailgun.net with SMTP mgrt 8784990917749; Wed, 20 Mar 2013 10:02:02 +0000"], ["X-Envelope-From", "<mdaytrades@gmail.com>"], ["Received", "from mail-ie0-f180.google.com (mail-ie0-f180.google.com [209.85.223.180]) by mxa.mailgun.org with ESMTP id 51498919.7f7908037770-in1; Wed, 20 Mar 2013 10:02:01 -0000 (UTC)"], ["Received", "by mail-ie0-f180.google.com with SMTP id a11so1597996iee.39 for <testreceiver@ducksoup.mailgun.org>; Wed, 20 Mar 2013 03:02:01 -0700 (PDT)"], ["Dkim-Signature", "v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:content-type:subject:date:references:to:message-id :mime-version:x-mailer; bh=z2IoCinGNl4lOcyJgDRZOTqqTbEmOXG+ftlhXIfSivI=; b=G2IaZ6gPn9GtugTTrIErSWuIyo4Ad9uiUcsp/XpoxXQFBJ5T0CEwNznldIXI15vQ4Q ND8G6wf1PGRWKURTD8nSb0TRSFInBqImAW5t3KnhXKt/WGEVNOajwfwTR3m2OiryZkG8 C5qA8YHNWQHS8j9rzXP0RNxY3T8MN7YmtrWEdcybMpfgxFFjFdyB0IEF5+KDhaJ0Nc/u oCtdrsXU1rVZNp52uqdiCHrR/7Ca7rAwMP3rxQx6X7yXiBcSqu2Lp9pSmwkHhWnFoJcn WnLWbEVRQgsg+qKy8OJHIexEvdJa7Bwo/nSuPs+IIWyFKhQzLOqb8dXgDcDsuqVV2oPt 8BKw=="], ["X-Received", "by 10.42.88.145 with SMTP id c17mr13130362icm.47.1363773721513; Wed, 20 Mar 2013 03:02:01 -0700 (PDT)"], ["Return-Path", "<mdaytrades@gmail.com>"], ["Received", "from [192.168.0.12] (CPE0026f336fc30-CM0026f336fc2d.cpe.net.cable.rogers.com. [99.233.238.184]) by mx.google.com with ESMTPS id wn10sm4453782igb.2.2013.03.20.03.01.59 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 20 Mar 2013 03:02:00 -0700 (PDT)"], ["From", "mdaytrades <mdaytrades@gmail.com>"], ["Content-Type", "multipart/alternative; boundary=\"Apple-Mail=_EC882749-B764-43DB-9769-3F4850B978F3\""], ["Subject", "SUBJECT OF MESSAGE"], ["Date", "Wed, 20 Mar 2013 06:02:00 -0400"], ["References", "<8A01B27E-CD7D-4599-B1C5-F87BF874624C@gmail.com>"], ["To", "\"testreceiver@ducksoup.mailgun.org\" <testreceiver@ducksoup.mailgun.org>"], ["Message-Id", "<9385EC89-5462-4BA1-93B9-9E4E8BEC1BDA@gmail.com>"], ["Mime-Version", "1.0 (Mac OS X Mail 6.3 \\(1503\\))"], ["X-Mailer", "Apple Mail (2.1503)"], ["X-Mailgun-Incoming", "Yes"]]

    [timestamp] => 1363773724

    [token] => 48km59sauktayd45fziefvfs6ndfalfh5kr0emkroa0g78irt8

    [signature] => d525e5b382f4182d03266b80995019c1e300d2f643d1d242a6552c04650d84f0

    [attachment-count] => 1

    [body-plain] => > BODY OF MESSAGE.    MESSAGE SHOULD HAVE 1 ATTACHMENT


    [body-html] => <html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>BODY OF MESSAGE. &nbsp; &nbsp;MESSAGE SHOULD HAVE 1 ATTACHMENT</div></div></blockquote><img height="241" width="240" apple-width="yes" apple-height="yes" id="821a76ff-2351-4833-a22c-c3a2121bbbf4" src="cid:99098D0F-79DD-44A9-AC75-B65899C122BA@vlan1.phub.net.cable.rogers.com"></div></body></html>

    [stripped-html] => <html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><img height="241" width="240" apple-width="yes" apple-height="yes" id="821a76ff-2351-4833-a22c-c3a2121bbbf4" src="cid:99098D0F-79DD-44A9-AC75-B65899C122BA@vlan1.phub.net.cable.rogers.com"></div></body></html>

    [stripped-text] => > BODY OF MESSAGE.    MESSAGE SHOULD HAVE 1 ATTACHMENT

    [content-id-map] => {"<99098D0F-79DD-44A9-AC75-B65899C122BA@vlan1.phub.net.cable.rogers.com>": "attachment-1"}

)



Here is a snip from the mailgun docs



On Tuesday, March 19, 2013 1:26:15 PM UTC-4, MDay wrote:
hi there - hoping someone can help me with a simple issue I have having with retrieving data from an HTTP Post.   I am using mailgun to post an email message to a simple method in my application...  Everything is working correctly except I can't get attachments out of the POST.  I noticed that the encoding is set as multipart/form-data and the support guys at mailgun indicated I had to use $FILES to retrieve the data from the post.   I am a bit of a newbie, so now I am lost..

Here is what my current code looks like:

$subject = $this->request->data('subject');   //THIS WORKS

$attachement_test1 = $this->request->data($_FILES['attachment-1']);  //THIS DOESN'T WORK

$attachement_test2 = $this->request->data('attachment-1');//THIS DOESN'T WORK EITHER

$this->log("New Message");

$this->log($subject);

$this->log($attachement_test1);

$this->log($attachement_test2);


Snip from the mailgun docs:


attachment-xstringattached file ('x' stands for number of the attachment). Attachments are handled as file uploads, encoded as multipart/form-data.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: