rgb192
asked on
embed image php mail()
related question:
https://www.experts-exchange.com/questions/27840780/image-opening-by-default.html
want to embed (not attach)
image
using
php mail (not phpmailer())
I also can not use a mailing list program (constant contact, sendgrid) for this project
https://www.experts-exchange.com/questions/27840780/image-opening-by-default.html
want to embed (not attach)
image
using
php mail (not phpmailer())
I also can not use a mailing list program (constant contact, sendgrid) for this project
ASKER
I dont understand example B
could you put in code that I could run please
could you put in code that I could run please
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Trying to do it raw from mail() is a little hard because of the order in which things happen. It -IS- possible, though. I wrote up a quick class to make it happen. The end result is that it goes out through mail(), though, so you can echo the parts if you want to see how you would write it:
<?php
$Mail = new Gr8GonzosMailWrapper("rgb192@somewhere.com","Test Subject");
$Mail->setBody("Hello! Image is below:<br>".$Mail->embedImage("php.gif")."Testing...<br>\n");
$Mail->setFrom("gr8gonzo@somewhereelse.com"); // Optional
$Mail->Send();
class Gr8GonzosMailWrapper
{
public $from = "";
public $to = "";
public $subject = "";
public $body = "";
public $boundaryRelated = "";
public $boundaryMessageTypes = "";
public $parts = array();
public function __construct($to,$subject)
{
$this->boundaryRelated = "==========RELATED".time()."==";
$this->boundaryMessageTypes = "==========MSGTYPES".time()."==";
$this->setTo($to);
$this->setSubject($subject);
}
public function Send($displayCommand = true)
{
// Build $headers
$headers = "";
if($from) $headers .= "From: {$from}\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related;\n boundary=\"".$this->boundaryRelated."\"\n";
// Build structure
$newbody = "This is a multi-part message in MIME format.\n";
$newbody .= "--".$this->boundaryRelated . "\n";
// Build HTML content
$newbody .= "Content-Type: multipart/alternative;\n boundary=\"".$this->boundaryMessageTypes."\"\n";
$newbody .= "MIME-Version 1.0\n\n";
$newbody .= "--".$this->boundaryMessageTypes."\n";
$newbody .= $this->body[0] . "\n";
$newbody .= "MIME-Version 1.0\n\n";
$newbody .= $this->body[1] . "\n\n";
$newbody .= "--".$this->boundaryMessageTypes."--\n";
// Add images
foreach($this->parts as $part)
{
$contentHeader = $part[0];
$content = $part[1];
$newbody .= "--".$this->boundaryRelated."\n";
$newbody .= $contentHeader . "\n\n";
$newbody .= $content . "\n\n";
}
$newbody .= "--".$this->boundaryRelated."--";
if($displayCommand)
{
echo "mail(\"".$this->to."\",\"".$this->subject."\",\"".$newbody."\",\"".$headers."\");\n";
}
mail($this->to,$this->subject,$newbody,$headers);
}
public function setTo($address) { $this->to = trim($address); }
public function setFrom($address) { $this->from = trim($address); }
public function setSubject($content) { $this->subject = trim($content); }
public function setBody($content) {
$this->body = array("Content-Type: text/html; charset=\"UTF-8\"",trim($content));
}
public function embedImage($image,$cid = "")
{
$data = file_get_contents($image);
$name = basename($image);
if($cid == "") $cid = preg_replace("/[^a-zA-Z0-9]/","_",$name);
if(strpos($cid,"@") == false) $cid .= "@gr8gonzosmailwrapper";
$this->parts[] = array(
"Content-Type: ".mime_content_type($image)."; name=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-ID: <{$cid}>\n" .
"Content-Disposition: attachment;\n" .
" filename=\"$name\"",
chunk_split( base64_encode($data), 68, "\n"));
return "<img src=\"cid:{$cid}\">";
}
}
?>
ASKER
tried on two servers
Fatal error: Call to undefined function mime_content_type() on line 82
Fatal error: Call to undefined function mime_content_type() on line 82
Throw this onto the end of the script:
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
}
ASKER
same error
Fatal error: Call to undefined function mime_content_type() on line 82
Fatal error: Call to undefined function mime_content_type() on line 82
@ rgb192
Please do try with phpmailer with it. I gave ref. links how to do.
Please do try with phpmailer with it. I gave ref. links how to do.
If you are still getting that error, then something went wrong when you pasted in that code. Can you show us the whole script so far?
ASKER
<?php
$Mail = new Gr8GonzosMailWrapper("me@gmail.com","Test Subject");
$Mail->setBody("Hello! Image is below:<br>".$Mail->embedImage("php.gif")."Testing...<br>\n");
$Mail->setFrom("me@gmail.com"); // Optional
$Mail->Send();
class Gr8GonzosMailWrapper
{
public $from = "";
public $to = "";
public $subject = "";
public $body = "";
public $boundaryRelated = "";
public $boundaryMessageTypes = "";
public $parts = array();
public function __construct($to,$subject)
{
$this->boundaryRelated = "==========RELATED".time()."==";
$this->boundaryMessageTypes = "==========MSGTYPES".time()."==";
$this->setTo($to);
$this->setSubject($subject);
}
public function Send($displayCommand = true)
{
// Build $headers
$headers = "";
if($from) $headers .= "From: {$from}\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related;\n boundary=\"".$this->boundaryRelated."\"\n";
// Build structure
$newbody = "This is a multi-part message in MIME format.\n";
$newbody .= "--".$this->boundaryRelated . "\n";
// Build HTML content
$newbody .= "Content-Type: multipart/alternative;\n boundary=\"".$this->boundaryMessageTypes."\"\n";
$newbody .= "MIME-Version 1.0\n\n";
$newbody .= "--".$this->boundaryMessageTypes."\n";
$newbody .= $this->body[0] . "\n";
$newbody .= "MIME-Version 1.0\n\n";
$newbody .= $this->body[1] . "\n\n";
$newbody .= "--".$this->boundaryMessageTypes."--\n";
// Add images
foreach($this->parts as $part)
{
$contentHeader = $part[0];
$content = $part[1];
$newbody .= "--".$this->boundaryRelated."\n";
$newbody .= $contentHeader . "\n\n";
$newbody .= $content . "\n\n";
}
$newbody .= "--".$this->boundaryRelated."--";
if($displayCommand)
{
echo "mail(\"".$this->to."\",\"".$this->subject."\",\"".$newbody."\",\"".$headers."\");\n";
}
mail($this->to,$this->subject,$newbody,$headers);
}
public function setTo($address) { $this->to = trim($address); }
public function setFrom($address) { $this->from = trim($address); }
public function setSubject($content) { $this->subject = trim($content); }
public function setBody($content) {
$this->body = array("Content-Type: text/html; charset=\"UTF-8\"",trim($content));
}
public function embedImage($image,$cid = "")
{
$data = file_get_contents($image);
$name = basename($image);
if($cid == "") $cid = preg_replace("/[^a-zA-Z0-9]/","_",$name);
if(strpos($cid,"@") == false) $cid .= "@gr8gonzosmailwrapper";
$this->parts[] = array(
"Content-Type: ".mime_content_type($image)."; name=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-ID: <{$cid}>\n" .
"Content-Disposition: attachment;\n" .
" filename=\"$name\"",
chunk_split( base64_encode($data), 68, "\n"));
return "<img src=\"cid:{$cid}\">";
}
}
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
}
?>
>>
Please do try with phpmailer with it. I gave ref. links how to do.
I can not use phpmailer because shared hosting companies (godaddy and 1and1 will not work with gmail authentication). I would like to send phpmailer without username,password
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "gmail@gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
ASKER
i am still getting the error with php mail()
ASKER
when I do not use gmail,
this works, thanks
this works, thanks
A. Insert a REFERENCE to an image that is hosted at a remote URL:
Open in new window
or
B. Attach the image to the e-mail and display it using a local reference:
Open in new window
Those are your only two options with email.