douchuitang0331 2016-06-10 15:23
浏览 24

如何使用PHP和图像内容而不是路径在mailgun中添加内联图像

I am trying to add an inline image to an email sent using the mailgun api using only the contents of the image file.

As far as i know mailgun-php only allows for paths to be specified in the inline element.

Is there a way to add images using their contents without modifying the mailgun-php library?

I've tried inlining the whole thing inside of html img tag but that doesn't work in all email clients (for instance gmail):

<img src="data:image/png;base64,BASE64CONTENTSHERE==" />
  • 写回答

1条回答 默认 最新

  • douji4948 2016-07-14 17:01
    关注

    I have not found a way to do this without modifying the mailgun-php code so I eneded up using the following to store the file in the temporary folder.

            $prefix = 'someprefix'; // Used to easily identify the file if needed
            $filename = uniqid($prefix, true).'.png';
            $path = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
            $f = fopen($path, 'w');
            fwrite($f, $binaryImageContent);   
            fclose($f); 
    

    Then to send the email and remove the temporary file

            $mailgun = new \MailgunClient();
            $response = $mailgun->sendEmail(
                $fromEmail,
                $replyTo,
                array($toEmail), 
                $subject,
                $html,
                array(
                  'inline' => $path
                )                    
            );
    
            unlink($path);
    

    Below is what the MailgunClient class looks like, I had to explicitly set the Guzzle http client, since the default was failing. Note MAILGUN_API_KEY and MAILGUN_DOMAIN have to be set as constants somewhere else in the code using define('MAILGUN_API_KEY', 'key_here'); and define('MAILGUN_DOMAIN', 'domain.com');

    <?php
    
    use Mailgun\Mailgun;
    
    class MailgunClient {
    
        public function sendEmail($from, $replyTo, $to, $subject, $html, $inline = null) {
            $client = new \Http\Adapter\Guzzle6\Client();
            $mailgun = new \Mailgun\Mailgun(MAILGUN_API_KEY, $client);        
            $domain = MAILGUN_DOMAIN;
    
            $params = array();
            $params['from'] = $from;
            $params['h:Reply-To'] = $replyTo;
            $params['subject'] = $subject;
            $params['text'] = $html;
            $params['html'] = $html;
            $params['inline'] = $inline;
            $params['to'] = $to;
    
            $mailgun->sendMessage($domain, $params, $inline);     
        }
    
    }
    

    Finally in the email use the following tag to reference the inlined image. The src attribute has to be cid:the_filename_created to match what the library outputs in the email.

     <img src="cid:'.$filename.'.png" alt="logo" style="width:auto;height:150px;max-height:150px;"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services