4

我正在尝试在 symfony 的帮助下发送带有附件的邮件mailgun

我已经提到了这个问题。而这个参考。但没有帮助我。

这是我的功能,

public function sendMail($to, $subject, $body, $attachment = null)
{
    $mgClient = new Mailgun($this->container->getParameter('mailgun_key'));
    $domain = $this->container->getParameter('mailgun_domain');
    $content = [
        'from' => $this->container->getParameter('mailgun_from'),
        'to' => 'tester <' . $to . '>',
        'subject' => $subject,
        'text' => $body
    ];
    if (!empty($attachment)) {
        $result = $mgClient->sendMessage("$domain", $content);
    } else {
        $result = $mgClient->sendMessage("$domain", $content, [
            'attachment[0]' => [$attachment]
        ]);
    }
    return $result;
}

在附件中,我传递了文件路径。IE/home/mypc/Downloads/test.txt

但我只收到空白邮件。附件不来。

我哪里错了?请帮忙。

4

3 回答 3

2

mailgun附件文档包含以下信息:

附件

您可以从内存或文件路径附加文件。

从文件路径

$mg->messages()->send('example.com', [
  'from'    => 'bob@example.com', 
  'to'      => 'sally@example.com', 
  'subject' => 'Test file path attachments', 
  'text'    => 'Test',
  'attachment' => [
    ['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
  ]
]);

从记忆里

// Some how load the file to memory
$binaryFile = '[Binary data]';

$mg->messages()->send('example.com', [
  'from'    => 'bob@example.com', 
  'to'      => 'sally@example.com', 
  'subject' => 'Test memory attachments', 
  'text'    => 'Test',
  'attachment' => [
    ['fileContent'=>$binaryFile, 'filename'=>'test.jpg']
  ]
]);

内联附件

$mg->messages()->send('example.com', [
  'from'    => 'bob@example.com', 
  'to'      => 'sally@example.com', 
  'subject' => 'Test inline attachments', 
  'text'    => 'Test',
  'inline' => [
    ['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
  ]
]);
于 2019-09-25T14:02:44.800 回答
1

请替换以下代码

$result = $mgClient->sendMessage("$domain", $content, [
      'attachment[0]' => [$attachment]
]);

$result = $mgClient->sendMessage("$domain", $content, array(
      'attachment' => array($attachment)
));

例如。

$result = $mgClient->sendMessage("$domain", $content, array(
    'attachment' => array('/home/mypc/Downloads/test.txt')
));

参考:https ://documentation.mailgun.com/user_manual.html#sending-via-api

于 2017-04-17T12:53:47.337 回答
0

下面的例子对我有用。

测试

$mgClient = new Mailgun('key-abcfdfa5b40b6ea0ec0ccf9c33a90y65');
$domain = "sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org";

// SEND
$result = $mgClient->sendMessage(
    $domain,
    [
        'from'    => 'Sender <mailgun@sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org>',
        'to'      => 'Receiver <bentcoder@sandbox111df299cae04a3ea77733f374b73oi8.mailgun.org>',
        'subject' => 'Test subject',
        'text'    => 'Test message body',
    ],
    [
        'attachment' => [
            '/var/www/html/local/test_app/logo.jpg',
            '/var/www/html/local/test_app/ngrok.png'
        ]
    ]
);
// END

print_r($result);

stdClass Object
(
    [http_response_body] => stdClass Object
        (
            [id] => 
            [message] => Queued. Thank you.
        )

    [http_response_code] => 200
)

您也可以查看https://github.com/tehplague/swiftmailer-mailgun-bundle

于 2017-04-18T08:47:36.917 回答