I want to attach an image and then show it in an email.
The image I want to attach is on my server. I use html in the email.
How do I do this with php? (And I don't want to use any library like PEAR).
I want to attach an image and then show it in an email.
The image I want to attach is on my server. I use html in the email.
How do I do this with php? (And I don't want to use any library like PEAR).
This is the code to do it. Or are you looking for a library that sends mail?
$to = "[email protected]";
$from = "[email protected]"";
$subject = "Hello! This is HTML email";
$message = "
<html>
<body>
<!-- html goes here -->
</body>
</html>
";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
depends on do you want an image to be linked or embedded in email? In the former case use Ross's code and a conventional img tag:
$message = '
<html>....
<img src="http://yourdomain.com/full/path/to/image.gif">
etc
To embed an image you're better off using a library, like phpmailer, or study mime docs http://en.wikipedia.org/wiki/MIME and create your own.
Hi.
I'm using this function. It find all images in my letter and atach it to message.
Parametrs: it take your html (wich you whant to send);
Return: return a needed html and headers, which you can use in mail();
Example usage:
$final_msg = preparehtmlmail($html) // give a function your html
mail('[email protected]', 'your subject', $final_msg['multipart'], $final_msg['headers']); //send email with all images from html attached to letter
function preparehtmlmail($html) {
preg_match_all('~<img.*?src=.([\/.a-z0-9:_-]+).*?>~si',$html,$matches); $i = 0; $paths = array(); foreach ($matches[1] as $img) { $img_old = $img; if(strpos($img, "http://") == false) { $uri = parse_url($img); $paths[$i]['path'] = $_SERVER['DOCUMENT_ROOT'].$uri['path']; $content_id = md5($img); $html = str_replace($img_old,'cid:'.$content_id,$html); $paths[$i++]['cid'] = $content_id; } } $boundary = "--".md5(uniqid(time())); $headers .= "MIME-Version: 1.0\n"; $headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; $headers .= "From: ".DEFCALLBACKMAIL."\r\n"; $multipart = ''; $multipart .= "--$boundary\n"; $kod = 'utf-8'; $multipart .= "Content-Type: text/html; charset=$kod\n"; $multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n"; $multipart .= "$html\n\n"; foreach ($paths as $path) { if(file_exists($path['path'])) $fp = fopen($path['path'],"r"); if (!$fp) { return false; } $imagetype = substr(strrchr($path['path'], '.' ),1); $file = fread($fp, filesize($path['path'])); fclose($fp); $message_part = ""; switch ($imagetype) { case 'png': case 'PNG': $message_part .= "Content-Type: image/png"; break; case 'jpg': case 'jpeg': case 'JPG': case 'JPEG': $message_part .= "Content-Type: image/jpeg"; break; case 'gif': case 'GIF': $message_part .= "Content-Type: image/gif"; break; } $message_part .= "; file_name = \"$path\"\n"; $message_part .= 'Content-ID: <'.$path['cid'].">\n"; $message_part .= "Content-Transfer-Encoding: base64\n"; $message_part .= "Content-Disposition: inline; filename = \"".basename($path['path'])."\"\n\n"; $message_part .= chunk_split(base64_encode($file))."\n"; $multipart .= "--$boundary\n".$message_part."\n"; } $multipart .= "--$boundary--\n"; return array('multipart' => $multipart, 'headers' => $headers);
}