SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating HTML Email

    Hi, I'm using PHP 5.2.6 and I need to send automatic emails to help users reset their passwords...

    I've received the email ok, but it displays the html, tags and all... Can anyone point me in the right direction to get it to display correctly?

    Code:
    X-Message-Delivery: Vj0xLjE7dXM9MDtsPTA7YT0xO0Q9MTtTQ0w9Mw==
    X-Message-Status: n:0
    X-SID-PRA: donotreplyATsomethingDOTcom
    X-Message-Info:
    JGTYoYF78jHqxCLOcGSmIalZsSnmX77i+0RJd3fM2tipTgPAv4DzSwwDAS9CLMZize0xQmoU6xgKXEmqN4kZmD40Qw+lF92P
    Received: from WebServer1.DNPwebhostingDOTcom ([213.175.208.2]) by snt0-mc2-f22.Snt0.hotmailDOTcom with Microsoft SMTPSVC(6.0.3790.3959); 	 Tue, 8 Sep 2009 11:39:44 -0700
    Received: from WebServer1 ([127.0.0.1]) by WebServer1.DNPwebhostingDOTcom with Microsoft SMTPSVC(6.0.3790.3959); 	 Tue, 8 Sep 2009 19:40:20 +0100 
    Date: Tue, 08 Sep 2009 19:40:20 +0100 
    Subject: Password Reset Request... 
    To: testATdestinationDOTcoDOTuk 
    From: donotreplyATsomethingDOTcom
    Reply-To: donotreplyATsomethingDOTcom  
    X-Mailer: PHP/5.2.6 
    Return-Path: donotreplyATsomethingDOTcom 
    Message-ID: <WEBSERVER1nHM3SOlbp0000003aATWebServer1.DNPwebhostingDOTcom> 
    X-OriginalArrivalTime: 08 Sep 2009 18:40:20.0042 (UTC) FILETIME=[D1312AA0:01CA30B3]   						
    <html>
    <body>
    <p>
    <a href='locateme.info/b/b_account.php?mode=options&reset=f8bd59688b7b309f865aca711e9e113f'>
    <b>CLICK TO RESET PASSWORD</b>
    </a>
    </p>
    </body> 						
    </html>

  2. #2
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try this class - I haven't tested it in a while. Basically the version I provide below will send HTML emails. However, usually it's best practice to send both a plain text version (for those who have email clients that don't support HTML) and an HTML version. They do this by creating "boundaries" that separate plain text from HTML. But, with that said, here's a version that might help you.

    PHP Code:
    class HtmlMailer {
        

        var 
    $nameOfSender;      // name of Sender
        
    var $to;                  // email to
        
    var $from;              // from email address, whom to reply to
        
    var $htmlMsg;          // html message
        
    var $subject;          // subject
        
        
    public function HtmlMailer($nameOfSender$emailTo$subject$fromEmail$htmlMsg) {
            
            
    $this->nameOfSender     $nameOfSender;
            
    $this->to                 $emailTo;
            
    $this->subject             $subject;
            
    $this->from             $fromEmail;
            
    $this->htmlMsg             $htmlMsg;
            
        }
        
        public function 
    sendMsg() {
            
            
    $headers  'MIME-Version: 1.0' "\r\n";
            
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";    
            
    $headers .= "From: " $this->nameOfSender " <" $this->from ">";
            
            if(
    mail($this->to$this->subject$this->htmlMsg$headers)){
                
                return 
    true;
                
            } else {
                
                return 
    false;
                
            }
            
        
        } 
    // end sendMsg method
        

    }

    $mailer = new HtmlMailer("Steve","testemail@someplace.com","Subject","from@someplace.com","<b>This is a test</b>");
    $mailer->sendMsg(); 
    Hope this helps...

  3. #3
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I managed to get it working, I'm looking at the combined HTML / Plain Text version now LOL...

    I've picked out the following from the message:

    Code:
    --==Multipart_Boundary_xc75j85x 
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
    Is the Content-Transfer-Encoding required? And what is it's recomended 'setting' for HTML and for Plain Text as I've seen a number of different variations

  4. #4
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually set the Content-Transfer-Encoding to 8-bits. Honestly I don't know the answer off the top of my head but there's probably TONS of info about this if you Google it...let me know if you can't get the multipart working as I can probably dig up a script for you too...

  5. #5
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually posted the multipart as a separate one, but I am having trouble, it mails it off ok, but when u recieve it, it's blank LMAO,, but it's def there, cos if you check the full source of the email, u can see it...

  6. #6
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your HTML form contain this parameter:

    enctype="multipart/form-data"

    just want to be sure...

  7. #7
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm, what form, I'm not using a form... It's just PHP kicking off an email internally....

    The code for generating the email is below:

    Code:
    	// - -- --- This function sends message in both plain text and html for maximum compatability..
    
    	$semi_rand = md5(time());
    	$varBoundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    	
    	$vTo = $varTo;
    	$vSubject = $varSubject;
    	$varMessage = wordwrap($varMessage);	
    	$varMessagePlus = wordwrap($varMessagePlus);		
    
    	$varHeaders = "From: donotreply@somewhere.info" . "\n";
    	$varHeaders .= "Reply-To: support@somewhere.info" . "\n";				
    	$varHeaders .= "MIME-Version: 1.0\n";
    	$varHeaders .= "Content-type: multipart/alternative;   boundary=\"{$varBoundary}\"" . "\n"; 
    	$vHeaders = $varHeaders;	
    	
    	$vMessage =
    		"This is a multipart message in MIME format." . "\n" . 					
    		"\r\n\r\n--" . $varBoundary . "\n" . 
    		"Content-Type: text/plain; charset=\"iso-8859-1\"" . "\n" . 
    		"Content-Transfer-Encoding: 7bit" . "\n" . 
    		$varMessage .  
    		"\r\n\r\n--" . $varBoundary . "\n" . 
    		"Content-Type: text/html; charset=\"iso-8859-1\"" . "\n" . 
    		"Content-Transfer-Encoding: 7bit" . "\n" . 
    		$varMessagePlus .  
    		"\r\n\r\n--" . $varBoundary . "--";
    	
    	if (@mail($vTo, $vSubject, $vMessage, $vHeaders) or die()) {
    		 return 0;
    	} else {
    		 return 1;
    	}


Bookmarks

Posting Permissions

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