Page 1 of 1
Best Method for Post Array
Posted: Wed Dec 17, 2008 6:24 pm
by hoopplaya4
Hey All,
I'm very much enjoying learning and using SwiftMailer. I have one (hopefully basic) question. I'd like to make sure that I'm utilizing SwiftMailer in the best and most efficient manner. I have an Array that is $_POST'd from a form where multiple email addresses can be passed through. This is what I'm currently using as my script, which works fine, by the way:
Code: Select all
<?
echo "Please be patient...<br>";
$html = stripslashes($_POST["bpost"]);
$subj = stripslashes($_POST["subjectpost"]);
$address=$_POST['address'];
$fullname=$_POST['fullname'];
require_once "../lib/Swift.php";
require_once "../lib/Swift/Connection/SMTP.php";
require_once "../lib/Swift/Plugin/Decorator.php";
$swift =& new Swift(new Swift_Connection_SMTP("smtphost.net"));
$names = $_POST['players'];
foreach ($names as $n) {
$bod = $html;
$bod .= "<br>Your email is address is: $n'>";
$message =& new Swift_Message($subj, $bod, "text/html");
$recipients =& new Swift_RecipientList();
$recipients->addTo($n);
if ($swift->send($message, $recipients, new Swift_Address($address, $fullname))) echo "Mail sent to $n!<br>";
else echo "Failed";
}
$swift->disconnect();
?>
Am I utilizing SMTP and Multiple recipients the correct (and perhaps most efficient) way?
Thanks, looking forward to the comments and assistance!
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 6:35 pm
by John Cartwright
I'm not going to touch on the few poor coding practices since this is regaurding SwiftMailer only, but there are a couple things you can improve in that reguard.
In the example below, you will see how to properly use Swift_RecipientList, Swift_Plugin_Decorator, and Swift::batchsend() in conjunction. I would also recommend you take a look at the
throttler plugin, depending on how many emails you are sending out.
Code: Select all
$html = stripslashes($_POST["bpost"]);
$subj = stripslashes($_POST["subjectpost"]);
$address = $_POST['address'];
$fullname = $_POST['fullname'];
require_once "../lib/Swift.php";
require_once "../lib/Swift/Connection/SMTP.php";
require_once "../lib/Swift/Plugin/Decorator.php";
$swift =& new Swift(new Swift_Connection_SMTP("smtphost.net"));
$recipients =& new Swift_RecipientList();
$names = $_POST['players'];
if (is_array($names) && count($names)) {
$recipients->addTo($n);
$replacements[$n] = array('[name]' => $n);
}
$bod = $html;
$bod .= "<br>Your email is address is: [name]'>";
$message =& new Swift_Message($subj, $bod, "text/html");
$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
$swift->batchSend($message, $recipients, new Swift_Address($address, $fullname));
$swift->disconnect();
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 6:47 pm
by hoopplaya4
Thank you for your help Jcart. I will certainly look into those things.
Also, if you wouldn't mind telling me what I can do to improve my coding practices, that'd be great as well! I'm very new to coding and I'm looking to improve my 'game.'
However, if you find in imprudent and off topic, then that's okay.
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 8:07 pm
by John Cartwright
Glad you asked.
The few bad coding practices I see are
1) You do not check the existance of your input variables before using them
Code: Select all
$names = $_POST['players'];
//should be something like
$names = !empty($_POST['players']) ? $_POST['players'] : array();
//or
$names = array();
if (!empty($_POST['players'])) {
$names = $_POST['players'];
}
2) No validation. You did not validate that all the neccesary variables exist, nor did you validate the email address format. A simple google/forum search would reveal a plethora of information on the subject.
3) magic quotes are enabled (I assume tahts why you are using stripslashes)
I can only assume you are applying stripslashes because of magic quotes. This is old an deprecated, and considered poor practice to rely upon it. Another google/search will reveal more than I can explain here.
It is good practice to always raise your error levels during development. You can add error_reporting(E_ALL); to the top of your script to do this. Once done, try submitting your empty form and you'll see there are notices generated (because of the aforementioned)
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 8:14 pm
by John Cartwright
I just noticed I omited the message composition in my original example, which has since been updated (to demonstrate the decorator plugin)
Good luck.
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 9:21 pm
by hoopplaya4
Thanks for the reply jcart. I will certainly take your advice to heart and apply to them to my future coding. In fact, I have already done so.
With the code you provided me, none of the emails were being sent, so I used "error_reporting(E_ALL);" which helped me determine that "$n" needed to be changed to $names, because $n was an undefined variable.
So, I updated the code as such:
Code: Select all
...
$names = $_POST['players'];
if (is_array($names) && count($names)) {
$recipients->addTo($names);
$replacements[$names] = array('[name]' => $names);
}
$bod = $html;
$bod .= "<br>Your email is address is: [name]";
$message =& new Swift_Message($subj, $bod, "text/html");
...
The emails now go through, however, I have a new error: "Warning: Illegal offset type in /home/html/pacificbb2/secure/swift.php on line 20" This is Line 20:
Code: Select all
$replacements[$names] = array('[name]' => $names);
I know that "Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type." But, I'm a little confused as to how this fits my situation?
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 9:40 pm
by John Cartwright
Whoopsie. I forgot to add the loop which builds the recipient list. That would explain why $n was undefined as well. I'm glad you found my advise useful.
The following should work:
Code: Select all
if (is_array($names) && count($names)) {
foreach ($names as $n) {
$recipients->addTo($names);
$replacements[$n] = array('[name]' => $n);
}
}
Re: Best Method for Post Array
Posted: Wed Dec 17, 2008 10:15 pm
by hoopplaya4
That was it! Thanks again, much appreciated for your help. I'm thankful for your willingness to take time out of your day to do some 'teaching.'
Next, I'm going to (attempt to) tackle pulling some fields from MySQL with Swift. Wish me luck!