Jump to content


Photo

Mail/array help


  • Please log in to reply
15 replies to this topic

#1 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 06:20 PM

Hi,

I'm pretty new to php, but today I have been trying to create a very simple newsletter-script. So far I have managed to create the subscription/unsubscription feature, which adds/removes the email to/from a database.

I am now stuck on trying to code sending out the newsletter to the recipents. This is what I have got so far:

<?php require_once("db.php"); ?>
<?php
if ($_POST['submit'] == TRUE) {
	    $subject = mysql_real_escape_string($_POST['Subject']);
    $message = mysql_real_escape_string($_POST['Message']);
		$query = "SELECT * FROM newsletter"; 
		$result = mysql_query($query);
 		 while($row = mysql_fetch_array($result)){
		 $recipents = $row['Email']. "; ";
		}
		mail('$recipents', '$subject', '$message');
		echo "Mail sent!";
}
else
{
    ?>
	<h3>Send Newsletter</h3>
    <form method="post" action="<?php echo $PHP_SELF ?>">
    Subject: &nbsp;&nbsp;<input name="Subject" size="44" maxlength="255">
    <br>
    Message: <textarea name="Message" rows="20" cols="40"></textarea>
    <br>
    <input type="submit" name="submit" value="Send Newsletter">
    </form>
    <?
}
?>

Basically I am trying to create an array of the emails from the database, which I have done (if I print $recipents it shows the list of emails) - and then send out one message to all the recipents. When I click on the submit-button, no mail has been sent, but the line "Mail sent!" is still printed. I am not sure if this could be a simple syntax error or a problem with my array or whatever. But the mail function is working on the server (I'm using it in the sub/unsub. features too).

Maybe I am just too tired ::), but can anyone see the problem?

#2 MaaSTaaR

MaaSTaaR
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationKuwait

Posted 20 July 2006 - 06:34 PM

if you want to make an array values the emails you should do something like this :

<?php

$emails = array();

$query = mysql_query('SELECT * FROM table');

while ($r = mysql_fetch_array($query))
{
    $emails[] = $r;
}

?>

then you will have $emails array which store all email , now try to print one of these emails :

<?php

echo $emails[0]['Email']

?>

so use "for" or "while" loop :)
Sorry for my bad English, it's not my mother language and I am still learning it :)

#3 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 06:40 PM

You also need to seperate the emails with a comma instead of a semi-colon.

See: http://us2.php.net/m...nction.mail.php

#4 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 06:56 PM

I see, thanks for the tip. Now I am one step closer to the solution. But how would I go about combining the items of an array, separating them with a ',' and saving them as a variable to use in the mail-statement?

#5 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 07:00 PM

http://us2.php.net/implode

Implode the array. :)

Cheers

#6 MaaSTaaR

MaaSTaaR
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationKuwait

Posted 20 July 2006 - 07:02 PM

if you want the emails store in the variable like 'em@em1.com,em@em2.com' try this :

$query = mysql_query('SELECT * FROM table');

$mails = '';

while ($r = mysql_fetch_array($query))
{
    $mails .= $r['Email'] . ',';
}

Sorry for my bad English, it's not my mother language and I am still learning it :)

#7 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 07:08 PM

Alright, one final question: Doesn't it matter if there is an extra comma at the end? Wouldn't it skip the subject in the mail-statement?  ???

#8 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 07:10 PM

No, an extra comma shouldn't matter.  An easy way to avoid that would be to add your email onto the end outside of the loop if you are really afraid of it breaking.  I'd suggest you give it a try and see what happens though.

#9 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 07:19 PM

Still no mails. This is what the code looks like at the moment:

    $subject = mysql_real_escape_string($_POST['Subject']);
    $message = mysql_real_escape_string($_POST['Message']);
		$query = mysql_query('SELECT * FROM newsletter');
		$mails = '';
		while ($r = mysql_fetch_array($query))
		{
	    $mails .= $r['Email'] . ',';
		}
		mail('$mails', '$subject', '$message');
		echo "Mail was sent:<br>";
		//just to check
		echo "$mails";
}


#10 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 07:21 PM

Did that code echo "$mails" correctly?

#11 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 07:22 PM

Yep, it does in fact print out both my emails, with a comma between and at the end.

#12 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 07:22 PM

Aha! I have it.  Take out the ' ' around your variables.

#13 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 07:26 PM

Yesss, it works now! Thank you again ;)

#14 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 07:34 PM

:) No problem.  Because of this, I know understand the mail function!  Hooray!

#15 parallax

parallax
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 20 July 2006 - 08:26 PM

Do you know why the message of the mail gets outputted like this:

line1\r\nline2\r\nline3\r\n\r\nline5\r\nline6

Instead of actual breaks?

#16 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 08:38 PM

    $subject = mysql_real_escape_string($_POST['Subject']);
    $message = mysql_real_escape_string($_POST['Message']);
If you're not going to insert the subject and message into the database, you don't need to use mysql_real_escape_string.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users