Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Foreach used to send emails from database?
    text
    copied!<p>I am a bit confused about how to use foreach. I read some internet things on it and I kind of understand how it works, but I don't fully understand it. I think I could use foreach to create a PHP mass emailer that sends blank carbon copy to email addresses and adresses the customer by name in the subject (Dear, Michael Here is your email). I've figured out how to retrieve the names and emails from my database into variables and I know how to email, but I don't know how to send multiple emails at once and to associate the name and email address.</p> <pre><code>&lt;?php //Variables for connecting to your database. //These variable values come from your hosting account. $hostname = "MichaelBerna.db.10339998.hostedresource.com"; $username = "MichaelBerna"; $dbname = "MichaelBerna"; //These variable values need to be changed by you before deploying $password = "********"; $usertable = "subscribers"; $yourfield = "name"; $yourfield1 = "email"; //Connecting to your database $link = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later."); mysql_select_db($dbname); //Fetching from your database table. $query = "SELECT * FROM $usertable"; $result = mysql_query($query); if ($result) { while($row = mysql_fetch_array($result)) { $name = $row["$yourfield"]; $email = $row["$yourfield1"]; echo "Name: $name&lt;br&gt;"; echo "Email: $email&lt;br&gt;"; //mysqli_free_result($result); //mysqli_close($link); } } ?&gt; </code></pre> <p>Here is my email code: </p> <pre><code> &lt;?php require_once '../PHPMailer_5.2.2/class.phpmailer.php'; $name = $_POST['name'] ; $email = $_POST['email'] ; //$file = $_POST['file'] ; // I'm going to later add a file later to be attached in email from database $body = "Hey $name thank you for continuing to be a valued customer! This month's story is included in this email asa an attachment."; $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch try { $mail-&gt;AddAddress($email, $name); $mail-&gt;SetFrom('admins_email@yahoo.com', 'Site Admin'); $mail-&gt;AddReplyTo('admins_email@yahoo.com', 'Site Admin'); $mail-&gt;Subject = "Dear $name Your monthly subscription has arrived!"; $mail-&gt;Body = $body; if ($_FILES['file']['size']) { $mail-&gt;AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);// attachment } $mail-&gt;Send(); echo "Email Sent Successfully&lt;/p&gt;\n"; } catch (phpmailerException $e) { echo $e-&gt;errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e-&gt;getMessage(); //Boring error messages from anything else! } ?&gt; </code></pre> <p>Basically, I need a way to combine these two scripts and link them together and that's what I'm unsure of how to do.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload