Jump to content

Mail/array help


parallax

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?
Link to comment
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 :)
Link to comment
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]
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.