Splitting data in text box - send emails to each individualy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Mon Feb 08, 2010 6:45 pm

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
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Mon Feb 08, 2010 7:12 pm

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

Syntax: [ Download ] [ Hide ]
$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?
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby JakeJ » Mon Feb 08, 2010 7:28 pm

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.
JakeJ
Forum Regular
 
Posts: 626
Joined: Thu Dec 10, 2009 7:27 pm

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Mon Feb 08, 2010 7:34 pm

with $to1 = $splitdata[0]; i just get the message failed notification now from
Syntax: [ Download ] [ Hide ]
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
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby JakeJ » Mon Feb 08, 2010 7:46 pm

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.
JakeJ
Forum Regular
 
Posts: 626
Joined: Thu Dec 10, 2009 7:27 pm

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Mon Feb 08, 2010 8:01 pm

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"
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby JakeJ » Mon Feb 08, 2010 8:31 pm

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:
Syntax: [ Download ] [ Hide ]
while ( $splitdata = explode('; ', $toemail)) {
echo $splitdata[0],"<br>";
}

What happens?
JakeJ
Forum Regular
 
Posts: 626
Joined: Thu Dec 10, 2009 7:27 pm

Re: Splitting data in text box - send emails to each individualy

Postby a.heresey » Mon Feb 08, 2010 8:39 pm

I think the problem is somewhere else you're not showing us

The following worked fine
Syntax: [ Download ] [ Hide ]
 
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>";
        }
    }
}
 
And silence, and darkness, and the red death held illimitable dominion over all.--Edgar Allen Poe
http://bleedingheartsandartists.net
http://ashleyhill.me
User avatar
a.heresey
Forum Commoner
 
Posts: 59
Joined: Wed Dec 13, 2006 8:31 pm
Location: Chesapeake, VA, US

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Tue Feb 09, 2010 4:50 am

ok my whole code is:
Syntax: [ Download ] [ Hide ]
$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?
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby a.heresey » Tue Feb 09, 2010 10:03 am

Clutching at straws here, but, are you on a Windows server?
http://php.net/manual/en/function.mail.php
And silence, and darkness, and the red death held illimitable dominion over all.--Edgar Allen Poe
http://bleedingheartsandartists.net
http://ashleyhill.me
User avatar
a.heresey
Forum Commoner
 
Posts: 59
Joined: Wed Dec 13, 2006 8:31 pm
Location: Chesapeake, VA, US

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Tue Feb 09, 2010 11:47 am

no i believe it is linux because i have a cgi-bin

also could it be to do with
Syntax: [ Download ] [ Hide ]
# if(isset($_REQUEST['txtTo'])){
being needed? i will try this code and see if it works instead
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby a.heresey » Tue Feb 09, 2010 12:08 pm

what would
Syntax: [ Download ] [ Hide ]
 
echo mail($to1, $subject1, $body1, $headers1);
 

give you?
And silence, and darkness, and the red death held illimitable dominion over all.--Edgar Allen Poe
http://bleedingheartsandartists.net
http://ashleyhill.me
User avatar
a.heresey
Forum Commoner
 
Posts: 59
Joined: Wed Dec 13, 2006 8:31 pm
Location: Chesapeake, VA, US

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Tue Feb 09, 2010 12:31 pm

I dont know about the echo-ing but i got it working with a.hereseys code like this:

Syntax: [ Download ] [ Hide ]
 
 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?

Syntax: [ Download ] [ Hide ]
 
$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
Syntax: [ Download ] [ Hide ]
 
$q = "SELECT DISTINCT email FROM orders";
$rs = $oAppl->query($q);
while ( $rw = $oAppl->row($rs) )
{
    $to1 = $rw["email"];
 


to:

Syntax: [ Download ] [ Hide ]
 
$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
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby cvmflinchy » Tue Feb 09, 2010 4:22 pm

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
cvmflinchy
Forum Newbie
 
Posts: 13
Joined: Sat Feb 06, 2010 1:58 pm

Re: Splitting data in text box - send emails to each individualy

Postby a.heresey » Tue Feb 09, 2010 5:54 pm

you don't need a while and a for each loop.
And silence, and darkness, and the red death held illimitable dominion over all.--Edgar Allen Poe
http://bleedingheartsandartists.net
http://ashleyhill.me
User avatar
a.heresey
Forum Commoner
 
Posts: 59
Joined: Wed Dec 13, 2006 8:31 pm
Location: Chesapeake, VA, US

Next

Return to PHP - Code

Who is online

Users browsing this forum: swan52 and 1 guest