1

How can I insert an image in a php mail form?

I have this code:

$headers  = 'MIME-Version: 1.0' . "\n";
        $headers .='Content-Type: text/html; charset="UTF-8"'."\n";
        $headers .='From: text@example.fr';       
        $image = "http://lcda.fr/site/abonnement/img/logo.png";
        //ecriture du message 

        $message .= '<img src=".$image.">';
        $message .= '<p><br/>Votre commande a bien été  enregistrée sous le numéro/ <span style="color:blue">Your command has been successfully recorded under the number :</span> <b>(ref - '.$_SESSION['refvb'].')</b></p>';      
        $message .= '<table width="500">';
        $message .= '<tr><th colspan="2" align="left">Informations abonnement/<span style="color:blue"> Subscription informations</span> :</th></tr>';
        $message .= '<tr><td width="250">Abonnement/<span style="color:blue"> Subscription</span> : </td><td>'.$infoabo['abo'].'</td></tr>';
        $message .= '<tr><td>Durée/<span style="color:blue"> Duration</span> : </td><td>'.$infoabo['duree'].' an(s) </td></tr>';
        $message .= '<tr><td>Quantité/<span style="color:blue"> Quantity</span> : </td><td>'.$infoabo['quantite'].'</td></tr>';
        $message .= '<tr><td>Localisation/<span style="color:blue"> Localisation</span> : </td><td>'.$infoabo['zone'].'</td></tr>';
        $message .= '<tr><td>Prix/<span style="color:blue"> Price</span> : </td><td>'.$infoabo['prix'].' €</td></tr>';  
        $message .= '<tr><td>Commencer l\'abonnement au prochain numéro/<span style="color:blue"> Start the subscription in the next issue</span> : </td><td>'.$startabo.'</td></tr>';  
        $message .= '</table>'; 

        }
        $message .= '<br><p><b>Mode de paiement/<span style="color:blue"> Payment</span> : '.$mode;        
        $message .= '</p>';
        $message .= '';                

        //envoie du mail   
        ini_set("sendmail_from",'test@example.fr');

But the image does not load? Is there any other way to do this? (the point is to put the logo company where the image is placed)

Alpha
  • 121
  • 1
  • 3
  • 10

10 Answers10

4

Your image needs to be hosted on an public URL which is then used in the email

For example

$image = 'http://cdn.mydomain.tld/image.png';    

Update:

As posted by swapnesh (so credit to him), your string concatenation is wrong (although his correction is wrong) so your $image path won't be in your string.

You need

// correct
$string = '<img src="'.$image.'">'; //=> <img src="image.png">

// wrong
$string = '<img src=".$image.">'; //=> <img src="$image">

Note the extra ' single quotes in your string. You are not breaking out of the string to add the variable, and as the main string is enclosed in single quotes PHP does not parse it for interpolation.

fullybaked
  • 4,117
  • 1
  • 24
  • 37
  • Exactly this. Also you should add an "alt" attribute to your image as it will not be loaded by default by most (all?) of the email providers. – Cyrille Armanger Jun 21 '13 at 08:49
  • I have a full path.. I have this link: http://lcda.fr/site/abonnement/img/logo.png – Alpha Jun 21 '13 at 08:49
  • Yes but in your code example, `$image` doens't contain the URL just the image name – fullybaked Jun 21 '13 at 08:50
  • Yeah, sorry.. I was writing the message when I remember that it may not working cuz of not having the full link and I try it with full link but I forgot to change it here.. Does't work with full link also.. – Alpha Jun 21 '13 at 08:54
  • Possibly dumb question, but your email client isn't blocking images is it? If you view the source for the email is the HTML correct? – fullybaked Jun 21 '13 at 08:55
  • I don't think so since I try it in several email hosts, But yes I see the code correctly and now that I added the "alt" attribute I see it... – Alpha Jun 21 '13 at 09:00
3

You have to give full url path in your $image variable.

Like this $image= "http://yourdomain.com/images/image_name.ext"

Amar Banerjee
  • 4,992
  • 5
  • 34
  • 51
2

Set Absolute image path in email.

Also remove . from if $message previously not existing -

$message = '<img src="$image">';

And yes correct it --

$message = "<img src='".$image."'>";
swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • You would only need to use differrent content types in a multipart email with embedded images as a separate part. the content the OP posted is text/html. Removed the downvote though, cos you have pointed out correctly (and i missed) that the string concatenation is wrong for the `$image`, but not quite right on how to fix it\ – fullybaked Jun 21 '13 at 08:59
  • if I do that will that not screw the rest of the mail? – Alpha Jun 21 '13 at 09:01
  • @fullybaked lol I was chking this http://stackoverflow.com/questions/14069231/the-code-is-sending-2-times-without-proper-image so mixed up....after downvote I again look into the code and found the `.` error :) – swapnesh Jun 21 '13 at 09:03
  • @swapnesh although your current edit would work, it won't produce valid html as it will not wrap the image URL in `"` – fullybaked Jun 21 '13 at 09:08
1

Make sure you're image path is absolute and available to user trying to view so something like:

http://www.google.com/image.png

Hugh Downer
  • 106
  • 6
1

Try using the full path to the image. so:

 www.example.com/site/images/link.png
user2067005
  • 859
  • 7
  • 15
1

Glad you solved it. Just to shed some light for other people: It's been a problem of quotation.

This is one of your tries:

$message .= '<img src="$image">';

In php, double and single quotes behave differently. When opening single quotes, everything will be taken as-is. So there will be no parsing of the variable $image. Hence inside your mail source code you will read: <img src="$image"> which of course can't work.

If you want the variable to be recognized as a variable and parsed you have to close the single quotes before the variable. This is a possibility:

$message .= '<img src="'.$image.'">';

Here php will do no parsing, is just writes everything in the single quotes, including the double quotes (which is the smart part), then we leave the quote and php parses again, so recognizes a variable and adds its content. Then (as we enter single quote again) php writes down everything inside, again including the double quote. So you'll get what you want:

<img src="http://lcda.fr/site/abonnement/img/logo.png">

(Note the difference to your first version:

$message .= '<img src=".$image.">';

where you also do not close the single quotes before the variable. So in this case in your mail source code you will read: <img src=".$image.">)

All this is a bit tricky, because when using double quotes variables will be parsed by php inside of the quotes. But as soon if you want to give out double quotes as part of your string (at the latest), it can be quite confusing – and single quotes (which are much more strict) might be easier to understand.

Hope this helps Greetings! oe

dasoe
  • 11
  • 1
0

Your $image must have absolute url; Try the edited code below;

    $headers  = 'MIME-Version: 1.0' . "\n";
    $headers .='Content-Type: text/html; charset="UTF-8"'."\n";
    $headers .='From: text@example.fr';       
    $image = "http://framework.zend.com/images/head-bottom-picture.png";
    //ecriture du message 

    $message .= '<img src="$image">';
    $message .= '<p><br/>Votre commande a bien été  enregistrée sous le numéro/ <span style="color:blue">Your command has been successfully recorded under the number :</span> <b>(ref - '.$_SESSION['refvb'].')</b></p>';      
    $message .= '<table width="500">';
    $message .= '<tr><th colspan="2" align="left">Informations abonnement/<span style="color:blue"> Subscription informations</span> :</th></tr>';
    $message .= '<tr><td width="250">Abonnement/<span style="color:blue"> Subscription</span> : </td><td>'.$infoabo['abo'].'</td></tr>';
    $message .= '<tr><td>Durée/<span style="color:blue"> Duration</span> : </td><td>'.$infoabo['duree'].' an(s) </td></tr>';
    $message .= '<tr><td>Quantité/<span style="color:blue"> Quantity</span> : </td><td>'.$infoabo['quantite'].'</td></tr>';
    $message .= '<tr><td>Localisation/<span style="color:blue"> Localisation</span> : </td><td>'.$infoabo['zone'].'</td></tr>';
    $message .= '<tr><td>Prix/<span style="color:blue"> Price</span> : </td><td>'.$infoabo['prix'].' €</td></tr>';  
    $message .= '<tr><td>Commencer l\'abonnement au prochain numéro/<span style="color:blue"> Start the subscription in the next issue</span> : </td><td>'.$startabo.'</td></tr>';  
    $message .= '</table>'; 

    }
    $message .= '<br><p><b>Mode de paiement/<span style="color:blue"> Payment</span> : '.$mode;        
    $message .= '</p>';
    $message .= '';                

    //envoie du mail   
    ini_set("sendmail_from",'test@example.fr');
Philip F
  • 86
  • 8
0

SOLVED THE ISSUE.

Make your image varible this:

$image = 'http://lcda.fr/site/abonnement/img/logo.png';
user2067005
  • 859
  • 7
  • 15
0

Apparently it works if I put the link directly in the "img" tag.. Thanks

Alpha
  • 121
  • 1
  • 3
  • 10
0

I did the following, based on the other answers:

$comments .= "<center><img src='http://mywebsite.com/thepic468white.jpg' alt='logo' width='468' height='60'></center>";
$comments .= $mail; /*message posted from textarea box on associated htmlpage.*/
$sent = mail($tosubscriber,$subject,$comments,$headers);
Tim Malone
  • 3,364
  • 5
  • 37
  • 50
John
  • 1
  • 1