Jump to content

Mail script and cronjob


GamerGun

Recommended Posts

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 = "[email protected]";

$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.

Link to comment
https://forums.phpfreaks.com/topic/200521-mail-script-and-cronjob/
Share on other sites

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/

Archived

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

×
×
  • 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.