Page 1 of 2
Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 5:45 pm
by cvmflinchy
I have a 3 text boxes on a form. txtTo, txtSubject and txtBody.
The form also has a submit button. When it is clicked it calls code.php.
The user will specify several email addresses in the text box seperated by semicolons and a space between each.
How can i split these into seperate email addresses and send the email to each one seperately? I do not want each recipient to see everyones email address it is sent to.
Any help is appreciated
Thank you cvmflinchy
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 6:12 pm
by cvmflinchy
i followed a link by JakeJ in my other topic (which went off topic which is why i made this) and im finally getting there. for some reason though i get this error:
error wrote:
Warning: mail() expects parameter 1 to be string, array given in /home/sites/leisure-supplies.co.uk/public_html/admin/newsletter.submit.php on line 30
Newsletter delivery failed... Please try again.
even though this is in my code
Code: Select all
$toemail = $_REQUEST['txtTo'];
while ( $splitdata = explode('; ', $toemail))
{
$to1 = $splitdata;
line 30: if (mail($to1, $subject1, $body1, $headers1)) {
if txtTo only has one email address in and $to1 = $toemail or "my email" then it works but not with this code.
any ideas?
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 6:28 pm
by JakeJ
You're not referencing a specific place in the array. Try $to1 = $splitdata[0];
By omitting [0] you're essentially referencing the entire array, not a specific record within.
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 6:34 pm
by cvmflinchy
with $to1 = $splitdata[0]; i just get the message failed notification now from
Code: Select all
if (mail($to1, $subject1, $body1, $headers1)) {
echo("<p>success message</p>");
} else {
echo("<p>Failed... Please try again.</p>"
im trying it with "my email; my email; my email" in the text box (but with my actual email address)
im feel like im so close
Thank you for your help JakeJ I just hope i can clear this last obstacle
Thanks,
CVMFlinchy
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 6:46 pm
by JakeJ
This part of your code doesn't make sense: echo("<p>Failed... Please try again.</p>"
how about: echo "<p> Failed...Please try again.</p>"; }
Perhaps you're just not showing all of you code, but what you entered, isn't valid php code. The success message doesn't need to be in parenthesis either although it doesn't hurt anything.
Maybe you should try echoing out your array to see if the explode command is actually producing anything useful.
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 7:01 pm
by cvmflinchy
when i echo $splitdata; it comes out as "Array" repeated loads of time but if i echo $splitdata[0]; it shows my email address repeated loads of times. i only put my email in 3 times though.
i changed this for a foreach loop and it displays it 3 times but still wont send the email when i change $to1 to equal the split parts
edit: no i didnt show all my code i just copied and pasted that section to show you where the error message was coming from. i know that bit works because the whole code works if $to1 = "email"
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 7:31 pm
by JakeJ
not that it should make a difference, but instead of using $to1, just use $splitdata[0] as your variable. No need to reassign it.
So if you do:
Code: Select all
while ( $splitdata = explode('; ', $toemail)) {
echo $splitdata[0],"<br>";
}
What happens?
Re: Splitting data in text box - send emails to each individualy
Posted: Mon Feb 08, 2010 7:39 pm
by a.heresey
I think the problem is somewhere else you're not showing us
The following worked fine
Code: Select all
if(isset($_REQUEST['txtTo'])){
$toemail = $_REQUEST['txtTo'];
$subject1="subject";
$body1="body";
$headers="From: me@example.com";
$splitdata = explode('; ', $toemail);
foreach($splitdata as $x){
$to1 = $x;
if (mail($to1, $subject1, $body1, $headers1)) {
echo "<p>success message</p>";
}else{
echo "<p>Failed... Please try again.</p>";
}
}
}
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 3:50 am
by cvmflinchy
ok my whole code is:
Code: Select all
$body = $_REQUEST['txtBody'];
$subject = $_REQUEST['txtSubject'];
$toemail = $_REQUEST['txtTo'];
$splitdata = explode('; ', $toemail);
foreach ( $splitdata as $emailadd)
{
$to1 = $emailadd;
$subject1 = $subject;
$body1 = $body;
$headers1 = "From: my name <name@email.com>";
if (mail($to1, $subject1, $body1, $headers1)) {
echo("<p>Success message.</p>");
} else {
echo("<p>Failed... Please try again.</p>");
}
}
when i comment out everything in between from if (mail ...</p>"); } and put echo $to1 . " " . $subect1 . " " . $body1 it displays the email addres then then subject then the body all seperated by a space then immediately shows the next one. so surely the split is successful. its just the mail function isnt picking it up?
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 9:03 am
by a.heresey
Clutching at straws here, but, are you on a Windows server?
http://php.net/manual/en/function.mail.php
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 10:47 am
by cvmflinchy
no i believe it is linux because i have a cgi-bin
also could it be to do with
being needed? i will try this code and see if it works instead
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 11:08 am
by a.heresey
what would
Code: Select all
echo mail($to1, $subject1, $body1, $headers1);
give you?
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 11:31 am
by cvmflinchy
I dont know about the echo-ing but i got it working with a.hereseys code like this:
Code: Select all
if(isset($_REQUEST['txtTo'])){
$toemail = $_REQUEST['txtTo'];
$subject1= $_REQUEST['txtSubject'];
$body1= $_REQUEST['txtBody'];
$headers1 = "From: name <email>" . "\r\n";
$headers1 .= 'MIME-Version: 1.0' . "\r\n";
$headers1 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$splitdata = explode('; ', $toemail);
foreach($splitdata as $x){
$to1 = $x;
if (mail($to1, $subject1, $body1, $headers1)) {
echo("<p>success</p>");
} else {
echo("<p>failed... Please try again.</p>");
}
}
}
only thing is now that means that my database code must have been wrong. using "while" just repeated the emails being sent. i need to change this to a foreach command to be able to send the emails only once per address.
I am using the following code. How would i ammend the while command because the foreach needs ($something as $somethingelse) doesnt it?
Code: Select all
$q = "SELECT DISTINCT email FROM orders";
$rs = $oAppl->query($q);
while ( $rw = $oAppl->row($rs) )
{
$to1 = $rw["email"];
$subject1 = $_REQUEST['txtSubject'];
$body1 = $_REQUEST['txtBody'];
$headers1 = "From: name <email>" . "\r\n";
$headers1 .= 'MIME-Version: 1.0' . "\r\n";
$headers1 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (mail($to1, $subject1, $body1, $headers1)) {
echo("<p>success</p>");
} else {
echo("<p>"failed... Please try again.</p>");
}
}
NOTE: THIS CODE WORKS AS IT IS. I JUST NEED TO REPLACE THE WHILE COMMAND WITH A FOREACH COMMAND TO STOP IT REPEATING.
Thanks for your help guys its been really useful and helped me loads

i appreciate it
Edit: would i change
Code: Select all
$q = "SELECT DISTINCT email FROM orders";
$rs = $oAppl->query($q);
while ( $rw = $oAppl->row($rs) )
{
$to1 = $rw["email"];
to:
Code: Select all
$q = "SELECT DISTINCT email FROM orders";
$rs = $oAppl->query($q);
$rw = $oAppl->row($rs)
foreach ($rw['email'] as $emailaddress)
{
$to1 = $emailaddress;
Any help is appreciated
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 3:22 pm
by cvmflinchy
i amended my code to:
$q = "SELECT DISTINCT email FROM orders";
$rs = $oAppl->query($q);
while($rw = $oAppl->row($rs)){
foreach($rw as $emailaddress){
$to1 = $rw["email"];
$subject1 = $_REQUEST['txtSubject'];
$body1 = $_REQUEST['txtBody'];
$headers1 = "From: name <email>" . "\r\n";
$headers1 .= 'MIME-Version: 1.0' . "\r\n";
$headers1 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (mail($to1, $subject1, $body1, $headers1)) {
echo("<p>success</p>");
} else {
echo("<p>failed... Please try again.</p>");
}
}
}
but for some reason it is sending it to each person twice
is there any way to stop this?
cheers
Re: Splitting data in text box - send emails to each individualy
Posted: Tue Feb 09, 2010 4:54 pm
by a.heresey
you don't need a while and a for each loop.