hashstar Posted May 15, 2012 Share Posted May 15, 2012 I am trying to send out an email to a mailing list with php mailer. The problem i am having is how to use the email addresses that are in the database. I can access the database and get the data, i can pass the data in a form but i can't figure out how to get it into $mailer->AddAddress this is the code i am trying at the minute <?php require('connect.php'); // Grab our config settings require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); // Grab the FreakMailer class require_once($_SERVER['DOCUMENT_ROOT'].'/include/MailClass.inc'); // instantiate the class $mailer = new FreakMailer(); // Set the subject $mailer->Subject = 'This is a test'; // Body $mailer->Body = 'This is a test of my mail system!'; //loop through for($x=0;$x<count($_GET);$x++) { if ($_GET["mail_$x"]) { } } if(!$mailer->Send()) { echo 'There was a problem sending this mail!'; } else { echo 'Mail sent!'; } $mailer->ClearAddresses(); $mailer->ClearAttachments(); ?> this is the part that contains the email address : for($x=0;$x<count($_GET);$x++) { if ($_GET["mail_$x"]) Where do i go from here? I know i really need to understand more of the basics, but I've started this now and it's really bugging me that i can't get it to work! Please help me :-\ Hashstar Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/ Share on other sites More sharing options...
scootstah Posted May 15, 2012 Share Posted May 15, 2012 What you are attempting to do looks pretty dangerous. Never trust your input, especially with sending emails. If you are just feeding in whatever $_GET has to offer, your script could potentially be used to send mass spam by an attacker. Anyway, you said you need to get emails out of the database but I don't see that happening anywhere, nor do I see any form data. What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345597 Share on other sites More sharing options...
hashstar Posted May 15, 2012 Author Share Posted May 15, 2012 Hi scootstah, I am trying to send out a an email to a mailing list. This is the php page that i get the data from mysql database: <?php require('connect.php'); echo "<h1>Mailing List</h1>Send to<p>"; //setup variables $mailcount = 0; $get = mysql_query("SELECT * FROM mailinglist"); echo"<form action='send.php' method='GET'>"; while ($getrow = mysql_fetch_assoc($get)) { echo "<input type='checkbox' name='mail_".$mailcount++."' value='".$getrow['email']."' CHECKED>".$getrow['email']."<br>"; } echo "<input type='submit' name'submit' value='send'> </form>"; ?> I had tried it another way which worked just as well <?php // Grab our config settings require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); // Grab the FreakMailer class require_once($_SERVER['DOCUMENT_ROOT'].'/include/MailClass.inc'); // instantiate the class $mailer = new FreakMailer(); // Set the subject $mailer->Subject = 'This is a test'; // Body $mailer->Body = 'This is a test of my mail system!'; // Get the user's Email $con = mysql_connect("organicgrowshopcouk.fatcowmysql.com","worm","*aldoushuxley2012*"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ogs_mailinglist1", $con); $result = mysql_query("SELECT * FROM mailinglist"); while($row = mysql_fetch_array($result)) { echo $row['email'] . " " ; echo "<br />"; } if(!$mailer->Send()) { echo 'There was a problem sending this mail!'; } else { echo 'Mail sent!'; } $mailer->ClearAddresses(); $mailer->ClearAttachments(); ?> i don't know how to get the the email address into $mailer->AddAddress tho... Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345634 Share on other sites More sharing options...
mrMarcus Posted May 15, 2012 Share Posted May 15, 2012 You want to have a form that draws multiple email addresses where a user can check the box to select whom to send an email to? Am I getting that correctly? Or is this just one single email address? Your code suggests multiple, plus please clarify. Instead of: name='mail_".$mailcount++."' Use an array for easier use when compiling your list of addressees for mailing: name='mail[]' Then you can just fire that array into the class using options that PHPMailer allows (probably a semi-colon delimited string of email addresses): $emails = implode(';', $_GET['mail']); $mailer->AddAddress = $emails Pseudo-code, so please refer to the class to ensure accuracy. I would suggest moving from GET to POST. No reason other than POST is cleaner. Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345662 Share on other sites More sharing options...
hashstar Posted May 15, 2012 Author Share Posted May 15, 2012 Hi mrMarcus What am i looking for in the class to ensure accuracy? not sure what bit of this i need to change and what to change it to?: $emails = implode(';', $_GET['mail']); $mailer->AddAddress = $emails Cheers Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345683 Share on other sites More sharing options...
mrMarcus Posted May 15, 2012 Share Posted May 15, 2012 You want to have a form that draws multiple email addresses where a user can check the box to select whom to send an email to? Am I getting that correctly? Or is this just one single email address? Your code suggests multiple, plus please clarify. Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345686 Share on other sites More sharing options...
hashstar Posted May 15, 2012 Author Share Posted May 15, 2012 Yep you're correct. It is for multiple email addresses. Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345689 Share on other sites More sharing options...
mrMarcus Posted May 15, 2012 Share Posted May 15, 2012 OK - here's the idea... and this code is basic/raw/untested. I don't know what's up with your config file and such in terms of where you're making your connection to your database, so I'll let you plug that stuff in. <?php // Grab our config settings require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); // Grab the FreakMailer class require_once($_SERVER['DOCUMENT_ROOT'].'/include/MailClass.inc'); $result_message = ''; if (isset($_POST['submit'])) { $errors = array(); if (empty($_POST['email_addresses'])) { $errors['emails'] = 'No emails selected.'; } // here you can/should loop through and verify email addresses to avoid spamming; put a restriction to # of emails allowed to be sent, etc. // santizing/controlling your email functions is greatly overlooked and can cause your site to be dropped by your hosting provider if your site is being used to send gobs of spam if (empty($errors)) { $emails = implode(',', $_POST['email_addresses']); $result_message = 'There was a problem sending this mail!'; // instantiate the class $mailer = new FreakMailer(); // Get the user's Email $mailer->AddAddress($emails); // Set the subject $mailer->Subject = 'This is a test'; // Body $mailer->Body = 'This is a test of my mail system!'; if ($mailer->Send()) { $result_message = 'Mail sent!'; } $mailer->ClearAddresses(); $mailer->ClearAttachments(); } } $sql = "SELECT * FROM mailinglist"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { echo (!empty($result_message) ? $result_message .'<br/><br/>' : ''); echo " <h1>Mailing List</h1> Send to<br/><br/> <form action='send.php' method='POST'> "; while ($getrow = mysql_fetch_assoc($result)) { echo "<input type='checkbox' name='email_addresses[]' value='". $getrow['email'] ."' checked='checked'>". $getrow['email'] ."<br>"; } echo "<input type='submit' name='submit' value='Send »'/></form>"; } else { echo 'Add some email addresses.'; } } else { trigger_error(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345714 Share on other sites More sharing options...
mrMarcus Posted May 15, 2012 Share Posted May 15, 2012 Just looked at the PHPMailer class now, and it appears that $mailer->AddAddress() does not accept multiple addresses in one instance; but instead, you must create a new instance of $mailer->AddAddress() which will collect them all and send them to the Send() function. See here: PHPMailer Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345719 Share on other sites More sharing options...
hashstar Posted May 16, 2012 Author Share Posted May 16, 2012 That's great, thanks! Just one problem with it though is this part: $emails = implode(',', $_POST['email_addresses']); It says: Invalid address: info@organicgrowshop.co.uk,mj12@hushmail.comThere was a problem sending this mail! How do i get it so that comma is a line break? Cheers, Hashstar Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345864 Share on other sites More sharing options...
mrMarcus Posted May 16, 2012 Share Posted May 16, 2012 That's great, thanks! Just one problem with it though is this part: $emails = implode(',', $_POST['email_addresses']); It says: Invalid address: info@organicgrowshop.co.uk,mj12@hushmail.comThere was a problem sending this mail! How do i get it so that comma is a line break? Cheers, Hashstar Ya, that's not correct. Have a look at the documentation in the link I provided (which I hadn't before I responded). To send to multiple recipients they must be added one at a time... try the following: <?php // Grab our config settings require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); // Grab the FreakMailer class require_once($_SERVER['DOCUMENT_ROOT'].'/include/MailClass.inc'); $result_message = ''; if (isset($_POST['submit'])) { $errors = array(); if (empty($_POST['email_addresses'])) { $errors['emails'] = 'No emails selected.'; } // here you can/should loop through and verify email addresses to avoid spamming; put a restriction to # of emails allowed to be sent, etc. // santizing/controlling your email functions is greatly overlooked and can cause your site to be dropped by your hosting provider if your site is being used to send gobs of spam if (empty($errors)) { $result_message = 'There was a problem sending this mail!'; // instantiate the class $mailer = new FreakMailer(); // Build foreach ($_POST['email_addresses'] as $email_address) { $mailer->AddBCC($email_address); } // Set the subject $mailer->Subject = 'This is a test'; // Body $mailer->Body = 'This is a test of my mail system!'; if ($mailer->Send()) { $result_message = 'Mail sent!'; } $mailer->ClearAddresses(); $mailer->ClearAttachments(); } } $sql = "SELECT * FROM mailinglist"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { echo (!empty($result_message) ? $result_message .'<br/><br/>' : ''); echo " <h1>Mailing List</h1> Send to<br/><br/> <form action='send.php' method='POST'> "; while ($getrow = mysql_fetch_assoc($result)) { echo "<input type='checkbox' name='email_addresses[]' value='". $getrow['email'] ."' checked='checked'/>". $getrow['email'] ."<br>"; } echo "<input type='submit' name='submit' value='Send »'/></form>"; } else { echo 'Add some email addresses.'; } } else { trigger_error(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345931 Share on other sites More sharing options...
hashstar Posted May 16, 2012 Author Share Posted May 16, 2012 THANK YOU!!! It worked! You are awesome! Quote Link to comment https://forums.phpfreaks.com/topic/262558-problems-using-phpmailer/#findComment-1345947 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.