SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Mail() and MySQL

    I would like to send mail to recipients stored in the database and have the message of the mail be taken from the database as well. I've managed to retrieve the recipients and send the mail to each, but I'm having trouble extracting the text for the message. Here's the script I'm working with:

    <?php

    $dbcnx = @mysql_connect("localhost", "root", "");
    mysql_select_db("kyle");

    $recipients = mysql_query("SELECT id, email FROM mailing_list");

    while ($recipient = mysql_fetch_array($recipients))
    {$rid = $recipient["id"];
    $remail = $recipient["email"];

    // Get issue and text from database
    $sql = mysql_query("SELECT issue, text FROM newsletter WHERE id=$id");


    $subject = "$issue";
    $msg = "$text";
    $headers = "From: me@localhost.com\r\nReply-To: ta@byte.net";
    $mail = mail("$remail", "$subject", "$msg", "$headers");

    if (!$mail)
    {
    echo("<P>Error sending mail<BR>".
    "Error: " . mysql_error());}
    else
    {echo "Mail send completed!";}

    ?>

    Running this simply returns a parse error at an undesignated line number.
    Ideas? Thanks in advance.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You never close your while loop for the mysql_fetch_array. You also never actually retireve the data from the issue, text query. BTW why don't you run the query to grab the issue and text before you run the loop for email addresses, so you only need to run it once not once for every address like this.

    PHP Code:
    <?php 

    $dbcnx 
    = @mysql_connect("localhost""root"""); 
    mysql_select_db("kyle"); 

        
    // Get issue and text from database 
        
    $sql mysql_query("SELECT issue, text FROM newsletter WHERE id=$id"); 
        
    $row mysql_fetch_array($sql);
        
    $subject $row["issue"];
        
    $msg $row["text"];
        
    $headers "From: [email]me@localhost.com[/email]\r\nReply-To: [email]ta@byte.net[/email]"
        

        
    $recipients mysql_query("SELECT id, email FROM mailing_list"); 
        
        while (
    $recipient mysql_fetch_array($recipients)) {
            
    $rid $recipient["id"]; 
            
    $remail $recipient["email"]; 
            
    $mail mail("$remail""$subject""$msg""$headers"); 
            
            if (!
    $mail) { 
                echo(
    "<P>Error sending mail<BR>"
                
    "Error: " mysql_error());
                } 
            else {
                echo 
    "Mail to $remail completed!";
                }
            } 

    ?>
    Last edited by freddydoesphp; Aug 11, 2001 at 11:46.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the help
    BTW why don't you run the query to grab the issue and text before you run the loop for email addresses, so you only need to run it once not once for every address
    I don't know. Much of this (php) is still a mystery to me, but getting help on specific problems like this is my favorite way to learn

    Actually, the code you suggest works when I plug in my isp info and upload and run it from there, but when I try the same script on my local machine I get the following:

    Warning: Failed to Connect in C:\Inetpub\wwwroot/my_templates/php_mysql1/CMS/sendmail_fred.php on line 28

    Error sending mail
    Error:
    Warning: Failed to Connect in C:\Inetpub\wwwroot/my_templates/php_mysql1/CMS/sendmail_fred.php on line 28


    Error sending mail
    Error:
    Warning: Failed to Connect in C:\Inetpub\wwwroot/my_templates/php_mysql1/CMS/sendmail_fred.php on line 28


    Error sending mail
    Error:

    This is line 28:
    $headers = "From: me@localhost.com\r\nReply-To: ta@byte.net";

    Notice I've given credit where due.

    Anyway thanks, I appreciate it.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL on the file name. in order to send mail from your local pc, you'll need to put your isp's smpt server in your php.ini file.

    Set the following lines in your php.ini file and then restart your web server

    Code:
    [mail function]
    ; For Win32 only.
    SMTP = myispsmtpserver
    
    ; For Win32 only.
    sendmail_from = me@mydomain.com
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •