PHP Attaching an Image to an Email

PHP Attaching an image to an email

Try the PEAR Mail_Mime package, which can embed images for you.

You need to use the addHTMLImage() method and pass a content id (cid), which is a unique string of text you will also use in your img's src attribute as a cid: URL. For example:

include('Mail.php');
include "Mail/mime.php";

$crlf = "\r\n";
$hdrs = array(
'From' => 'foo@bar.org',
'Subject' => 'Mail_mime test message'
);

$mime = new Mail_mime($crlf);

//attach our image with a unique content id
$cid="mycidstring";
$mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, $cid);

//now we can use the content id in our message
$html = '<html><body><img src="cid:'.$cid.'"></body></html>';
$text = 'Plain text version of email';

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('person@somewhere.org', $hdrs, $body);

How to Attach Image into Mail Function Without HTML Form

use the PHPmailer script, is the better way, wp_mail is even more easy but that´s another subject, here is a post that will solve your problem.

Send attachments with PHP Mail()?

using wp_mail would be something as easy as this:

<?php
$to = "somobe@yahoo.ca";
$subject = "I am sending an image";
$txt = "Body of the message (can be a html or text/plain)";
$headers = "From: someone else";
$attachment= "image.jpg";// just like this in the root folder of your script, if you have your image in another folder then just write the path.
wp_mail($to,$subject,$txt,$headers,$attachment);
?>

Embedding an image in an email with PHP

Try send with PHPMailer it is simple solution

https://github.com/PHPMailer/PHPMailer/wiki/Tutorial

Works with TLS/SSL smtp connections (gmail need TLS connections)

insert image in mail body

To create an HTML email you can do something like this:

...
$message = "<html><head></head><body>";
$message .= "<img src='link-image.jpg' alt='' /></body></html>";

$headers = "From: $from_email";
$headers .= "Content-type: text/html";

mail($to, $subject, $message, $headers);

This should build an HTML email for you and you should then be able to insert just normal html.

edit
You can read more about how to create HTML emails using PHP from here: http://css-tricks.com/sending-nice-html-email-with-php/

Add Picture In Email Body

You are using a relative path:

<img src=images/c22.jpg>

When you open your mail, that path will mean nothing in a mail client and it is highly unlikely to exist on a webmail client. And if it does, it is not your image...

If you have that image stored on a web-server, you should use the absolute path to that image:

<img src="http://www.your-server.com/images/c22.jpg">

How to add image to php mail body

This should work as expected:

<?php 
...
$body .= '<img src="http://domain.com/absolute/path/to/image/img/logo.png" alt="Hotel" /><br/>';
...
?>

Attaching an uploaded image in PHP mail

Here is the solution I came up with. It took a little finessing based on the upload code and attaching files information. Plus I ran into a problem with the headers, but I managed to get it to work and it works well.

    <?php
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
echo "<br> File submitted.";
}
}
}
else
{
echo "Invalid file";
}

// DO THE MAILING HERE
$filename = "upload/" . $_FILES["file"]["name"];
;
$to3 =
$to4 =
$subject = 'Sustainability Photo Contest';
$bound_text = "dacc1231";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"". PHP_EOL;
$headers .= "From: noreply@dacc.edu\r\n";

$fields = array('name','address','city','state','zip','phone','photo','location');

$greet = "The following was submitted on " . date("F j, Y, g:i a") . "<p>";

$body = $greet;
//$cn = 1;

foreach($fields as $efield) {
if(isset($_POST[$efield])) {

if($efield == "name") {
$body.= "<strong>Name: </strong>" . $_POST[$efield] . "<p>";
} elseif($efield == "address") {
$body.= "<strong>Address:</strong> " . $_POST[$efield] . "<p>";
} elseif($efield == "city") {
$body.= "<strong>City:</strong> " . $_POST[$efield] . "<p>";
} elseif ($efield == "state") {
$body.= "<strong>State: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "phone") {
$body.= "<strong>Daytime Phone Number: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "photo") {
$body.= "<strong>Name of Photo: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "location") {
$body.= "<strong>Location where Photo was taken:</strong> " . $_POST[$efield] . "<p>";

}
}
}

$message = "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."Here is a Submission for the Photo Contest\r\n" .$body ."\r\n"

.$bound;
$file = file_get_contents($filename);

$message .= "Content-Type: image/jpg; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"".$filename."\"\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;

if(mail($to3, $subject, $message, $headers)
&&
mail($to4, $subject, $message,$headers ))
{
echo '<br> MAIL SENT';
} else {
echo '<br> MAIL FAILED';
}

?>


Related Topics



Leave a reply



Submit