Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

parallax

Mail/array help

Recommended Posts

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:

[code]<?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>
    <?
}
?>[/code]

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?

Share this post


Link to post
Share on other sites
if you want to make an array values the emails you should do something like this :

[code=php:0]
<?php

$emails = array();

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

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

?>
[/code]

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

[code=php:0]
<?php

echo $emails[0]['Email']

?>
[/code]

so use "for" or "while" loop :)

Share this post


Link to post
Share on other sites
You also need to seperate the emails with a comma instead of a semi-colon.

See: http://us2.php.net/manual/en/function.mail.php

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
if you want the emails store in the variable like 'em@em1.com,em@em2.com' try this :

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

$mails = '';

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

Share this post


Link to post
Share on other sites
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?  ???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Still no mails. This is what the code looks like at the moment:

[code]    $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";
}
[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[code]    $subject = mysql_real_escape_string($_POST['Subject']);
    $message = mysql_real_escape_string($_POST['Message']);[/code]
If you're not going to insert the subject and message into the database, you don't need to use mysql_real_escape_string.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.