Rails:使用Gmail API发送电子邮件,附件仅返回编码文件

时间:2018-01-29 09:02:08

标签: ruby-on-rails google-api gmail-api google-api-ruby-client

我正在尝试使用gmail API发送电子邮件,我在ruby中的代码运行良好,没有附件如下:

    client = google_client user_id
    token = Token.find_by_user_id(user_id)
    access_token = token.access_token
    gmail = Google::Apis::GmailV1::GmailService.new
    gmail.authorization = client
    message              = Mail.new
    message.date         = Time.now
    message.subject      = 'Supertram p'
    message.body         = "<p>Hi Alex, how's life?</p>"
    message.content_type = 'text/html'
    message.from         = self.email
    message.to           = 'email_name@gmail.com'

    msg = message.encoded
    message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s)
    gmail.send_user_message('me', message_object)

成功发送html格式的电子邮件,我的问题是如何将文件附加到此邮件?有些人建议我使用add_file

client = google_client user_id
    token = Token.find_by_user_id(user_id)
    access_token = token.access_token
    gmail = Google::Apis::GmailV1::GmailService.new
    gmail.authorization = client
    message              = Mail.new
    message.date         = Time.now
    message.subject      = 'Supertramp'
    message.body         = "<p>Hi Alex, how's life?</p>"
    # message.content_type = 'text/html'
    message.from         = self.email
    message.to           = 'myemail@gmail.com'
    message.add_file("/Users/myname/Downloads/image.png")

    msg = message.encoded
    message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s)
    gmail.send_user_message('me', message_object)

但这是我收到的消息:

  

- Content-Type:text / plain; charset = UTF-8 Content-Transfer-Encoding:7bit嗨Alex,生活怎么样?

     

- 内容类型:image / png; filename = image.png Content-Transfer-Encoding:base64 Content-Disposition:attachment;   filename = image.png内容ID:   &LT; 5a6ebb78429d2_e4a3fd9b60336c4945c7@Jaiss-MacBook-Pro.local.mail>   iVBORw0KGgoAAAANSUhEUgAAAfAAAABMCAYAAACS0 + VcAAAX9UlEQVR4Ae2d   XWxj1bXH / 6lKK + D1VE1oKEa6CPNwpQh4SFBapYOrIoGUuRnN6IRR25HuC1C /   AC8JDMiFGTlP8GIK6KoSbZXkSGjQRLoSrXBpxI3GfgBk1BcDUidAIEG4L5WA   tkj4au199jn7HB9 / xjaJ / BC04 / Ox99pr / bZ91llr7eNM / OMF / 6gj9vriiy9i   R / q7e9111 / VX4DGR9uqrr + LMmTPHRFuqSQIkQAIkcJQJfOsoK0fdSIAESIAE   SIAEkgl8O / lwZ0clUn / ++ edV40cffbSzTodstb + / ryR8 // vfx7e + NZj7j88 /   / XX / + + ctf8M477 + Cjjz5S491444245ZZbcO 99 + L6669Xx1566SWcO3fukBY1   dpfx // SNP + HNN9 / EBx98oBrcdNNNuPPOO / GZN / 0sGL + XJ4 + QAAmQAAmMC4Ge   Hbhx3h9 // // DFuuOGGofL697 // JU8 RSDcOKlUgkvv / wy4mUEceTyr1wu4 / TP   03j33XfVdr8d + BtvvIE // // OEPavw77rgD8k9e4sgvXboEScP OC / X49 // OOH   MudgJEACJEACR4tATw487rwfeuihoVklTluc9yCcuDjv3 / 3ud8qW2dlZ3H33   3ZDIW17ivP / 85z8rp23a9Ntocd4vvvgifvjDH + KRRx7B9773vcgQn332GZ55   5hnVRtYRSER + 5F / lNUyspbB / 2cVkgrIH3klMeW7T8wldejrUfpwy1ibWkNq /   DDeuaNVDrjKD7HQRBXnPzsNJ1KIKL1fEdDaL + XgDX0bOTSf2HJmDxs6ZCnLF   6RasmllsM6xhp1DAXiaH7rEdpm8z3Xo5 / g3oUdtBwQPc7DxqXg5eNaa3k + 1 +   XmRelaA03JyLnj / FIqenz0XMBu4qAl078CTnPcxFaZI2H4QTl7S1RN7y + UUV   f4m5ubnIR0QcuUTbf // 73 / H +++ 9HzvVjR8aXyFucdz6fD0SePXtWba + vryuH   LudWV1eVE7 / tttuOfTp90r2MuhuYO7CN3sepYadYRTrjwkmnkZvvRcVQRi + 9   B9pHLvaFPWQOc1EOFLTtrARHj / 6GfdNw9LVtp2G1WARmsuomswbAyWSRte4o   q14OhVwFmaQbzUThVXheDZlsrvHGNLF95wdrOwUU9jIY + RvbzpF01bKrIvI3   7byNZcaJf + c73wki8a + // tqc7uldat5in0TecedtBErNexDOW + T / 8Y9 / VONL   5N3uJW1EV + nD14AJ1KqoIINMzyEHgH7IGLCZfRE / Lnb2BdaghFRRqaaRsRx2   fKS0m0M2AxSL8dA83tLfr9VQgwMnnlVq0pyHh0eg4wj8qDhvg8Y48X6l0ysV   HTFI2jzpJc5b6t + DesmCNal3x9PmEnnHX9JG2r711ls4depU / LTaL69NYC21   j8t + Prghheyntv93dhn / 00G7 / ZVdTNmp8AMPJ6c8uEHKuUUKWjSS8eZWta6L   m2HKvGWK / QDeySksb +鲁+ VIJmIumuZVdpgGAxc3QZt3L / 79hnKjsxc1NzEY6   6J1atQLMuDpl3pD + 0 + nRooQ5KtLJJKbWIzLgR3vuDCpeEWGEVIOX86AvqfE0   pfQx52Qk + 7xJ0bqAZ9o4segqqifSro54grQo4OVyCI6jSXu5F1ER0wwytSKK   teg4UTs1E / O / RH1e1egdkx + XX / RIeFfZgiKgIkrhFStXNMyPLSPG0KSRfZky   D9VCDkVz3NhqJjceyfpjZZwixA + KU + wozW / bkK42ZD + iEan / Wck42tn686ZZ   + rZZ + qoj1Qqq6Rm0S2o56Rk4xQqqSPvp8CifIGpP / Iw4qrQRojHzqqC1sSmc k9AOD / rj18hQ8QgHinCOnjM6 + MyyGewVGr8Pqo9dBlP2SXYhoewVqnpktzp2   4L / 5zW / wySefKEPk / fz5822N + sEPfoBeV6fLanOpc3f6MjXxqampTrtE2u3t   7al9U / OOnARU + rzfC9bsMT788MPEmradQrfby6p0ceDNXrMLeWytbePAldrz   Aba9LWAL2D5wVY33YLeMRfcc7k3lcV8H7SZnd5HfWgv7b3vY0gLhyk3CwS7K   iy7OxevHouDWMqZmS6jX9U8OyM2F3AzUV5JcprFIO1jP3Uf9shYq / eawiE2 /   iXbesyjVL / vOV24iprCWqqOlaLSXrYeoQmcjk0IP7YQqM1nk / GhHXZDgYNqY   oN6TZNSU3GwuB0dd0AvIFeUClINrnOdODWklV19Ya5lwHHURKuxE6phVr + L3   B5Qe3g7SqlYf11Pve1W5WLrIZeMp9FbtfcPkpiabQy6CJclOn6I471oG2Zy /   dqBWxV7ATY9XDOyNwAt3fMfnBI7Sv2sKW7TeEp3dHHIqk6KZFqvzcNPzyOac   hpsB7RwcuLms5eAK8BzLydSKqj6fa + ctjWZxGzoyoYaipJjNIFUvwrJWtaNo   u4RhBm3y7jhwUEFN3UFqHnE + O + ks5pM + I7Wd7ucvQQ2VCWiZQq + hujcTfG70   nOyglp5X3xuvaM1Pterf / MpANRQL4fcB4qQLHhwpE81n4e7l4O2kkZ2HLo + 5   / S8NJJg7kENdpdC71cBcsLvtx / Z9IDC7gPzWLnaVKHnfxGZ + C7vqgDh0wF2Y   BDpth1ksBP2h5Gxu5rGlBeJAC0xcqAaJuC2POntuE4ur22iZzzjYhreVx4q1   omx2pYRwdYC + KcmXVqzIeRbnNhexut1SMtBWts9fRTOZ5LqfpItr0VRl2k1Y   3JMow0HG9Z2Zk8aMI3XKjO8oHKRnHNT2 / Ku79HcycK2UqDOfQbpWQdVyAPbY   6UwGTm1PRfcqfR / R08F8Jo1qxb7wW5 + 3BrsS2qcTmCTaqSN25bzthX / OvGVP   zF5LlXCzCq9QROi8wzMdb6VdK0JOYyYN1JT3SpJQQ7VSg81Ush6ZjBPl5nRT   WunVBgeZeP3GzK1kBdLpMOvTawnD / 4yFw4itQMX + gNmYup4 / u3M32w7mzfdE   2ToTfq6VmJq + AZHttMkkyI5khqzvYjqDjCOlBT22fD / kDnpnx0PRsT8X + VXX   + r / jCFxWmpsoXCLrBx98EINcvNZJJC11b5NCl3q4LG7r9TU9PQ2JwmW1ebMo 3MiWBWe /// 3vVbv77rvPHD7UuyxeM898dyJI2kqf5q8UUour2C6Lg9tG2T2H   lVQeE9tlrKR24cHFCyqw7bQdkEpp5yi + eLvs4txKCvmJbZRXUtiVGwItsLlK   5sxkCrP + rYU51PC + u4utxRReaDhhDuxid2sRqViDydQs4O3iALPJNxPSva1s   PYY4ufRMk / BKLqLOdHjxNGrF3lvKUG11bdFpUmBUTsaZiY3jYNqpQfn4SBQc   G1x21cVeIqxc9KQzrdP30aNt28ebm / 1EO2tFeCqz0LhqP0yf + hJarDGoep5e   iNWijdGj + bsf6Vs3PU40VWJ1FbbC2DokbkHmqCL1YHERja + oTdHyQn9sEEcl   5Q9JOct8RsdoVcJo0Nava8vNY00cda2KQq4YbZZuZqmf5bHvAQ81N9FhI3tW   Cl8f9wdy5pHNAgUpeygsVmYkIkB29HdM51hldx5upoCCiuAHpXiDEgM50LED   F2dtnLg8 + Y0 / 4DJoJ97K4iTnfZgfdpmZmVEOXB4Va5cql9Xq8iMv1157bSsV   uzonj4S98sorkEfF4nXwuCBpI + nzpaWl + ClrfxIL7iKmtstYQBnuuRVgcgH5   uW2UF4Ct2QVcVq07bSfdXSxO6f7qhgCTWMjPYVsLxIIWaOnQZFPS7QAWmpwO   DvsZhCAr7 / dLqQZy4 + FnFIIGfs / ZVHPnLU1SKSy2lK1reUVZDNTEf0Octx8J   BRdzdVFEmEKv7aCljMDQ5hvNnUajg0mUIno60 + qRokDPxIb + wTbtLf8XSmlm   p2QOZiqqfm8 / eiSOrjidRc7VGqnUaHB1DcWaLYmE4RVQQHQ1tTnf / l07b3kc   TafQtQOKuStLTIsbJJV6tppam5ISjt0mBWcPb0MgSjtxGcgvv4gnm3ealzCs   nsGmWqme8csDcmOS7nwleLfzFwza7YY4b3nkLOevQVH2Wh8UceLqsRB9g +的   5ZX4QFakrk4JKwfpdBVtSzdxUUdsv6sUunHi8sMtxonL4rZhv / rtvEX / N / ZK   J8ohy0I1eR682UvOmcVs / Y染色体长臂+ Zax77rlHjf / ss882Gzo4Ls + Cy82D9Gn1Ug63   vIa1sgvJlkOlwctYW1tFfiGsP3faTm4A3EXpX9bpd5G4kEd5bQ2r + QU / lS31   5Qmc9A5C1baW8VKQ1T6A98AysHnOSn37TWVh3MRJqK6S2scq1iw55ZeWperu   V /肾切除+ rcmpWKL2NtLrRNauYTa8HApiOgMgCtZEtAUoHkEZven6sLub4AGMFy   UbQdXFsZpmOr9 / QM0hLJ7oSSa5L6wwzSnXhkSdEj2r / VcOi2vfiRFqyc + SYY   GXuBnr6YhhkHna5uqZO / yM0pFlAIOGgnG6Z55VEnOyS0JcYj6jCdarcKt3Va   X6LmUKKWn5bce08vvcgqYoP / GTKpXXHInrVgK2kYmfsQgdSx / VeTEoY5Hb7L   zYxekxCUZeQzJrX10NiwecNWm / nrwabIEOK0 / QWbOvsU2iifs + BbENE3fjMr   a0xCY / T3xZQ7xH5JF7pwXRdO0eIZUeR47HQcgRtzjBOXdLpx4sOMxAfhvMU2   + XlU + UMj8iMt8u + 9997DiRMngnS6 + eU149zlWfHwImTo9P4u4 // iF79Qz3c / 9thjePjhh1Ukbq9Cl8hbHLwseJPz5iddm44qDhfL8NwXgog0lQK2VvNYsaPl   TttJxO0Cy55Jv + toFluryEcExjRa3ERqewIT5tH6fAl1q7Yda + 3vzmJlfxMn   p6YwsawPySr0PNaC5vJs9z5OYmpiIjiWL7VbwCZNZ7FSL6kFb8myxanI4vPg   8hjIDzf0BdnLyQI0fVTVTKsmrutERiit + VbjOIivPG7eWaUP57Mu9iw9pXmw   atqZRyadi6xCb9m + Yaz2dooTVwuHcjk9biaNnGeiVYmEWnE2A6bhZjMoFArI   VfQPkcy7GVRkXyFPw3XTCPCbbupdarrFIN0q9ex0xA9b5w1bufFAAQWr9BAw   i8juZqfRBtE5YCEZi4wDzwoy49IlWC76aWM5JzrJ8oiq / FZBk3JPrWgYaWkq   UxCzX7M1cyLtoun5UA + 9JiLQGfH50 / MQnG9jk1rPkbNWoYcDwT6nNLLr / VIC   KoT6qlXzgU0OMtMVdSOgxcl3SJdxql4BRWSQVW1F1wpyhQJ0FsMa / JhsTvT6   18gk8rZr4t2sNj9M7dysTjc178OkzZPmSB4nEwf + 5ZdfJp1Wka84 + mbPiid2   8g928tfI5NfYpL4u48ujYinxuKpsu6vS5hJ5P / DAA4kr1luNPRLn1KNru1ip   2wvXBmCZSd3ZC6 + 6HaYfMrod85toPy52fhNsOx3TTy3358d4Oh30qLaTdHrs   8cKjqmof9Oo6Ajdj2pH4sFebD8p5i21SC5c / WII / 7CLOXBa2idOURW633nqr   SrW3jXwNpB7e5TfOxXHLj7TIs + HmUTFZsCY1b0mbD3L8HlQeUhedet /吉隆坡/ ZA   / eCGVQuy5JfXDjFEP2QcYvihdR0XO4cGtIeBdAnDWnXdgwx2OZ4Eeo7AD2Pu   YSLww4z7TfftJAL / pnU8OuNHf2hF6SWpd + txtKOjKzUhARI4GgQYgR + NeaAW   Y05gEu7lettflBpzSDSfBEggQkDq3UExPHJmFHe6WoU + igBoEwmQAAmQAAkc   RwKJDnzCWtXbb6P6veis3 / pRHgmQAAmQAAkcBwKJDlwWiQ3C0cqNwTXXXHMc   uAxEx + 9 + 97v45z // ORDZFEoCJEACJDBeBBIXsY0XguFZK8 + SF / XVVz09gjY8   LTkSCZAACZDAcSBABz7kWfrb3 / 6G3d1d / OTF / xryyByOBEiABEhglAjQgY / S   bNIWEiABEiCBsSGQWAMfG + tpKAmQAAmQAAkcUwJ04Md04qg2CZAACZDAeBOg   AX / V + af1JEACJEACx5TAt3fe2DmmqlNtEiABEiABEhhfAhP1Yf8lkvFlTctJ   gARIgARIoG8EmELvG0oKIgESIAESIIHhEaADHx5rjkQCJEACJEACfSNAB943   lBREAiRAAiRAAsMjQAc + PNYciQRIgARIgAT6RoAOvG8oKYgESIAESIAEhkeA   Dnx4rDkSCZAACZAACfSNAB1431BSEAmQAAmQAAkMj0CiAy89PQH5293y79Tm   wfC0iY10sHlK6XCxHDshu3sbOOXreLF8gI2nNzB8TUu46OtgeOn3U9jYS9CZ   h0iABEiABEigTwQSHfjcE3XsbyxhaWMfl5YnWw5VevoiSi1b9H5ycvk5rJ9O   6N + AjUfO4vZSHfI7NCdem8LZvya1G / SxOTxev4ILWML6R1oX0Wd / 4 / ZBD0z5   JEACJEACY04g0YF3zqSE15 / svHV / Wy7h5mktce6Jfaz / Z3 + lH0ba5PLjuN / X   7TBy2JcESIAESIAEmhHowIEfYOOMpNMvolS + qFPrZyRdLenju3Ae53HXhJ8y   ttLaOvXup5jPXMRFkXHm11iVlPOZDZT89PjE0yZ + t9PR7aL6SZz4L + DsjRPQ   6fVJ3P / E / TC5ApN6n / D1MvsXn9b6rz6tU / PKJgDBeUnVt7Whdar + YPOinz4P   uV1UJYlV / NrnqPe1jWbssFwR79eORbOp5XESIAESIIGRJiC / hZ702t9Yqi9t   7PunrtQvAHU8daVer + / X108v1dc / klNy / EJdjjZu + 20 + + Wq8vwbSv1 tqH77s   sL893pWnUL9QEpkyltlWg0T + kz4QvQId6vV66YKvp799er0uVohMrb8WEY6h   x7lSUq1i9jSxIaKFz0bpIbpYtio + tv66rbbNZ6GYikD7nL0dGYw7JEACJEAC   JKAIdBCBm / uXC7jyxJzZaXzfu4q3VTQu0bpE5q / gqlnIdfoUTtgp5dPreC5W   W59cvoRLy1DR / l0dpuWlj9ScrzwlWQA / or36NvDkXTpTMHceePkqrvraXvhp   的qP / CTY /克/ GT + 9L93FZieBLqxIUIgrIE31r8v4MSs3TjcP / I / S8B / 3OyfnMN /   byyFOiFsZ / fmNgmQAAmQAAkIgS4ceAfATq9jvx4u5no84rja9Ffp + V8Bz4hD   btMW0VXnqgZ ++ jxe91ery + I7cez63 + MI3bYld / YELjz5ulqAd7CHoJ6Ow9gA   4DD178mbb8dS4NAtXblJAiRAAiRAAjECnTlwFZnGesZ3p2 / G7S + FXW / NI1 /升   i359OtZw7ypeiR2S3dJr53GhdKnzxV9 / tcbCVVx9WS9qEyf4yv2 / DVbGN18l   LxHv23i9XMLrV2 / W9fNObUjQP3JI6uhBbT9yJrIz + aNTwP2 / Ch45K732Nk79   yFTyI025QwIkQAIkQAJRAkmlBFUv9mu6SxvvqDq0qjWfXq + vSy05qDvrGnVQ   9 / Xr26btvl / XVfuq1mvVi59aj8h9J6hnG / lL9ecKCTVupfB + fX3jiq5r + 30g的   dWWpnFuypI4f7tv1aVOPTzrm66Dq57bOutofMrPOBTVw3fdCybBBHUpOfF9L   CXUz6wKS24VjcosESIAESIAE6vUJgRB16dwjARIgARIgARI46gQ6S6EfdSuo   HwmQAAmQAAmMGQE68DGbcJpLAiRAAiQwGgTowEdjHmkFCZAACZDAmBGgAx + Z   Cae5JEACJEACo0GADnw05pFWkAAJkAAJjBkBOvAxm3CaSwIkQAIkMBoE6MBH   Yx5pBQmQAAmQwJgRoAMfswmnuSRAAiRAAqNBgA58NOaRVpAACZAACYwZATrw   MZtwmksCJEACJDAaBOjAR2MeaQUJkAAJkMCYEaADH7MJp7kkQAIkQAKjQYAO   fDTmkVaQAAmQAAmMGQE68DGbcJpLAiRAAiQwGgTowEdjHmkFCZAACZDAmBGg   AX + zCae5JEACJEACo0GADnw05pFWkAAJkAAJjBkBOvAxm3CaSwIkQAIkMBoE   6MBHYx5pBQmQAAmQwJgRoAMfswmnuSRAAiRAAqNBgA58NOaRVpAACZAACYwZ   ATrwMZtwmksCJEACJDAaBOjAR2MeaQUJkAAJkMCYEaADH7MJp7kkQAIkQAKj   QYAOfDTmkVaQAAmQAAmMGQE68DGbcJpLAiRAAiQwGgTowEdjHmkFCZAACZDA   mBGgAx + zCae5JEACJEACo0GADnw05pFWkAAJkAAJjBkBOvAxm3CaSwIkQAIk   MBoE / h87Uz9I1gPvMAAAAABJRU5ErkJggg == ----

我只收到编码字符串,但没有附加文件。我的问题是如何通过附加的Gmail API发送电子邮件?

1 个答案:

答案 0 :(得分:1)

我认为你错过了关于attacment的事情

message              = Mail.new
message.date         = Time.now
message.subject      = 'Test Email'
message.body         = "<p>Hi Test, how's life?</p>"
message.content_type = 'text/html'
message.from         = "Test User <userone@example.com>"
message.to           = 'usertwo@example.com'

service = client.discovered_api('gmail', 'v1')

result = client.execute(
  api_method: service.users.messages.to_h['gmail.users.messages.send'],
  body_object: {
    raw: Base64.urlsafe_encode64(message.to_s)
  },
  parameters:  {
    userId: 'userone@example.com'
  },
    headers:   { 'Content-Type' => 'application/json' }
)

response = JSON.parse(result.body)

对于包含附件的多部分电子邮件:

message         = Mail.new
message.date    = Time.now
message.subject = 'Supertramp'
message.from    = "testone <userone@example.com>"
message.to      = 'testtwo@example.com'

message.part content_type: 'multipart/alternative' do |part|
  part.html_part = Mail::Part.new(body: "<p>Hi TEst, how's life?</p>", content_type: 'text/html; charset=UTF-8')
  part.text_part = Mail::Part.new(body: "Hi test, how's life?")
end

open('http://google.com/image.jpg') do |file| 
  message.attachments['image.jpg'] = file.read 
end