0

I've been trying to add a png to my php email for quite sometime with no luck. I tried the traditional methods and I've tried to add Base64 to both html and css using this site http://www.base64-image.de

But no matter what I do I get a blank header. Can anyone please tell me, step by step what I have to do to make a png appear on the email I send, and no it can't be an attachment. Thanks in advanced.

 <?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";

?>
ChosenJuan
  • 821
  • 2
  • 18
  • 46
  • I'm not 100% on this, but did you try adding an absolute path to the image source? – ja408 Jul 01 '15 at 19:58
  • i think you might need to move the $mail2->IsHTML(true) line to after you set the Body, according to http://stackoverflow.com/questions/11140263/phpmailer-sending-html-code, also you need to change the img tag not to point to images/logo.png. see the html example on https://css-tricks.com/data-uris/ or you need to point to an http website, not a local file – ecsos Jul 01 '15 at 19:58
  • Yes, I've tried the full src="C:/folder/image.png" – ChosenJuan Jul 01 '15 at 20:00
  • That is not the URL. – Maximus2012 Jul 01 '15 at 20:16

1 Answers1

1

This is very confused. Why are you calling mail() as well as using PHPMailer? I've no idea why you think you need to mess about with headers manually like that - the point of using a library like PHPMailer is so you don't have to concern yourself with things like that!

PHPMailer will not do anything with your image path if you just set Body - you need to pass your body into msgHTML() where it will look up the images and convert everything to embedded images.

There are examples provided with PHPMailer that show you how to do this (if not in the examples folder, look at the unit tests).

I can also see that you've based your code on an old example and are probably using an old version of PHPMailer, so I suggest you update to at least 5.2.10.

Synchro
  • 29,823
  • 14
  • 69
  • 85
  • My phpmailer code is exactly like the one herehttps://github.com/PHPMailer/PHPMailer and can you elaborate on how to pass body to msgHTML()? – ChosenJuan Jul 01 '15 at 20:37
  • There are several things in your code that are from an old exapmple. To use `msgHTML`, look at [this example](https://github.com/PHPMailer/PHPMailer/blob/199bd9698b9b2b2c6b09e046fd12118fd10a7f73/examples/gmail.phps). It's basically `$mail->msgHTML($message, $imagepath);` – Synchro Jul 01 '15 at 21:50