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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Emailing blues

  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Emailing blues

    Everywhere says how easy it is to send out email newsletters with php.

    Must be me, then, because I simply cannot find a successful way to send emails to a group of friends who I have in a mysql database.

    I have the sql code, like

    PHP Code:
    $result = @mysql_query("SELECT email FROM mates"); 
    and then the php code

    PHP Code:

    <?php 
    mail
    ('mates@friends.net''Subject''My message.'); 
    ?>
    But how can I make perhaps a variable of the sql code to get it to supply the address for the mail function, please?

    And how can I get the function to send me a BCC of each message?

    Guidance would be appreciated very much.

  • #2
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to supply more info about your database if you want help

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi goughy000

    The code links to the mysql db and the table - "contacts" - has fname, sname, email and a few other fields. Mostly for this project, I just want to use the email addresses. (It would be great if I could start the email, Dear fname sname, but that isn't vital.)

    And that's it. I enter the message in a message variable, I have a subject line, I have my email address as the from, and I ask for a copy of each email to come to me.

    Cheers

  • #4
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is this what you are looking for...


    PHP Code:
    <?php
    // Set Variables

    $mysql_server "localhost";
    $mysql_username "zonkd";
    $mysql_password "psswd";
    $mysql_database "contactdb";


    // Mysql conection
    mysql_connect("$mysql_server""$mysql_username""$mysql_password");
    mysql_select_db("$mysql_database");

    // Mysql Query
    $result mysql_query("SELECT * FROM contacts");

    // Goes through each result sending them the message
    while($row mysql_fetch_array($result)){
       
    $subject "Subject Here";
       
    $message "Dear $row[fname] $row[sname], bla bla";
       
    mail("$row[email]""$Subject""$message");
    }

    // End code
    ?>
    About the BCC... no idea, sorry

    Havnt tested above code, you need to set the variables at the top of the code
    Last edited by goughy000; 02-17-2006 at 01:24 PM.

  • #5
    New Coder
    Join Date
    Aug 2005
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That looks really encouraging, goughy000. Will try it and report back. Many thanks and cheers.

  • #6
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nit picking but you should use quotes around your variable name.

    PHP Code:
    mail("$row[email]""$Subject""$message"); 
    Seeing as though your string is wrapped in double quotes then use single quotes around the variable.

    PHP Code:
    mail("$row['email']""$Subject""$message"); 
    Not using the quotes means you are defining it as a constant.

  • #7
    New Coder
    Join Date
    Aug 2005
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Worked well, thanks, goughy000, with a bit of a tweak here and there.

    Oddly, degsy's suggestion didn't work. Well, it wouldn't accept it. Thanks, degsy, but couldn't follow through there.

    Cheers, goughy000.

  • #8
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see what degsy is getting at, but can't see why its needed

    zonkd.. happy to help you can PM me if you ever want help directly

  • #9
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, that should have been
    PHP Code:
    mail($row['email'], $Subject$message); 
    $row[email] (no quotes) defines a constant.
    $row['email'] or $row["email"] defines a variable. The quotes you use depends on your code.

    e.g. you could also use

    PHP Code:
    mail("{$row['email']}""$Subject""$message"); 
    Although there is really no point.

  • #10
    New Coder
    Join Date
    Aug 2005
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks, degsy, all noted. Thanks for making it clearer.

  • #11
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by degsy
    $row[email] (no quotes) defines a constant.
    That is just not proper syntax. It doesn't define anything.
    PHP Code:
    <?php
    $arr 
    = array();

    $arr[foo] = 'bar'// Here you get an undefined constant notice.

    var_dump(defined('foo')); // gives bool(false)
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #12
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Define was the wrong word. If you have a variable in the form of
    $foo[bar] then PHP will first look for a constant before looking for a variable.

    http://www.zend.com/zend/tut/using-s...ew=1#nonquoted

  • #13
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's incorrect syntax but its not incorrect inside of a string. Becuase everything is a string, the value between [ and ] is also considered to be a string and as such wont cause any errors if you dont have quotes. I prefer using braces ({}) and quotes becuase it looks better to me, but theres nothing wrong with either method IMO.

  • #14
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Using non-quoted strings gives you a notice... if you don't consider that as an error you shouldn't do it anyway. Defining a constant may make your script stop working as expected.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #15
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    Using non-quoted strings gives you a notice... if you don't consider that as an error you shouldn't do it anyway. Defining a constant may make your script stop working as expected.
    If you use that within a string, eg:

    PHP Code:
    <?php
    $var 
    "$variable[var]";
    ?>
    I have never got an error, on E_ALL error reporting. I will do a couple of tests though.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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