【问题标题】:How can I add a .png to an outgoing PHP Email with HTML?如何将 .png 添加到带有 HTML 的传出 PHP 电子邮件?
【发布时间】:2015-09-19 03:30:33
【问题描述】:

我一直在尝试在我的 php 电子邮件中添加一个 png,但没有成功。我尝试了传统的方法,并尝试使用此站点 http://www.base64-image.de 将 Base64 添加到 html 和 css

但无论我做什么,我都会得到一个空白标题。谁能告诉我,一步一步我必须做什么才能使 png 出现在我发送的电子邮件中,不,它不能是附件。 非常感谢。

 <?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;

$mail2 = new PHPMailer();

// set mailer to use SMTP
$mail2->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail2->Host = "smtp.comcast.net"; // specify main and backup server

$mail2->SMTPAuth = true; // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_name@comcast.net
// pass: password
$mail2->Username = "name@comcast.net"; // SMTP username
$mail2->Password = "*******"; // SMTP password
$mail2->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail2->Port = 465;                                    // TCP port to connect to


// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
//$mail2->From = 'user@gmail.com';


//Set who the message is to be sent from
$mail2->setFrom('user@gmail.com', 'user');


// below we want to set the email address we will be sending our email to.
$mail2->AddAddress("$email");

//Set an alternative reply-to address
$mail2->addReplyTo('talk@gmail.com');


// below we want to set the email address we will be sending our email to.
//$mail2->AddAddress("$email");

// set word wrap to 50 characters
$mail2->WordWrap = 50;
// set email format to HTML
$mail2->IsHTML(true);

mail($to, $subject, $message, $headers);

$mail2->Subject = "Thanks";


$headers = "From: " . strip_tags($_POST['user@gmail.com']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['talk@gmail.com']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


$message = '<html><body>';

$message .= '<div style="height:130px; text-align:center; width:100%; background:#666;"> <img src="images/logo.png"/></div>';

$message .= '<p>Welcome,</p> ';
$message .= "<p>You have been added</p>";


$message .= "</body></html>";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail2->Body = $message;
$mail2->AltBody = $message;

if(!$mail2->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail2->ErrorInfo;
exit;
}

echo "Message has been sent";

?>

【问题讨论】:

  • 我不是 100% 同意这一点,但是您是否尝试过为图像源添加绝对路径?
  • 我认为您可能需要在设置 Body 后将 $mail2->IsHTML(true) 行移动到,根据stackoverflow.com/questions/11140263/…,您还需要将 img 标签更改为不指向图像/标志.png。请参阅css-tricks.com/data-uris 上的 html 示例,或者您需要指向 http 网站,而不是本地文件
  • 是的,我已经尝试了完整的 src="C:/folder/image.png"
  • 那不是网址。

标签: php png sendmail html-email phpmailer


【解决方案1】:

这很混乱。你为什么打电话mail() 以及使用PHPMailer?我不知道你为什么认为你需要像那样手动处理标题 - 使用 PHPMailer 之类的库的目的是让你不必担心这样的事情!

如果您只设置Body,PHPMailer 不会对您的图像路径做任何事情 - 您需要将您的正文传递到msgHTML(),它会在其中查找图像并将所有内容转换为嵌入的图像。

PHPMailer 提供了一些示例,向您展示了如何执行此操作(如果示例文件夹中没有,请查看单元测试)。

我还可以看到您的代码基于一个旧示例,并且可能使用的是旧版本的 PHPMailer,因此我建议您至少更新到 5.2.10。

【讨论】:

  • 我的 phpmailer 代码和这里的一模一样 https://github.com/PHPMailer/PHPMailer 你能详细说明如何将正文传递给 msgHTML() 吗?
  • 您的代码中有几处来自旧示例。要使用msgHTML,请查看this example。基本上是$mail-&gt;msgHTML($message, $imagepath);
猜你喜欢
  • 2016-06-10
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
  • 2015-07-05
  • 2017-02-24
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
相关资源
最近更新 更多