這是郵件內容的一部分如何用html使用phpmailer在郵件中正確添加圖片?
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>
這種方式的電子郵件外觀:
但我不能顯示logo.png
了「LAZOS SA「
img文件夾裏面是logo.png
裏面的文件夾mailAvisoSinTareasReg爲contenido.html
這是使用的PHPMailer我的PHP代碼:
<?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.berge[email protected]');
}
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;
}
}
?>
對不起我的英語。
'img'標籤需要完整的網址。 – Federkun
@Federico不好意思,但你能告訴我一個例子嗎? – Jeanbf
[PHPMailer提供示例](https://github.com/PHPMailer/PHPMailer/blob/master/examples/)展示瞭如何使用鏈接和嵌入圖像。 – Synchro