Вложение MailGun не отображается PHP - PullRequest
0 голосов
/ 08 мая 2020

Электронная почта отображается без прикрепленных файлов. У меня есть текстовый файл на test / test.txt, который не прикрепляется. Вот простая функция. При использовании электронное письмо отображается без вложения:

function send_email_with_attachment($email,$name){
    $mgClient = new Mailgun(MAILGUN_KEY);
    $domain = "mg.example.com";

    $file = array(
        array(
            'filePath' => 'test',
            'filename' => 'test.txt'
        )
    );

    $result = $mgClient->sendMessage($domain,array(
        "from"    => "Company Support <support@example.com>",
        "to"      => "{$name}<{$email}>",
        "subject" => "File Attached",
        "text"    => "Here is a cool text file",
        'attachment' => json_encode($file)
    )); 
}

Ответы [ 2 ]

0 голосов
/ 08 мая 2020

Проблема была связана с путаницей с моей стороны относительно того, как Mailgun показывает их пример filePath и filename значения переменных.

Как указано выше, вот один из примеров из 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'] //what? Which file is being sent?
  ]
]);

Здесь для меня было несколько ошибок:

  1. Во-первых, я использовал путь относительно файла php, а не из root моего server.
  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'=>'foo.jpg'] //this is a better way to demonstrate
  ]
]);
0 голосов
/ 08 мая 2020

Почему бы не использовать такое вложение напрямую:

$result = $mgClient->sendMessage($domain,array(
        "from"    => "Company Support <support@example.com>",
        "to"      => "{$name}<{$email}>",
        "subject" => "File Attached",
        "text"    => "Here is a cool text file",
        'attachment' => [
    ['filePath'=>'/test.txt', 'filename'=>'test']
  ]
    )); 

Попробуйте использовать этот код из официального источника:

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