开发者

Putting images in a PHP email

I have set up a mail form to send emails from my webpage, and I would like to be able to set up images in these emails. This is the code I currently have:

 $to = "test@test.com";
 $subject = "Emergency details";
 $body = "Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/".$passport."'/>";
 if (mail($to, $sub开发者_开发百科ject, $body)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }

When I send this email, the outputs looks like this:

Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/test.jpg"/>

and actually displays the code rather than the picture. Is it possible to make this display the picture instead?

Thanks for any help


That's because you're actually sending a text mail and not an HTML mail. You have to set the proper headers.

Have a look at the mail() function manual : http://php.net/manual/en/function.mail.php

Specifically : Example #4 Sending HTML email

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


you are sending this email as plain text. You should specify that it should be interpreted as an html mail by using mail()'s fourth argument (headers).

Example can be found in the documentation.

The snippet:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";


You are sending a simple mail and due to this it is interpreted as text only. What you want to do is to send a mail containing html instead of simple text. This require that you include headers to the mail explaining the content of the mail.

Try this:

$headers .= "--$boundary\r\n
Content-Type: text/html; charset=ISO_8859-1\r\n
Content-Transfer_Encoding: 7bit\r\n\r\n";

$to = "test@test.com";
$subject = "Emergency details";
$body = "Passport picture: <img src='http://www.test.co.uk/files/passport_uploads/".$passport."'/>";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

(example snapped from http://www.daniweb.com/web-development/php/threads/2959)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜