How to send image in PHP mail function

44,391

Solution 1

You cannot display images on text/plain emails as shown above. You must send it as text/html.

So you first have to extend your headers like this:

$header = "From: [email protected]\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";

Then you must update your mailcontent replacing \n or linebreaks with html tags <br> or even <p>

Then you also can include image tags having the image you want to show in the email

<img src="http://www.yourserver.com/myimages/image1.jpg">

This will be downloaded from your webserver when recipient opens it.

.

BUT the much better way will be to use phpMailer Class

Using this, you will be able to include any images IN your email, without need to download it from any website. It is easy to learn and absolutely customizable.

By the way: You should use quotation marks for your $body and $body2 values...

$body= "<<<EOD
Contact Form Details of $nameFeild

Name: $nameFeild \n
City: $cityFeild \n
Country: $countryFeild \n"

Solution 2

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

$message = "<html><head>
<title>Your email at the time</title>
</head>
<body>
<img src=\"http://www.myserver.com/images_folder/my_image.jpg\">
</body>"
mail($email_to, $email_subject , $message,$headers);
Share:
44,391
Vivek Saha
Author by

Vivek Saha

Updated on November 09, 2020

Comments

  • Vivek Saha
    Vivek Saha over 3 years

    I want to send image in mail.How to add image so that it will show in the email I want to add image to the CLIENT MESSAGE BODY. How to use html in client message body?

    Here is my code :

    <?php
    /* subject and email varialbles*/
    
    $emailSbuject = 'Enquiry';
    $webMaster = '[email protected]';
    $emailSbuject2 = 'Thank you';
    $client = ' $emailFeild\r\n';
    /*gathering data variables*/
    
    $nameFeild = $_POST['name'];
    $cityFeild = $_POST['city'];
    $countryFeild = $_POST['country'];
    $emailFeild = $_POST['email'];
    $phoneFeild = $_POST['phone'];
    $otherFeild = $_POST['other'];
    $questionFeild = $_POST['question'];
    $commentFeild = $_POST['comment'];
    $phone1Feild = $_POST['phone1'];
    $hear1Feild = $_POST['hear1'];
    $hear2Feild = $_POST['hear2'];
    $hear3Feild = $_POST['hear3'];
    $hear4Feild = $_POST['hear4'];
    $referralFeild = $_POST['referral'];
    $otherhearFeild = $_POST['otherhear'];
    
    // admin message body
    $body= <<<EOD
    Contact Form Details of $nameFeild
    
    Name: $nameFeild \n
    City: $cityFeild \n
    Country: $countryFeild \n
    Email: $emailFeild \n
    Contact Phone: $phoneFeild \n
    Other Phone: $otherFeild \n
    Question: $questionFeild \n
    Comment: $commentFeild \n
    Contact Over: $phone1Feild \n
    Known Us through: \n
    
     $hear1Feild
     $hear2Feild
     $hear3Feild
     $hear4Feild
     $referralFeild
     $otherhearFeild
    EOD;
    // Client message body
    $body2 = <<<EOD
    
    
    Dear $nameFeild
    
    
    
             Thank u for contacting IntaxFin. One of our representatives will contact you the     soonest. If you have more questions or information needed, please let us know. We are happy to serve     you! \n
    
            -From
             IntaxFin Team \n http://www.intaxfin.com \n
    
             Like us on Facebook \n         
             Follow us on Twitter \n         
             Connect with us on LinkedIn \n
    
    
    -------------------------------------------------------------------------------------------------                     e-mail was automatically sent by IntaxFin Administration Directory and is for your reference. Please do not reply to this e-mail address.
    
    Powered by HexCode Technologies Pvt. Ltd.
    
    EOD;
        $headers = "From: $emailFeild\r\n";
        $header = "From: [email protected]\r\n";
        $success = mail($webMaster,$emailSbuject,$body,$headers);
        $success1 = @mail($emailFeild,$emailSbuject2,$body2,$header);
        /*Result*/
    
    
        $theResults = <<<EOD
    EOD;
    
    echo  "$theResults";
    
    header("Location: http://www.intaxfin.com/thankyou.html");
    exit;
    ?>
    
  • MrvynB
    MrvynB almost 9 years
    This link is what I use for reference (php.net/manual/en/function.mail.php)