SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I got a PHP/mySQL mailing list which works well. The thing is I would like to be able to say the users name in the content of the email, so I added name to Newsletter_members but now how do I get that into the body of the newsletter ($newslettertext) ?

    I thought some find and replace but then it would need to be inside the email reteen.

    Its quite a small script so I included it below

    include "common.inc";
    $con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
    mysql_select_db($DB_NAME);

    $rst = mysql_query("select Newsletter_Members.NewsletterID, Newsletter_Members.email, Newsletters.Name from Newsletter_Members LEFT JOIN Newsletters ON Newsletters.NewsletterID = Newsletter_Members.NewsletterID WHERE Newsletter_Members.NewsletterID = " . $newsletter, $con);

    while ($row = mysql_fetch_row($rst)) {
    if (!mail($row[1],$subject,$newslettertext,"From: \"" . $row[2] . "\" martin@1stconnection.net\n" )) {
    echo "<FONT FACE=ARIAL SIZE=2 Color=Red>ERROR<FONT Color=Black> : Mail not sent to " . $row[1] . " at " . date("Y-m-d H:i:s") . "</FONT><BR>\n";
    } else {
    echo "<FONT FACE=ARIAL SIZE=2>Mail sent to " . $row[1] . " at " . date("Y-m-d H:i:s") . "</FONT><BR>\n";
    }
    }

    mysql_close($con);

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I would suggest something that seems pretty obvious, but which you didn't do, namely select the user's name from Newsletter_members to start off with. Then you will have the name and some text in $newslettertext.

    From there, you can do whatever you want. There are a hundred string functions and some additional regexp functions that allow you to manipulate strings like $newslettertext. Since you didn't say where you wanted to put the user name in the body of the email, I assume that is unimportant, and so I will leave that up to you.
    Last edited by 7stud; May 18, 2001 at 22:35.

  3. #3
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Yeah I have treid selecting name aswell and putting

    $name = $data["Newsletter_Members.Name"];

    in the while statement however, it didn't work.

    I will probably just have the customers name at the begging of the email. So it would say like "hi Bob" etc. So maybe I could do

    $newslettertext = "hi " . $row[0] . " " . $newslettertext;

    I test that and it worked
    But really I would like to have $name anywhere in the email.

    I tried just $name = $row[0]; which didn't work. I guess I need some find and replace for $newslettertext. If you could surgest some code that would search through $newsletter text and replace $name in the content with $name the value it would be much appricated.

    Thanks again,
    From Martin

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I'm on the right wavelength, what your problem is, is that you will have to re-parse the string that contains $name (within the while loop) so that each time PHP substitutes the *current* value of $name.

    example:
    PHP Code:
    while ($row mysql_fetch_row($rst)) { 
       
    $newslettertext "Hello $row[0]. Thanks for subscribing to Widgets News";
       if (!
    mail($row[1],$subject,$newslettertext,"From: \"" $row[2] . "\"
             [email]martin@1stconnection.net[/email]\n" 
    ))
    ... 
    Get what I mean?

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers, yeah I can get it to do that.

    What I need is some find and replace thing that will replace $name from the text of $newslettertext with $row[0] which would then be placed inside the while ($row = mysql_fetch_row($rst)) as you mentioned.

    That way I would be able to reference the users name where-ever I wanted in the email, which is the desired effect.

    In perl it you could do something like
    foreach $temp (@lines)
    {
    $temp =~ s/$name/$row[0]/g;
    print $temp;
    }

    But what is it in PHP?

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    If I'm on the right wavelength, what your problem is, is that you will have to re-parse the string that contains $name (within the while loop) so that each time PHP substitutes the *current* value of $name.
    Sorry, I did not mean to say $name, I meant to say $row[0] as was in my code.

    What I was getting at is that when you use double quotes to enclose as string, PHP parses the string and substitutes the value of any variable named in the string for you.

    For example:
    PHP Code:
    $names = array("bob""mary""rajiv");
    foreach(
    $names as $name) {
       echo 
    "Mr Widgets welcomes $name as our newest subscriber\n";

    Results:

    Mr Widgets welcomes bob as our newest subscriber
    Mr Widgets welcomes mary as our newest subscriber
    Mr Widgets welcomes rajiv as our newest subscriber

    But if you want to you can roll your own template(ized) string and use str_replace (to use regular expressions there is function ereg_replace() for more complex pattern matching). For example:
    PHP Code:
    $messageTemplate 'Mr Widgets welcomes {NAME} as our newest subscriber';
    ...
    while*($row*=*mysql_fetch_row($rst))*{*
    ***$newslettertext*
    =*str_replace('{NAME}'$row[0], $messageTemplate); 


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
  •