I’ve went against my mantra (to never code while being tired) and confused myself. Taking $x out of the loop was needed, yeah. Thanks for pointing it out.
My full script will send an email to each addy in my userbase. The script was found online & modified by me. After 1 error gets fixed, another presents itself. Take a look at my full code (minus DB connection info, mail host info, plus I’ve renamed a var or two for clarification):
[code] <?php
$host = “XXX”;
$username = “XXX”; //SMTP username
$password = “XXX”; // SMTP password
require(“class.phpmailer.php”);
require(“mainvars.php”);
$uploaddir = ‘upload’;
$key = 0;
$tmp_name = $_FILES[“userfile”][“tmp_name”][$key];
$name = $_FILES[“userfile”][“name”][$key];
$sendfile = “$uploaddir/$name”;
move_uploaded_file($tmp_name, $sendfile);
//exit;
$url = $_POST[‘url’];
$id = $_POST[‘id’];
$their = $_POST[‘theirname’];
$their = preg_split(‘/ +/’, $their);
$to = $_POST[‘to’];
$addresses = array();
$addresses = explode(“\n”,$to);
//print_r($addresses);exit;
$name = $_POST[‘who’];
$email_subject = $_POST[‘subject’];
$Email_msg = $_POST[‘message’];
$Email_msg2 = str_replace(“\n”, “
”, $Email_msg);;
//$Email_to = “you@yourSite.com”; // the one that recieves the email
$email_from = $_POST[‘from’];
//$dir = “uploads/$filename”;
//chmod(“uploads”,0777);
$attachments = array();
//foreach ($addresses as $Email_to) { echo $Email_to.“
”; }
//exit;
$x=0;
foreach ($addresses as $Email_to)
{
$tn=$their[$x];
$Email_msg2=str_replace(“-name-”,“$tn”,“$Email_msg2”);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $host; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->From = $email_from;
$mail->FromName = $name;
$mail->AddAddress($Email_to);
//$mail->AddReplyTo(“info@worldtradetown.com”,“Information”);
//foreach($attachments as $key => $value) { //loop the Attachments to be added …
//$mail->AddAttachment(”uploads”.”/”.$value);
//}
$mail->AddAttachment($sendfile);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $email_subject;
$mail->Body = $Email_msg2;
$mail->AltBody = $Email_msg;
if(!$mail->Send())
{
echo “Message was not sent
”;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo “Message to $Email_to has been sent
”;
$Email_msg2=str_replace(“$tn”,“-name-”,“$Email_msg2”);
$x++;
}
?>
[/code]
Sometimes, the $tn var is ‘array’ no matter what I do. Others, the script prints off the email text multiple times for each email. If I run this script without so much as trying to call $tn, everything works perfectly. The error in this code, I just cannot see.