Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 07-23-2013, 09:25 PM   #1
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
PHP: How to send mail in a foreach loop?

Here's my code:

PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Control Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../style.css" rel="stylesheet" type="text/css">
</head>

<body class="text">
<h1>Pending Ads</h1>
<form action="" method="post">

<?php

include("../includes/connect.php");

$query_1 "SELECT id, name, last_updated FROM model WHERE status = 'Pending' ORDER BY last_updated DESC";
$result_1 $db->query($query_1);

$num_results $result_1->num_rows;

echo 
"<p>Number of results found: ".$num_results."</p>";

for (
$i=0$i <$num_results$i++) {
  
$row $result_1->fetch_assoc();
  
$id $row['id'];
  
$name $row['name'];
  
$date $row['last_updated'];

  echo 
"<hr>";
  echo 
"<table width=\"450\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
  echo 
"<tr>";
  echo 
"<td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$id."\"></td>";
  echo 
"<td>";
  echo 
"<b>id:</b> $id<br>";
  echo 
"<b>name:</b> $name<br>";
  echo 
"<b>date:</b> $date<br>";
  echo 
"<a href=\"update.php?id=$id\" target=\"_blank\">Edit</a>";
  echo 
"</td>";
  echo 
"</tr>";
  echo 
"</table>";  
}

?>

<p><hr><input type="submit" name="approve" value="Approve"> <input type="submit" name="delete" value="Delete"></p>

<?php

include('../pear/Mail-1.2.0b1/Mail.php');

$error 0;

if (isset(
$_POST['approve']) && $num_results 0)
{
foreach (
$_POST['checkbox'] as $index => $val)
{
$query_2 "SELECT account_id, name, email, DATE_FORMAT(expiry_date, '%b %e, %Y') AS expiry_date_formatted FROM model WHERE id = $val AND status = 'Pending' ";
$result_2 $db->query($query_2);

$list $result_2->fetch_assoc();
$account_id $list['account_id'];

$query_3 "UPDATE model SET status = 'Active' WHERE id = $val AND status = 'Pending' ";
$result_3 $db->query($query_3);

$password substr(md5(uniqid(rand(),1)),rand(0,21),8);

$query_4 "UPDATE member SET password = sha1('$password') WHERE model_id = $val AND account_id = $account_id ";
$result_4 $db->query($query_4);

// This part isn't sending out emails
$recipients "".$list['email']."";

$headers['From']    = "[email protected]";
$headers['To']      = "".$list['email']."";
$headers['Subject'] = "Hello Hello Hello!";

$body "Hi ".$list['name'].",\n\n" .
        
"Account Number: ".$list['account_id']."\n\n" .
        
"Password: ".$password."\n\n" .
        
"Expiry Date: ".$list['expiry_date_formatted']."\n\n" .
        
"Regards,\n" .
        
"Admin";

$params['sendmail_path'] = "/usr/lib/sendmail";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail'$params);

$mail_object->send($recipients$headers$body);
}
}
if (
$result_1 && $result_2 && $result_3 && $result_4 && $error == 0)
{
echo 
"<meta http-equiv=\"refresh\" content=\"0;URL=pending-ads.php\">";
}
?>
</form>
</body>
</html>
Why aren't emails being sent out in the foreach loop? I can't seem to figure it out. Any ideas?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-23-2013, 10:58 PM   #2
alcstrategy
Confirmed User
 
Industry Role:
Join Date: May 2012
Posts: 124
there are some problems with that code in general, but you should add some error handling to help with debugging.

Personally I would dump pear mail and use swiftmailer library. Just a suggestion to make things a little easier for you. They also have some plugins that you might find helpful
swiftmailer.org
alcstrategy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-24-2013, 12:50 AM   #3
PornDiscounts-V
Confirmed User
 
PornDiscounts-V's Avatar
 
Industry Role:
Join Date: Oct 2003
Location: L.A.
Posts: 5,744
Super fast.
__________________
Blog Posts - Contextual Links - Hardlinks on 600+ Blog Network
* Handwritten * 180 C Class IPs * Permanent! * Many Niches! * Bulk Discounts! GFYPosts /at/ J2Media.net
PornDiscounts-V is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-24-2013, 07:23 AM   #4
Dankasaur
So Fucking Fossilized
 
Industry Role:
Join Date: Sep 2011
Posts: 1,432
Quote:
Originally Posted by eMonk View Post
Here's my code:

PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Control Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../style.css" rel="stylesheet" type="text/css">
</head>

<body class="text">
<h1>Pending Ads</h1>
<form action="" method="post">

<?php

include("../includes/connect.php");

$query_1 "SELECT id, name, last_updated FROM model WHERE status = 'Pending' ORDER BY last_updated DESC";
$result_1 $db->query($query_1);

$num_results $result_1->num_rows;

echo 
"<p>Number of results found: ".$num_results."</p>";

for (
$i=0$i <$num_results$i++) {
  
$row $result_1->fetch_assoc();
  
$id $row['id'];
  
$name $row['name'];
  
$date $row['last_updated'];

  echo 
"<hr>";
  echo 
"<table width=\"450\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
  echo 
"<tr>";
  echo 
"<td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$id."\"></td>";
  echo 
"<td>";
  echo 
"<b>id:</b> $id<br>";
  echo 
"<b>name:</b> $name<br>";
  echo 
"<b>date:</b> $date<br>";
  echo 
"<a href=\"update.php?id=$id\" target=\"_blank\">Edit</a>";
  echo 
"</td>";
  echo 
"</tr>";
  echo 
"</table>";  
}

?>

<p><hr><input type="submit" name="approve" value="Approve"> <input type="submit" name="delete" value="Delete"></p>

<?php

include('../pear/Mail-1.2.0b1/Mail.php');

$error 0;

if (isset(
$_POST['approve']) && $num_results 0)
{
foreach (
$_POST['checkbox'] as $index => $val)
{
$query_2 "SELECT account_id, name, email, DATE_FORMAT(expiry_date, '%b %e, %Y') AS expiry_date_formatted FROM model WHERE id = $val AND status = 'Pending' ";
$result_2 $db->query($query_2);

$list $result_2->fetch_assoc();
$account_id $list['account_id'];

$query_3 "UPDATE model SET status = 'Active' WHERE id = $val AND status = 'Pending' ";
$result_3 $db->query($query_3);

$password substr(md5(uniqid(rand(),1)),rand(0,21),8);

$query_4 "UPDATE member SET password = sha1('$password') WHERE model_id = $val AND account_id = $account_id ";
$result_4 $db->query($query_4);

// This part isn't sending out emails
$recipients "".$list['email']."";

$headers['From']    = "[email protected]";
$headers['To']      = "".$list['email']."";
$headers['Subject'] = "Hello Hello Hello!";

$body "Hi ".$list['name'].",\n\n" .
        
"Account Number: ".$list['account_id']."\n\n" .
        
"Password: ".$password."\n\n" .
        
"Expiry Date: ".$list['expiry_date_formatted']."\n\n" .
        
"Regards,\n" .
        
"Admin";

$params['sendmail_path'] = "/usr/lib/sendmail";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail'$params);

$mail_object->send($recipients$headers$body);
}
}
if (
$result_1 && $result_2 && $result_3 && $result_4 && $error == 0)
{
echo 
"<meta http-equiv=\"refresh\" content=\"0;URL=pending-ads.php\">";
}
?>
</form>
</body>
</html>
Why aren't emails being sent out in the foreach loop? I can't seem to figure it out. Any ideas?
Like stated above, do some error handling, and also just use PHP's built in mail() function, it works great. Also it's best to not loop and mail, but to create BCC list by imploding your recipients list and send out that way in one single blast.

If you'd like I can create a custom script for you, or fix this one for $65/hr. Just hit me up. Contact info in signature.
Dankasaur is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-24-2013, 07:33 AM   #5
woj
<&(©¿©)&>
 
woj's Avatar
 
Industry Role:
Join Date: Jul 2002
Location: Chicago
Posts: 47,882
like someone said earlier try mail() 10x easier to get working, if it still doesn't work, icq: 33375924 and I'll work out what the problem is for a few bucks...
__________________
Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager
woj is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-24-2013, 10:44 AM   #6
react
Confirmed User
 
Industry Role:
Join Date: Sep 2003
Location: NZ
Posts: 673
Paste your connect.php, I bet the problem is there.

Just BCC it to me so that Josh doesn't see it.
__________________
--
react

Last edited by react; 07-24-2013 at 10:47 AM.. Reason: Security pro tip.
react is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-24-2013, 01:30 PM   #7
Why
MFBA
 
Industry Role:
Join Date: Mar 2003
Location: PNW
Posts: 7,230
do not implode a list and BCC it all, that will have a number of negative effects on deliverability.

I would agree with using mail() function instead of a library, unless you are sending tens of thousands of mails. Your MTA will most likely be a bigger bottleneck then php's mail() function.

you really shouldnt need much error handling on such a simple script. i think your code could be cut to half as many lines though.
Why is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-25-2013, 12:36 PM   #8
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
If it's best not to loop and mail or implode a list and BCC it all, what is the best way of doing this?

I know my code is newbie as I'm new to the php coding scene (bought a decent book last year) and how the code could be cut in half but I plan on redoing it as I learn more. This is for a small non-profit site to help me learn the language.

Thanks for everyone's input so far.
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-25-2013, 01:41 PM   #9
alcstrategy
Confirmed User
 
Industry Role:
Join Date: May 2012
Posts: 124
Overall without any debugging at all you will not know the exact problem - it could be a number of things.

Using mail may not be simpler if the code problems lie in the database perhaps wrong formatted to address, etc. So your mail code could be fine, but because of something else it isn't working, so changing to mail would make no difference. I don't think there's anything wrong with using pear in your case, I personally dislike it but its just a matter of preference.

Also you want to send mail in batch but the problem with using mail function is that its inefficient in a loop, pear is actually more appropriate usage. Granted you may not have 100k users, but that isn't the point.

I recommended that library because it will simplify things for you and also be efficient. The docs might also help explain things better for you to give you a better understanding in general. Again, I personally dislike pear. It's just a matter of preference

Also if you wanted to add attachments or something it would be a pain in your ass using mail function.

Also your code has efficiency issues and also security issues. You should also try to read about prepared statements

Either way don't let people on a board tell you workarounds or cutting corners for things especially if you don't know better and are just learning. If you know why you are cutting corners then it's fine, but if you do it just blindly then you will never learn "proper"
alcstrategy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-25-2013, 02:36 PM   #10
adult-help
Confirmed User
 
Industry Role:
Join Date: Mar 2008
Posts: 2,450
does database even return any results? also use switftmailer or something like that.

this is definitely a spaghetti code,pretty ugly
adult-help is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.