0

我正在使用 mailGun Web API 并遇到了添加内联文件的问题。我们的软件创建一个图像并将其作为字符串传递。我想内联该图像 我遇到的问题是 php curl 接收文件指针,而不是实际文件。如果可能,我想避免编写 tmp 文件,因为我们有许多在服务器上运行的进程并且不想发送错误的电子邮件

提前致谢

MailGun 内联示例:http ://documentation.mailgun.net/user_manual.html#inline-image 我正在使用的代码示例:

function send_inline_image($image) {
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
  curl_setopt($ch,
              CURLOPT_POSTFIELDS,
              array('from' => 'Excited User <me@samples.mailgun.org>',
                    'to' => 'sergeyo@profista.com',
                    'subject' => 'Hello',
                    'text' => 'Testing some Mailgun awesomness!',
                    'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
                    'inline' => $image))

  $result = curl_exec($ch);
  curl_close($ch);

  return $result;
 }
$image = 'Some image string that we have generated'
send_inline_image($image)
4

1 回答 1

4

您只需要更改数组的内联参数。我已经完成了它和它的工作。内联参数应该是一个数组而不是字符串图像路径。你可以这样做:

function send_inline_image($image) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
curl_setopt($ch,
          CURLOPT_POSTFIELDS,
          array('from' => 'Excited User <me@samples.mailgun.org>',
                'to' => 'sergeyo@profista.com',
                'subject' => 'Hello',
                'text' => 'Testing some Mailgun awesomness!',
                'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
                'inline' => array($image)//Use array instead of $image
))

   $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}
$image = 'Some image string that we have generated'
send_inline_image($image)

查看评论“使用数组而不是 $image”

于 2014-07-21T21:39:26.143 回答