phpmailer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

phpmailer

Post by cupaball »

I am trying to use this to create a mailing list. I am running into an issue with the line

Code: Select all

while ($row = mysql_fetch_array($result))
I get an error of Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/m/a/l/malikhaynes/html/mailtestmaillist2.php on line 13.

Here is the full code you guys to see:

Code: Select all

<?php 
// Grab our config settings 
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 

// Grab the FreakMailer class 
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc'); 

@MYSQL_CONNECT("localhost","root","1234");
@mysql_select_db("database");
$query  = "SELECT FirstName, Email, FROM users WHERE id=$id";
$result = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array($result))
{
	$mailer = new FreakMailer(); 
	
    // HTML body
    $body  = "Hello <font size=\"4\">" . $row["FirstName"] . "</font>, <p>";
    $body .= "<i>Your</i> personal photograph to this message.<p>";
    $body .= "Sincerely, <br>";
    $body .= "PHPMailer List manager";

    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "Hello " . $row["FirstName"] . ", \n\n";
    $text_body .= "Your personal photograph to this message.\n\n";
    $text_body .= "Sincerely, \n";
    $text_body .= "PHPMailer List manager";

    $mail->Body    = $body;
    $mail->AltBody = $text_body;
    $mail->AddAddress($row["email"], $row["FirstName"]);

    if(!$mail->Send())
        echo "There has been a mail error sending to " . $row["email"] . "<br>";

    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    $mail->ClearAttachments();
}
?>
Any help would be great!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You query has failed. And you can't see it because all warnings and notices are supressed by @
try

Code: Select all

$mysql=MYSQL_CONNECT("localhost","root","1234")
	or die(mysql_error());
mysql_select_db("database", $mysql)
	or die(mysql_error());
$query  = "SELECT FirstName, Email, FROM users WHERE id=$id";
$result = MYSQL_QUERY($query, $mysql)
	or die(mysql_error().': '.$query);
My bets are on "there is no $id" ;)
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Post by cupaball »

you are right! No $id. But where would that have been defined?

Error
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM users WHERE id=' at line 1: SELECT FirstName, Email, FROM users WHERE id=
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Is this someone else's code you're using? It looks like it was written in the assumption that register_globals will be in in php.ini. Do you go to a URL like http://site.tld/script.php?id=xxx ? If you do then you need $_GET["id"] instead. Your script would therefore be vulnerable to SQL injection attacks.

http://www.php.net/manual/en/language.v ... efined.php
http://www.php.net/manual/en/security.globals.php
http://www.php.net/manual/en/security.d ... ection.php
http://uk2.php.net/manual/en/function.m ... string.php
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Post by cupaball »

UPDATE

Now I am getting this
Fatal error: Call to undefined function: addaddress() in /home/content/m/a/l/malikhaynes/html/mailtestmaillist2.php on line 34
Here is the code

Code: Select all

<?php 
// Grab our config settings 
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 

// Grab the FreakMailer class 
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc'); 

$mysql=MYSQL_CONNECT("","","") 
        or die(mysql_error()); 
mysql_select_db("", $mysql) 
        or die(mysql_error()); 
$query  = "SELECT FirstName, Email FROM users"; 
$result = MYSQL_QUERY($query, $mysql) 
        or die(mysql_error().': '.$query);
		
while ($row = mysql_fetch_array($result))
{
	$mailer = new FreakMailer(); 
	
    // HTML body
    $body  = "Hello <font size=\"4\">" . $row["FirstName"] . "</font>, <p>";
    $body .= "<i>Your</i> personal photograph to this message.<p>";
    $body .= "Sincerely, <br>";
    $body .= "PHPMailer List manager";

    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "Hello " . $row["FirstName"] . ", \n\n";
    $text_body .= "Your personal photograph to this message.\n\n";
    $text_body .= "Sincerely, \n";
    $text_body .= "PHPMailer List manager";

    $mail->Body    = $body;
    $mail->AltBody = $text_body;
    $mail->AddAddress($row["Email"], $row["FirstName"]);

    if(!$mail->Send())
        echo "There has been a mail error sending to " . $row["Email"] . "<br>";

    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    $mail->ClearAttachments();
}
?>
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Post by cupaball »

d11wtq wrote:Is this someone else's code you're using? It looks like it was written in the assumption that register_globals will be in in php.ini. Do you go to a URL like http://site.tld/script.php?id=xxx ? If you do then you need $_GET["id"] instead. Your script would therefore be vulnerable to SQL injection attacks.

http://www.php.net/manual/en/language.v ... efined.php
http://www.php.net/manual/en/security.globals.php
http://www.php.net/manual/en/security.d ... ection.php
http://uk2.php.net/manual/en/function.m ... string.php
Yep I am using the example from phpmailer. http://phpmailer.sourceforge.net/extending.html

Should I just use swiftmailer instead?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

cupaball wrote:Yep I am using the example from phpmailer. http://phpmailer.sourceforge.net/extending.html
On a negative note, demonstrating code which requires register_globals to be on, and is also vulnerable to SQL injection is bad. However, I do belive that was not a copy & paste example... it's just a pseudo example to point you in the right direction.
cupaball wrote:Should I just use swiftmailer instead?
Unless you're happy using a library which ceased development in 2005, uses fairly amateur OOP and will not run in the latest versions of PHP without the error level turned down, then yes, it would probably be a good idea ;) You'll get more support too since, well, I write it and I'm actually here :P But then I'm biased.
cupaball
Forum Commoner
Posts: 85
Joined: Sun Feb 12, 2006 1:46 pm

Post by cupaball »

d11wtq wrote:
cupaball wrote:Yep I am using the example from phpmailer. http://phpmailer.sourceforge.net/extending.html
On a negative note, demonstrating code which requires register_globals to be on, and is also vulnerable to SQL injection is bad. However, I do belive that was not a copy & paste example... it's just a pseudo example to point you in the right direction.
cupaball wrote:Should I just use swiftmailer instead?
Unless you're happy using a library which ceased development in 2005, uses fairly amateur OOP and will not run in the latest versions of PHP without the error level turned down, then yes, it would probably be a good idea ;) You'll get more support too since, well, I write it and I'm actually here :P But then I'm biased.

Swiftmailer it is!!!

Thanks for your help. I need to save you as a friend or something because I am sure I will need help with the mailing list from a databas part.
Post Reply