mailgun - 发送带附件的邮件

时间:2017-04-17 12:27:37

标签: php symfony email mailgun symfony-3.2

我正尝试在mailgun

的帮助下使用附件发送symfony中的邮件

我已经提到了Question。这Reference。但是没有帮助我。

这是我的功能,

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;
}

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

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

我哪里错了?请帮忙。

3 个答案:

答案 0 :(得分:0)

请替换以下代码

$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')
));

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

答案 1 :(得分: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

答案 2 :(得分:0)

mailgun attachments documentation具有以下信息:

附件

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

来自文件路径

$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']
  ]
]);