dqyy38265 2015-11-23 16:04
浏览 72

如何使用phpmailer在html邮件中正确添加图像?

This is a part of email content

Html:

<table border="0" cellpadding="30" cellspacing="0" width="100%">
 <tr>
  <td align="center" valign="top" class="textContent">
   <img src="img/logo.png" style="height: 100px; margin-top: 13px;" alt="" />
    <br>
    <br>
    <div style="font-family:Helvetica,Arial,sans-serif;font-size:13px;color:#828282;margin-top:-23px;text-align:center;line-height:120%;">LAZOS S.A</div>
    </td>
    </tr>
</table>

The email look of this way:

enter image description here

But I can't display logo.png up "LAZOS S.A"

Inside img folder is logo.png

enter image description here

Inside the folder mailAvisoSinTareasReg is contenido.html

This is my php code using phpmailer:

<?php

require "vendor/autoload.php";

    class HelperMail{

      private $oPhpMailer;

        function __construct(){

            $this->oPhpMailer = new PHPMailer();
            $this->oPhpMailer->isSMTP();
            $this->oPhpMailer->SMTPDebug = 2;
            $this->oPhpMailer->Debugoutput = 'html';
            $this->oPhpMailer->SMTPSecure = 'tls';
            $this->oPhpMailer->SMTPAuth = true;

        }
                public function mailFrom($from,$usuario){
                  $this->oPhpMailer->setFrom($from, $usuario);             
                }
                public function mailPort($puerto){
                  $this->oPhpMailer->Port = $puerto;   
                }
                public function mailUsuario($usuario){
                  $this->oPhpMailer->Username = $usuario;   
                }
                public function mailPassword($pass){
                  $this->oPhpMailer->Password = $pass;       
                }
                public function mailHost($host){
                  $this->oPhpMailer->Host = $host;      
                }
                public function mailSubject($subject){
                  $this->oPhpMailer->Subject = $subject;       
                }
                public function mailAddress($address){
                  /*$this->oPhpMailer->addAddress($address);*/     
                  $this->oPhpMailer->addAddress('jean.bergeret.f@gmail.com');
                }
                public function mailAltBody(){
                  $this->oPhpMailer->AltBody = 'This is a plain-text message body';     

                }

                public function setData ($usuarios){ 

                  $html = '';
                  $htmlmail = file_get_contents('helpers/mailAvisoSinTareasReg/contenido.html'); 
                  foreach($usuarios as $sKey=>$oValue){
                      $html .= '<tbody><tr><td width="50%" align ="center">'.$oValue['nombre_usuario']." ".$oValue['apellido_usuario'].'</td><td width="50%" align ="center" >'.$oValue['rut_usuario'].'</td></tr></tbody>';
                  }
                  $htmlReplace = str_replace("<tr><td>datos</td></tr>",$html,$htmlmail);  
                  $htmlReplaceFecha = str_replace("fecha",$this->setFecha(),$htmlReplace);
                  $this->oPhpMailer->msgHTML($htmlReplaceFecha);
                  $this->sendMail();
                }

                public function sendMail(){

                  if (!$this->oPhpMailer->send()) {
                            echo "Mailer Error: " . $this->oPhpMailer->ErrorInfo;
                        } else {
                            echo "Message sent!";
                        }             
                }

        private function setFecha(){

            date_default_timezone_set("America/Santiago");
            $now = time();
            putenv("TZ=America/Santiago");
            $fecha=date("Y-m-d H:i:s",$now);
            $date=date("d/m/Y", strtotime($fecha));
            return $date;

        }

        }
?>

Sorry by my english.

  • 写回答

2条回答 默认 最新

  • dougu1985 2015-11-23 17:02
    关注

    You can encode your image and use it as img src

    $imgFullpath = '/home/myImage.png';
    $imgType     = 'png';
    $base64      = 'data:image/' . $imgType . ';base64,' . base64_encode($imgFullpath);
    

    ....

    when you build your HTML :

    <img src="' . $base64 . '">;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分