如何使用Mailgun(PHP)转发图像附件?

问题描述 投票:0回答:1

我将PHP Mailgun SDK用作用户的中间人,使他们可以选择隐藏其电子邮件地址。不幸的是,我不知道如何附加任何嵌入式图像。它们显示为损坏:https://i.imgur.com/lV9LihP.png

Mailgun有documentation,但它已经过时,不能提供我需要的所有信息。

该文档说使用attachment参数作为附件,并且可以按预期工作:附件图像被附加到电子邮件的末尾,并且可以作为常规附件下载。

也表示对inline使用inline attachments参数。为了进行测试,我通过电子邮件客户端(Thunderbird)附加了嵌入式图像:https://i.imgur.com/7Y7vnmY.png

当我使用inline参数时,我得到了与上图相同的断开链接。但是,图像不会作为预览附加到电子邮件的末尾。我仍然可以下载它,但这不是我想要的功能,因为它不是用户要发送的内容。

此外,就算是这样,我也无法分辨是内嵌附件还是常规附件。我将如何以编程方式检测要使用的参数?

Mailgun发布到我的webhook.php文件,该文件处理电子邮件并将其发送。我要使用整个_POST数组,其中包含以下相关信息:

_POST Array
(
    ...
    [attachment-count] => 2
    [content-id-map] => {
        "<[email protected]>": "attachment-1",
         "<[email protected]>": "attachment-2"}
    ...
)

我也可以访问_FILES数组,在此示例中,它看起来像这样:

_FILES Array
(
    [attachment-1] => Array
        (
            [name] => google.png
            [type] => image/png
            [tmp_name] => /tmp/phpy8JHpc
            [error] => 0
            [size] => 118408
        )

    [attachment-2] => Array
        (
            [name] => logo.png
            [type] => image/png
            [tmp_name] => /tmp/phpWcT38i
            [error] => 0
            [size] => 62404
        )

)

这是我用来创建文件数组的代码:

$files_array = [];
foreach($content_map as $cid => $attachment) {
    $file = $_FILES[$attachment];

    // Create attachment info to add to $files_array
    $arr = [];
    $arr["filePath"] = $file["tmp_name"];
    $arr["filename"] = $file["name"];
    $files_array[] = $arr;
}

// Resulting format example:
Array (
    [0] => Array (
        [filePath] => /tmp/phpy8JHpc
        [filename] => google.png
    )
    [1] => Array (
        [filePath] => /tmp/phpWcT38i
        [filename] => logo.png )
    )
)

以及实际发送它的代码:

$this->mailgun->messages()->send($domain, [
  'from'          => $email_from, 
  'to'            => $email_to, 
  'subject'       => $subject, 
  'html'          => $message, 
  'h:Return-Path' => $email_from,
  'h:Reply-To'    => $email_from,
  // AND EITHER OF THE FOLLOWING:
  'attachment'    => $files_array,
  'inline'        => $files_array,
]);

这里是我查看源代码时的图像外观(格式化以提高可读性之后:]

<img
    moz-do-not-send="false"
    src="cid:[email protected]"
    alt="fdsa"
    width="380"
    height="160">

我尝试过的:

  • [使用inlineattachments参数定期附加图像,然后发送电子邮件,希望Mailgun找出其余的内容
  • 使用两个参数附加图像,并用“ cid:/ tmp / phpy8JHpc替换” cid:part1 ...“]
  • 使用两个参数附加图像,并用“ cid:google.png”替换“ cid:part1 ...”
  • 使用两个参数附加图像,并用“ cid:/tmp/phpy8JHpc/google.png替换” cid:part1 ...“

如前所述,电子邮件发送正常。图像被附加,但不是我想要的方式。我没有错误。

php mailgun
1个回答
0
投票

本周我经历了一个非常相似的问题。

我设法通过首先确定哪些文件(实际上)是行内图像,哪些不是行内图像来解决此问题。我使用了一个正则表达式来根据内容ID映射在邮件正文中找到标签(例如cid:1234)。

我已将这些(内嵌图像)附加到“内嵌”属性中,以发送给Mailgun。在邮件正文中找不到所有其他CID文件,我已将其附加到“附件”属性。

棘手的部分(至少对我来说是要弄清楚,仅当附加到“内联”的文件不应该使用其(实际)名称(例如my-file.jpg)作为“文件名”时,它才有效,但是请使用其CID。

请注意,文件名应不带“ ”!

我希望这可以帮助您!

© www.soinside.com 2019 - 2024. All rights reserved.