SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hyperlinks in html mail

    <supplication>Hello oh wise and great ones </supplication>

    I'm sending html mail to a mailing list but can't seem to get hyperlinks pulled from a MySQL text column to show up properly in the mail.

    The mail is constructed from data fields in MySQL.

    For example:

    if the MySQL data field "text_for_htmlmail" contains:
    This is the message I want to be sent


    I create a message by combining it with html in the script and sending it out with the mail function

    PHP Code:
     $message "<html><body> Welcome to the newsletter  $text_for_htmlmail</html></body>";
    mail($to$subject$message$header); 
    The problem is that hyperlinks in the text pulled from the MySQL database are not showing up as hyperlinks. Even if I write out a proper hyperlink it still gets displayed in the email literally. Here are two examples:

    http://www.google.com
    <a href='http://www.google.com'>try google</a>
    That is exactly the text that was pulled from MySQL and it displays the html mail in outlook express WITHOUT underlining or activating the links. Here's the odd thing. The links that I set up in the script (eg. not pulled from the db, but part of the template) show up and work just fine.

    Can anyone tell me what I might need to do to get hyperlinks (that are part of some text pulled from a MySQL db) to show up properly in an html email?

    Please help as I am genuinely confused
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  2. #2
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to send the e-mail as a MIME e-mail, with HTML part. Read this:
    http://www.php.net/manual/en/function.mail.php
    Code:
    /* To send HTML mail, you can set the Content-type header. */
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

  3. #3
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I thought I had sent it as a MIME mail

    Here's the code that sets up the headers...am I doing this right? The thing is, all of the other html portions of the mail show up just fine. It is only the links pulled from the MySQL db that are not displaying properly in the mail once it is opened in Outlook Express.

    PHP Code:
    $htmlheader "From:newsletter@teachingfish.com
    MIME-Version: 1.0 
    Content-type: text/html; charset=iso-8859-1"

    that is then put into the message like so:


    PHP Code:
     mail($to$subject$htmlmessage$htmlheader); 
    Am I not setting up the headers properly?

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok what if you just put in the url? i.e
    Code:
    http://www.example.com/
    without anchor tags? I think <a> only works in AOL. I think at least...

  5. #5
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    it just shows up as text

    If I modify the record in MySQL to have URL's as:

    www.site.com

    then they just show up in the HTML mail as

    www.site.com


    The thing I don't understand is that the script has other URL's coded in that DO work. HERE is a code snippet:

    $htmlmessage = "
    <html><header><link rel='stylesheet' href='http://www.teachingfish.com/styles.css' type='text/css'></header><body bgcolor='#B0C0D0'>
    <h1 align='center'>The teachingfish.com newsletter</h1>
    <table width='500' border='0' cellspacing='0' cellpadding='0'
    align='center' >
    <tr>
    <td width='500' >
    <table width='500' border='0' cellpadding='0' cellspacing='0' >
    <tr >
    <td bgcolor='#B0C0D0' colspan='2' valign='bottom' >
    <table width='100%' border='0' cellspacing='0' cellpadding='0'>
    <tr>
    <td width='38%' height='49' valign='bottom'><a href='http://www.teachingfish.com'><img src='http://www.teachingfish.com/images/teachingfishlogo.gif' width='180' height='48' border='0'></a></td>
    <td align='right' height='5' valign='bottom'>
    <table width='100%' border='0' cellspacing='0' cellpadding='0' align='right'>
    <tr>
    <td height='24' align='right' valign='bottom'>
    <p><b><font color='#666666'><a name='top'></a>Teachingfish Newsletter Issue # $id
    </font></b></p>
    </td>
    </tr>
    <tr>
    <td align='right' valign='top'>
    <p class='p_small'><a href='http://www.teachingfish.com/advertising.php' target='_blank'>advertising</a>
    | <a href='http://www.teachingfish.com/free-resources.php'>get
    free teaching materials</a> | <a href='http://www.teachingfish.com/'>home</a><br>
    </p>
    </td>
    </tr>
    </table>


    That example is just part of the html that is built in the script. All of the hyperlinks in that (and all the other html in the script that is not pulled from MySQL)show up and work in the resulting html mail.

    Here's an example of the variable placement in the script:


    <td width='100%' bgcolor='#FFFFE1' ><font face='Verdana, Arial, Helvetica,
    sans-serif' size='2' color='#335588'><font color='#FF0000'><b>

    $advertisement_1_title<br><br>

    </b></font>

    $advertisement_1_copy<br><br>
    $advertisement_1_link<br><br>
    </font></td >
    The link held in the $advertisement_1_link variable will not display as a hyplerlink.

    The code to get the contents of the MySQL db is pretty standard, I send the SELECT statement, place it in an array and then call it out. Then I run the htmlspecialchars function on it:

    $advertisement_1_link = htmlspecialchars($advertisement_1_link);
    Any suggestions? My hair's falling out on this.

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  6. #6
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    here's what the source code REALLY is

    This is the source code that I traced back...

    &lt;a href='http://www.philiptoews.com'&gt;my site&lt;/a&gt;

    That is what is being placed in the variable before it goes to the mailer. How do I change it so that the <> symbols are preserved?

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  7. #7
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the problem:
    Code:
    $advertisement_1_link = htmlspecialchars($advertisement_1_link);
    because you are using htmlspecialchars(), all HTML characters (in our case, < and >) are escaped.

    Why do you have that line in there? I'd remove it otherwise you will never get this to work.

  8. #8
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The sound of me slapping my forehead

    wap wap wap wap



    Yup...that is the problem. I had it in there because I want to have people other than administrators submit newsletter content. htmlspecialchars will clean up that kind of stuff. Regardless, it is better to keep the content submission apart from the mail out management features...like you say, otherwise it'll never work.

    Thanks for your help.

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com


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
  •