Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    trying to use mail() function to send an html styled email

    Hi im trying to use the mail() function in php to send a styled html email but when sent i just recive the code through rather then a view of the styled html email.

    Is there something im meant to do with the code before putting it in the message part of the mail() function as a variable?

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    The PHP documentation for mail() includes a basic guide for creating HTML emails, you need to add something to the headers:

    PHP Code:
    'MIME-Version: 1.0' "\r\n"
    'Content-type: text/html; charset=iso-8859-1' 
    "\r\n" 
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Perhaps I should expand on this a little:

    PHP Code:
    mail(
        
    $to,
        
    $subject,
        
    $message_html,
        
    // Headers:
        
    'From: '.$from."\r\n".
        
    'MIME-Version: 1.0' "\r\n".
        
    'Content-type: text/html; charset=iso-8859-1'

    Last edited by Lamped; 10-27-2009 at 12:08 PM. Reason: I'm cursed, I always forget one or two letters...
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #4
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok thanks it's working now. Although it's not displaying images. Is there a way to get the imail to show images?

  • #5
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Ahh, you want multi-part mime, which gets quite complicated. PHP documentation suggests using:

    http://pear.php.net/package/Mail_Mime

    Or you could just use <img> links to a webserver. I believe hotmail won't display inline images, and most email clients won't display linked images without confirmation. Either way, you've got a fight on your hands.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    If you really want to investigate this matter further, you feel free to steal some code I wrote a while ago. Haven't tested it for ages, but it does illustrate basic use of multi-part mime messages, if you can understand it:

    PHP Code:
    class mai_email {
        private 
    $ffrom;
        private 
    $fto;
        private 
    $fcc;
        private 
    $fbcc;
        private 
    $fattach;
        private 
    $fboundary;
        private 
    $fsubject;
        
        function 
    __construct() {
            
    $this->fboundary '----maiestasweb----boundary'.rand(99999,999999);
            
    $this->ffrom NULL;
            
    $this->fto NULL;
            
    $this->fcc NULL;
            
    $this->fbcc NULL;
            
    $this->fsubject 'No subject';
            
    $this->fattach = array();
        }
        
        function 
    sender($email$name NULL) {
            if (
    is_null($name)) {
                
    $this->ffrom $email;
            } else {
                
    $this->ffrom $name.' <'.$email.'>';
            }
        }
        
        function 
    recipient($to$cc NULL$bcc NULL) {
            if (
    is_array($to)) { $this->fto $to; } else { $this->fto = array($to); }
        if (!
    is_null($cc)) if (is_array($cc)) { $this->fcc $cc; } else { $this->fcc = array($cc); }
          if (!
    is_null($bcc)) if (is_array($bcc)) { $this->fbcc $bcc; } else { $this->fbcc = array($bcc); }
        }
        
        function 
    subject($subj) {
            
    $this->fsubject $subj;
        }
        
        function 
    messageastext($msg) {
            
    $this->fattach[] =
              
    'Content-Transfer-Encoding: quoted-printable'."\n".
          
    'Content-Type: text/plain; charset=windows-1252'."\n".
              
    "\n".
              
    wordwrap($msg);
        }
        
        function 
    messageashtml($msg$setastext True) {
          if (
    $setastext$this->messageastext(strip_tags($msg));
        
    $this->fattach[] =
          
    'Content-Transfer-Encoding: quoted-printable'."\n".
          
    'Content-Type: text/html; charset=windows-1252'."\n".
          
    "\n".
          
    wordwrap($msg);
        }
        
        function 
    attach($filename$content$mime NULL) {
            if (
    is_null($mime)) {
                
    $fileinfo pathinfo($filename,PATHINFO_EXTENSION);
                
    // If the mime module is loaded, use it
                
    if (function_exists('mai_mime_convert')) {
                    
    $mime mai_mime_convert($fileinfo['extension']);
                } else {
                    
    // Minimal essential mime types. Yes, I know it's "officially" application/x-javascript.
                    
    $mimetypes = array('jpg'=>'image/jpeg','htm'=>'text/html','html'=>'text/html','xml'=>'application/xml',
                      
    'css'=>'text/css','txt'=>'text/plain','gif'=>'image/gif','png'=>'image/png','js'=>'text/javascript');
                    if (isset(
    $mimetypes[$fileinfo['extension']])) {
                        
    $mime $mimetypes[$fileinfo['extension']];
                    } else {
                        
    $mime 'application/octet-stream';
                    }
                }
            }
            
    $this->fattach[] =
              
    'Content-Type: '.$mime.";\r\n".
              
    '    name="'.$filename.'"'."\r\n".
              
    'Content-Disposition: inline;'."\r\n".
          
    '    filename="'.$filename.'"'."\r\n".
              
    'Content-Transfer-Encoding: base64'."\r\n".
              
    "\r\n".
              
    chunk_split(base64_encode($content));
        }
        
        private function 
    _headerfmt($data) {
            
    // Safely muli-lines, indents and comma seperates header entries
            
    return implode(",\r\n    ",$data)."\r\n";
        }
        
        function 
    send() {
            
    $header =
              
    'From: '.$this->ffrom."\r\n".
              
    'Reply-To: '.$this->ffrom."\r\n".
              
    'Date: '.date('r')."\r\n".
          
    'MIME-Version: 1.0'."\r\n".
              
    'Content-Type: multipart/alternative;'."\r\n".
              
    '    boundary='.$this->fboundary."\r\n".
              
    'X-Mailer: Maiestas Web module mai_email '.MAI_EMAIL."\r\n".
              
    'To: '.$this->_headerfmt($this->fto);
            if (!
    is_null($this->fcc)) $header .= 'Cc: '.$this->_headerfmt($this->fcc);
        if (!
    is_null($this->fbcc))$header .= 'Bcc: '.$this->_headerfmt($this->fbcc);
        return 
    mail(
              
    implode(', ',$this->fto).(is_null($this->fcc)?'':', '.implode(', ',$this->fcc)).(is_null($this->fbcc)?'':', '.implode(', ',$this->fbcc)),
              
    $this->fsubject,
              
    'This is a multipart message in MIME format.'."\n\n--".$this->fboundary."\n".
              
    implode("\n\n--".$this->fboundary."\n",$this->fattach)."\n\n--".$this->fboundary."--\n",
              
    $header
            
    ); 
        }

    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •