GamerGun Posted May 3, 2010 Share Posted May 3, 2010 Hey all, I have created the following script: mailer.php <?php include '../header.php'; ?> <div id="content"> <div id="contentleft"> <h2>Nieuwsbrieven versturen</h2> <?php $inum1 = rand(0,255); $inum2 = rand(0,255); $inum3 = rand(0,255); $inum4 = rand(0,255); $ip = $inum1.'.'.$inum2.'.'.$inum3.'.'.$inum4; $sql = mysql_query('SELECT * FROM `nieuwsbrief` WHERE `bevestigd` = 1') or die(mysql_error()); $num_rows = mysql_num_rows($sql)or die(mysql_error()); while ($row = mysql_fetch_array($sql)) { $slp = $_POST["speed"]; sleep($slp); $beste = $row["naam"]; $to = $row["email"]; $week = date('W'); $subject = "Nieuwsbrief www.watmijoverkwam.nl week $week"; $sender = "admin@watmijoverkwam.nl"; $headers .= "From: $sender\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=utf-8\r\n"; $headers .= "Reply-To: $sender"; $dezepagina = "<a href=\"http://watmijoverkwam.nl/afmelden.html\">afmeldpagina</a>."; $url = "<a href=\"http://watmijoverkwam.nl/\">http://watmijoverkwam.nl/</a>"; $msg = $_POST["message"]; $var1 = "Beste $beste,<br /><br />Hierbij sturen we je de nieuwste verhalen van afgelopen week.<br /><br />"; $var2 = $msg; $var3 = "Je ontvangt deze nieuwsbrief omdat je bent aangemeld op $url.<br />Wil je deze nieuwsbrief niet meer ontvangen?<br />Meldt je dan af op de $dezepagina"; $var4 = "<br /><br />Met vriendelijke groeten,<br />$url"; $fullmsg = $var1.$var2.$var3.$var4; mail($to,$subject,$fullmsg,$headers); echo "Nieuwsbrief verzonden aan: " . $row["naam"] . " - " . $row["email"] . "<br />\n"; } ?> <br /> </div><!-- Closing contentleft --> <?php include '../menu.php'; ?> <?php include '../footer.php'; ?> </div> </body> </html> send.php <?php $corkey1 = "X"; $corkey2 = "Y"; $control1 = $_GET["key1"]; $control2 = $_GET["key2"]; if ( $control1 == $corkey1 && $control2 == $corkey2 ) { echo ""; } else { echo "Ga weg!"; die; } ?> <?php include '../header.php'; ?> <div id="content"> <div id="contentleft"> <h2>Nieuwsbrief opstellen</h2> <form id="mform" name="mform" method="post" action="mailer.php"> Speed<br> <select name="speed"> <option value="1">Insane</option> <option value="4">Fast</option> <option value="8" selected="selected">Normal</option> <option value="12">Slow</option> </select> <br> Message:<br> <textarea name="message" rows="25" cols="75" wrap="off"> <?php $result = mysql_query("SELECT * FROM berichten WHERE TO_DAYS(NOW())-TO_DAYS(datum) <=7 ORDER BY id DESC") or die(mysql_error()); function neat_trim($str, $n, $delim='...') { $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } while($row = mysql_fetch_array($result)){ $id = $row['id']; $message = $row['bericht']; $onderwerp = $row['onderwerp']; $titel = $row['titel']; $titel2 = eregi_replace(' ', '-', $titel); $titel2 = strtolower($titel2); $titellink = "<a href=\"http://watmijoverkwam.nl/$titel2-$id.html\">$titel</a>"; $verderlezen = "<a href=\"http://watmijoverkwam.nl/$titel2-$id.html\">Lees verder</a>"; echo $titellink; echo " ("; echo $onderwerp; echo ")"; echo "\n<br />"; echo neat_trim($message, 120); echo "\n<br />"; echo $verderlezen; echo "\n\n<br /><br />"; } ?> </textarea> <br> <input type="submit" name="start sending" value="Start Sending" > </form> <br /> </div><!-- Closing contentleft --> <?php include '../menu.php'; ?> <?php include '../footer.php'; ?> </div> <script language="JavaScript">document.mform.submit();</script> </body> </html> Send.php contains a form and such, from which the mail will send (default values from the form, the rest will get via the mailer.php). As you can see i did set 2 keys in send.php, which need to be filled in (like send.php?key1=X&key2=Y) in order for the script to work. This works all fine. As for the cronjob i enter: /usr/local/bin/php -q /home/mqxfaokl/domains/watmijoverkwam.nl/public_html/mail/send.php?key1=X&key2=Y Though, via the cronjob it does not work. Perhaps because the script needs some time to process the e-mails. How can i make sure that the cronjob does not 'kill' the script right along but waits 'till it is done loading the mailer.php file? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
GamerGun Posted May 11, 2010 Author Share Posted May 11, 2010 Anyone please? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted May 11, 2010 Share Posted May 11, 2010 You seem to be missing the part where you tell cron WHEN to do the job. 1 0 * * * // 1 minute after midnight 1 0 * * * /usr/local/bin/php -q /home/mqxfaokl/domains/watmijoverkwam.nl/public_html/mail/send.php?key1=X&key2=Y HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
GamerGun Posted May 11, 2010 Author Share Posted May 11, 2010 Haha well i do have that; 0 10 * * 6 I assume this is correct for 00:10 saturday? Quote Link to comment Share on other sites More sharing options...
trq Posted May 11, 2010 Share Posted May 11, 2010 The $_GET array does not exist within cli scripts. Try passing your options as arguments then looking for them within the $argv array. See cli for more details. Quote Link to comment Share on other sites More sharing options...
trq Posted May 11, 2010 Share Posted May 11, 2010 And while where on the subject.... why does the script your planning on executing via cron have html in it? Nobody will see its output. Quote Link to comment Share on other sites More sharing options...
GamerGun Posted May 11, 2010 Author Share Posted May 11, 2010 There might be a time that i want to mail something different or so, which i then can do via the browser. Anyhow, thanks for your reply. Now is see why it's not working. But i just don't get the $argv thing, what should go in there? http://mardix.wordpress.com/2009/03/07/how-to-pass-variables-to-php-cli-shell-script/ Quote Link to comment Share on other sites More sharing options...
trq Posted May 11, 2010 Share Posted May 11, 2010 For example.... /usr/bin/php yourscript.php foo bar yourscript.php <?php print_r($argv); Would result in something like. Array( [0] => yourscript.php [1] => foo [2] => bar ) Quote Link to comment Share on other sites More sharing options...
GamerGun Posted May 11, 2010 Author Share Posted May 11, 2010 And the contents of foo and bar are for example the addresses and subject or am i seeing it wrong? Quote Link to comment Share on other sites More sharing options...
trq Posted May 11, 2010 Share Posted May 11, 2010 They are argument passed to the script. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.