Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
mailgun - send mail with attachment

I am trying to send mail in symfony with attachment with the help of mailgun

I have referred this Question. And this Reference. But didn't helped me.

This is my function,

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

In attachment, I'm passing the file path. i.e /home/mypc/Downloads/test.txt

But I'm receiving only blank mail. Attachment is not coming.

Where am I wrong? Please help.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The mailgun attachments documentation has the following information:

Attachments

You may attach a file from memory or by a file path.

From file path

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

From memory

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

Inline attachments

$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']
  ]
]);
about 4 years ago · Santiago Trujillo Relatório

0

Please replace below code

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

With

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

Eg.

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

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

about 4 years ago · Santiago Trujillo Relatório

0

Example below works for me.

Test

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

You might look into https://github.com/tehplague/swiftmailer-mailgun-bundle as well.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda