database driven html email problem

stevet70

New Member
Messages
35
Reaction score
0
Points
0
I need to be able to set up an email facility for a website that

1. has a small number of templates (eg. header, editable main text body and footer, the information for which will be in a MySQL database)
2. can be sent to multiple email addresses (addresses taken from the same database but different table)
3. use different lists of addresses dependent upon subscription options

I've had varying success so far using PHPMailer v2 and v5

With v2 I was able to create an editable template page and preview page that allowed sending a test email to an address specified within the script. This sorted issue no. 1, however I was unable to get an array of email addresses working so nos 2 & 3 failed.

With v5, which actually announces that it works with databases for multiple email addresses, I'm yet to manage anything worth mentioning.

There's an example with the PHPMailer 5 download for just such a project, the main chunk of which is:

Code:
@mysql_connect('localhost', 'username', 'password'); 
@mysql_select_db("database_name"); 
$query = "SELECT * FROM subs WHERE exhibitions='yes'"; 
$result = @mysql_query($query); 
 
while ($row = mysql_fetch_array ($result)) 
{ 
// HTML body 
$body = "Hello <font size=\"4\">" . $row["first"] . "</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["first"] . ", \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["first"]); 
 
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(); 
}

The result of using this (with all the real password, username etc bit in) is

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource ... on line 22

Line 22 is "while ($row = mysql_fetch_array ($result))"


I really need to get this sorted, but am hitting a very large brick wall.

Any thoughts?

thanks
Steve
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
put this code in before the line that generates the error so we can get some more information:

Code:
if (!$result) {
     exit(mysql_error());
}
 

stevet70

New Member
Messages
35
Reaction score
0
Points
0
Thanks, that was a huge help, showed I'd messed up with the wrong username
- been flipping between the test and live sites where there's a couple of differences due to hosting set up

big oops!

Now I just have to work out the SMTP error messages, I'm guessing to do with authentication, so no doubt I'll be posting another problem soon!

thanks again
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
No problem :)

If you have any more issues, let me know.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Just for information, I have a multiple e-mailshot function on my CRM site.

If you like it, I can forward you the scripts if you're interested.

Mine too has a preview function for either plain text or html e-mail.

The final page is a dynamic table of e-mails, with an option to check/uncheck those you want/ do not want to send to.

I've also built in merge fields to pick up personal information of each recipient within the mailer.

It also confirms each mail as it sends.
 
Last edited:
Top