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;"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题