2

WITHOUT using PHPMailer, Swiftmailer, PEAR, Zend_mail, or any other libraries at all, I want to send an email with an image attachment inline.

The important part here is attaching it inline: I already am able to do everything else.

Inline meaning that it is able to be used by the HTML in the email in an image tag.

I really don't want to use PHPMailer or anything like that--I am not the only one who has tried to figure out how to do this on stackoverflow, and so far all the questions I've seen get nothing but arguments about why they should be using PEAR or Zend_mail or something. I don't want to do that, and I don't want to argue about it.

brentonstrine
  • 21,694
  • 25
  • 74
  • 120
  • 2
    Well, you can do that. It's just a whole lot of pointless effort. There were a few users who provided the complete gibberish to accomplish it. Search harder. (Without any code shown in your question here: NARQ vote from me.) – mario Sep 21 '11 at 19:46
  • 2
    Start by reading up on the MIME mail format, and then go from there. Those libraries are simple, easy to use, and reliable. if you want to go through the pain of building your own mime message from the ground up, go for it, but don't come crying when it doesn't work. – Marc B Sep 21 '11 at 19:48
  • possible duplicate of [how to send an HTML email with an inline attached image with PHP](http://stackoverflow.com/questions/7288614/how-to-send-an-html-email-with-an-inline-attached-image-with-php) – Luís Cruz Jul 07 '15 at 10:31
  • Check out this: http://www.phpeveryday.com/articles/PHP-Email-Using-Embedded-Images-in-HTML-Email-P113.html – Justin Valentini Sep 21 '11 at 20:14
  • Thanks. I'm having trouble with their code though, gives a: Parse error: syntax error, unexpected $end at the end of the code, and it's not because of curly brace mismatch or a missing ";" – brentonstrine Sep 22 '11 at 00:04
  • That's probably due to the heredoc syntax. Make sure you don't have any spaces before the EOBODY; line. See here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – Justin Valentini Sep 22 '11 at 13:22
  • "WITHOUT using PHPMailer, Swiftmailer, PEAR, Zend_mail, or any other libraries at all" Why? Is there a **reason** you want to torture yourself like this? – ceejayoz Feb 13 '16 at 20:49
  • You can build mime emails yourself if you want, just... good luck. 99% of email questions on here seem to involve mime being built badly/incorrectly, e.g. "why is the attachment not working". – Marc B Jul 15 '16 at 18:48

2 Answers2

1

is this what you are looking for??

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?> 
ashish
  • 3,555
  • 1
  • 20
  • 26
0

I don't know if this is what you want, but is easy to send an email with mail() function in PHP, in HTML format:

http://php.net/manual/en/function.mail.php

In the examples, there's one with HTML

markmb
  • 852
  • 4
  • 12
  • 32