php php发送带有图像的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10255229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:44:50  来源:igfitidea点击:

php send email with image

phpimagezend-frameworkemailsendmail

提问by user1347693

I am trying to send an email with a picture; the body of my email is:

我正在尝试发送带有图片的电子邮件;我的电子邮件正文是:

<?php 

$message = <<< email

<img src="http://planet-earth.bogus.us/icons/secret.pictures.gif">

Dear {$email},

email;

?>

When I receive the email, I can't see the picture. Instead, I see <img src="http://planet-earth.bogus.us/icons/secret.pictures.gif">. I know it's because of the <<< email; how would I be able to display the picture instead of the code using <<< email?

当我收到电子邮件时,我看不到图片。相反,我看到了<img src="http://planet-earth.bogus.us/icons/secret.pictures.gif">。我知道这是因为 <<< 电子邮件;我如何才能使用 <<< 电子邮件显示图片而不是代码?

UPDATE

更新

I am using zend-framework. My code looks like this:

我正在使用 zend 框架。我的代码如下所示:

<?php
     $mail = new Zend_Mail();
     $mail->setFrom('[email protected]', 'My site');
     $mail->addTo($recoveryaccount->username);
     $mail->setSubject("Mysite");
     include "_email_recover_password.phtml";
     $mail->setBodyText($message);
     $mail->send();
?>

_include "_email_recover_password.pthml" 
<?php 

 $message = <<< email
 <img src="http://picturelocation.picture.gif">
 Dear {$account->getUsername()},

 Somebody has requested that the password associated with this account be
 reset. If you initiated this request, click the below link to complete the process:

 http://www.example.com/{$account->getRecovery()}

 Thank you!
 Mysite

 Questions? Contact us at [email protected]

 email;

 ?>

回答by David Z.

To answer your question, you need to send out content headers that sets the content type to text\html. Without it, your message is treated as plain text by the receiving client. The following code sets the content headers properly and sends out a basic HTML message.

要回答您的问题,您需要发送将内容类型设置为text\html. 没有它,您的消息将被接收客户端视为纯文本。以下代码正确设置内容标题并发送基本 HTML 消息。

// message
$message = '
<html>
<body>
  <img src="http://planet-earth.bogus.us/icons/secret.pictures.gif">
</body>
</html>
';

// Add the content headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'To: David <[email protected]>' . "\r\n";
$headers .= 'From: Bot <[email protected]>' . "\r\n";

mail("[email protected]", "mysubject", $message, $headers);

回答by Marc B

No, it's not because of the heredoc. You need to send a properly formatted HTML mail, which you aren't. There's various headers and MIME 'skeleton' structural stuff that you're not sending at all.

不,这不是因为heredoc。您需要发送格式正确的 HTML 邮件,而您不是。您根本没有发送各种标题和 MIME '骨架' 结构内容。

Don't try to build one yourself. Use Swiftmaileror PHPMailerto do that for you.

不要试图自己建立一个。使用SwiftmailerPHPMailer来为你做这件事。

回答by Muhamad Nabil

What i use ,

我用什么,

$from="From: TEST\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1"; 
mail("[email protected]", $title, $content, $from);