SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Insert HTML Into Email?

    Hi,

    I have password reminder system however I am unable insert HTML to it. For example I would like to place a link into the email. However as I add commas it creates an error. Can anyone advise how I can inser HTML into the code. Currently it just prints the code because it has a single comma.


    Code:
            $headers4="admin@website.com";  // Change this address within quotes to your address
    $headers.="Reply-to: $headers4\n";
    $headers .= "From: $headers4\n"; 
    $headers .= "Errors-to: $headers4\n"; 
    //$headers = "Content-Type: text/html; charset=iso-8859-1\n".$headers;// for html mail 
    
    // mail funciton will return true if it is successful
     if(mail("$em","website.com - Reset Password Request","This is in response to your request for login details at website.com \n \nHello $row->firstname $row->surname \n 
     <a href='http://website.com'  rel='nofollow' >
    <img src='/images/logo.png' alt='website.com'/>
    </a> \n
    <a href='http://www.website.com'>Click Here</a>  \n
    Password: $row->password \n\n Thank You \n \n Site Admin","$headers"))
    {echo "<center><b>THANK YOU</b> <br>Your password is posted to your email address . 
    Please check your email account. </center>";}
    
    else{// there is a system problem in sending mail
     echo " <center>There is a problem in sending login details to your address. 
    Please contact Site Admin: . <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center>";}
    	} 
    
    	else {// Validation failed so show the error message
    echo "<center>$msg 
    <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center>";}

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Hi justlukeyou,

    The above code snippet you have posted does have some clear indications as to why your HTML is showing up as plain text, the content-type header currently is commented out in the above code there for its been sent as Content-type: text/plain. To fix this you would simply un-comment that line but another issue would arise by doing that as the line begins with just $header = which would overwrite the previous headers set therefor you would need to change it to read $headers .= which would extend the previous header lines.

    Now I'm hoping you understood all that so we can move onto the bulk of the issues which is the un-readable code, at the moment its hard to see where the mail function ends and what is doing what within it, to clean things up I personally like to set everything in variables and then call them as its far more readable and easier to maintain which as a developer should be of your top priorities during development. On that same note you will notice that the above code also has two else statements which would never work as it would always only ever hit one of them rather then both, see the below which is a cleaned up example of your above snippet which would need some modifications on your part but overall should work without any issues.

    PHP Code:
    // Some kind of validation here
    $validationPassed  true;
    $validationMessage '';

    if (
    $validationPassed) {
        
    $from    "admin@website.com";                    // Change this address within quotes to your address
        
    $to      "someone@somewhere.com";                // The email address of the recepient
        
    $subject "website.com - Reset Password Request"// The subject for the email address

        
    $message  "<a href='http://website.com'  rel='nofollow'><img src='/images/logo.png' alt='website.com'/></a>\n";
        
    $message .= "This is in response to your request for login details at website.com\n\n";
        
    $message .= "Hello $row->firstname $row->surname\n";
        
    $message .= "<a href='http://www.website.com'>Click Here</a>\n";
        
    $message .= "Password: $row->password\n\n";
        
    $message .= "Thank You\nSite Admin";

        
    $headers  "Reply-to: $from\n";
        
    $headers .= "From: $form\n"
        
    $headers .= "Errors-to: $form\n"
        
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n" $headers;

         if (
    mail($to$subject$message$headers)) {
            echo 
    "<div align='center'><strong>THANK YOU</strong><br />Your password is posted to your email address, please check your inbox/spam box.</div>";
        } else{
            echo 
    "<div align='center'>There is a problem in sending login details to your address, please contact site admin.<br /><br /><input type='button' value='Retry' onclick='history.go(-1)'></div>";}
        }
    } else {
        echo 
    "<div align='center'>$validationMessage<br /><br /><input type='button' value='Retry' onClick='history.go(-1)'></div>";

    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    Hi justlukeyou,

    The above code snippet you have posted does have some clear indications as to why your HTML is showing up as plain text, the content-type header currently is commented out in the above code there for its been sent as Content-type: text/plain. To fix this you would simply un-comment that line but another issue would arise by doing that as the line begins with just $header = which would overwrite the previous headers set therefor you would need to change it to read $headers .= which would extend the previous header lines.

    Now I'm hoping you understood all that so we can move onto the bulk of the issues which is the un-readable code, at the moment its hard to see where the mail function ends and what is doing what within it, to clean things up I personally like to set everything in variables and then call them as its far more readable and easier to maintain which as a developer should be of your top priorities during development. On that same note you will notice that the above code also has two else statements which would never work as it would always only ever hit one of them rather then both, see the below which is a cleaned up example of your above snippet which would need some modifications on your part but overall should work without any issues.

    PHP Code:
    // Some kind of validation here
    $validationPassed  true;
    $validationMessage '';

    if (
    $validationPassed) {
        
    $from    "admin@website.com";                    // Change this address within quotes to your address
        
    $to      "someone@somewhere.com";                // The email address of the recepient
        
    $subject "website.com - Reset Password Request"// The subject for the email address

        
    $message  "<a href='http://website.com'  rel='nofollow'><img src='/images/logo.png' alt='website.com'/></a>\n";
        
    $message .= "This is in response to your request for login details at website.com\n\n";
        
    $message .= "Hello $row->firstname $row->surname\n";
        
    $message .= "<a href='http://www.website.com'>Click Here</a>\n";
        
    $message .= "Password: $row->password\n\n";
        
    $message .= "Thank You\nSite Admin";

        
    $headers  "Reply-to: $from\n";
        
    $headers .= "From: $form\n"
        
    $headers .= "Errors-to: $form\n"
        
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n" $headers;

         if (
    mail($to$subject$message$headers)) {
            echo 
    "<div align='center'><strong>THANK YOU</strong><br />Your password is posted to your email address, please check your inbox/spam box.</div>";
        } else{
            echo 
    "<div align='center'>There is a problem in sending login details to your address, please contact site admin.<br /><br /><input type='button' value='Retry' onclick='history.go(-1)'></div>";}
        }
    } else {
        echo 
    "<div align='center'>$validationMessage<br /><br /><input type='button' value='Retry' onClick='history.go(-1)'></div>";

    This is the Working Code for Parsing HTML in Email.


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
  •