Jump to content

Sending only one email rather than multiple from my script


Dicko_md

Recommended Posts

Hi

 

I have the script below that runs on a cron job every 1 minute. I have 2 scripts that sends email reminders. The other script sends a reminder email and a email on the time requested.

 

This script I would like the email to be sent as soon as the event has been logged. Only problem is that it keeps sending the email  every minute till the event.

 

Is there anyway to only send one email to the user from this script.

 

If a marker needs to be put down, then can you explain how to as im guessing this will need to be done in the MySQL database. ?

 

I have played round with the $data line and created the $event_logtime to see if that would do it..... but it didn't.

<?php

include('elements.php');

$curtime = time();
echo "Cron Started\nServer Time: ".date("F j, Y, g:i a", $curtime)."\n";

$event_logtime = ($curtime - 60);

$data = db_select('ar_notify', '*', $event_logtime);
while ($info = mysql_fetch_assoc($data)) SendReminder($info, false);

echo 'Cron Completed!';




function SendReminder($info, $isadvanced) {
	$sitename = GetConfig('sitename');
	$siteurl = GetConfig('siteurl');
	$comment = stripslashes($info['shcomment']);
	$stub = db_select('ar_members', 'id', "id=$info[userid]");
	if (!mysql_num_rows($stub)) {
		db_delete('ar_notify', "id=$info[id]");
		return;
	}
	$tzoffset = GetUserOffset($info['userid']);
	$formdate = time()-$tzoffset;
	$formdate = date("l jS F Y H:i", $formdate);
	$timecreated = date("l jS F Y H:i:s", $info['timecreated']-$tzoffset);
	if ($info['recurring']) $addcomment = 
			"\n\nYou have set up a CONTINUOUSLY recurring AutoReminder event.\n".
			" You can disable this ".
			"or find your password by going to $siteurl.";
	else $addcomment =
			"\n\nYou have set up a ONE-TIME non-recurring AutoReminder event and you will next be reminded on the event date.\n".
			"It was created on $timecreated by a web-browser at $sitename Internet site .".
			"You can disable this ".
			"or find your password by going to $siteurl.";
	if ($isadvanced) {
		switch ($info['notify_val']) {
			case 1:	 $info['notify_val'] = 'minute(s)'; break;
			case 2:	 $info['notify_val'] = 'hour(s)'; break;
			case 3:	 $info['notify_val'] = 'day(s)'; break;
			case 4:	 $info['notify_val'] = 'month(s)'; break;
			case 5:	 $info['notify_val'] = 'year(s)'; break;
		}
//Reminder Advance Email
$commenttype .= $addcomment."\n";
$smallcomment .= "\n".GetConfig('defaultmsg'); 
$commentmessage .= $info['shcomment'];
$commentreminder .= "Initial Reminder";
$to = $info['email'];

$subject = "Reminder for " .$info[subject];

$headers = "From: XXXXXXXX.com <reminder@XXXXXXXX.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= '<html><Head></Head><body>';
$message .= '<img src="http://www.XXXXXXXX.com/images/XXXXX_logo_paypal.png" alt="Logo" />';
$message .= '<table border=1px rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Event Date:</strong> </td><td>" .$formdate. "</td></tr>";
$message .= "<tr><td><strong>Event Message:</strong></td><td>" .$commentmessage. "</td></tr>";
$message .= "<tr><td><strong>Type of Message:</strong></td><td style='background: #66CD00;'>" .$commentreminder. "</div></td></tr>";
$message .= "<tr><td colspan='2' style='font-size: xx-small'>" .$commenttype. "</td></tr>";
$message .= "<tr><td colspan='2' style='font-size: xx-small'>" .$smallcomment. "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";

mail($to, $subject, $message, $headers);

	}
	else {

//Reminder Email
$commenttype .= $addcomment."\n";
$smallcomment .= "\n".GetConfig('defaultmsg'); 
$commentmessage .= $info['shcomment'];
$commentreminder .= "One Time Reminder";
$to = $info['email'];

$subject = "Reminder for " .$info[subject];

$headers = "From: XXXXXX.com <reminder@XXXXXXXXX.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message .= '<html><Head></Head><body>';
$message .= '<img src="http://www.XXXXXXXXX.com/images/XXXXX_logo_paypal.png" alt="Logo" />';
$message .= '<table border=1px rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Event Date:</strong> </td><td>" .$formdate. "</td></tr>";
$message .= "<tr><td><strong>Event Message:</strong> </td><td>" .$commentmessage. "</td></tr>";
$message .= "<tr><td><strong>Type of Message:</strong></td><td style='background: #66CD00;'>" .$commentreminder. "</div></td></tr>";
$message .= "<tr><td colspan='2' style='font-size: xx-small'>" .$commenttype. "</td></tr>";
$message .= "<tr><td colspan='2' style='font-size: xx-small'>" .$smallcomment. "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";

mail($to, $subject, $message, $headers);
	}

	echo "Sent mail to: $info[email]<br />";}
?>

Any more info, then just ask and I will try and provide

Link to comment
Share on other sites

Yes, you just need to store that you sent the email to disk / DB.

 

BY using a simple SELECT and INSERT statement you can do this. On an App I made for mailing I used something like the following (the code is typed off my mind so it's not right):

<?php

if (flock()) { //check out the documentation on how flock works

$result = mysql_query("SELECT * FROM sent WHERE email = $email AND event = $event");

if (!mysql_fetch_array($result)) {
sendemail();
mysql_query("INSERT INTO sent VALUES ($email, $event)");
} else {
//Already sent
}

}

Hope it helped ^^;

Link to comment
Share on other sites

You don't even need an extra table for this, nor the INSERT statement. Just add a column to the

ar_notify
table, and update it to show the status of the mails. If you make it an enum, it should be quite easy and clear enough what the state is.

All that you're missing then, is adding a condition to the original query. To only fetch e-mail addresses which haven't had their reminder e-mails sent yet.

Link to comment
Share on other sites

Figuring out how to write the code is your job. We're here to offer you help and advice to overcome problems, and figure out alternative/better solutions, not to do the work for you.

Try for yourself, and read up if you don't know how. I've given you all of the tools you need to figure this one out on your own, and with a bit of reading + searching on the net this should be quite easy. If you get stuck, post your code and explain where and why. Then we'll be able to help you some more. ;)

Link to comment
Share on other sites

First Question.

 

adding another column to the table and I chose ENUM. it says that it must have a figure in it of some kind. I will be choosing Yes or No as the column will be called email_sent.

 

What I would like to know is. Will old data and new be OK as I don't fill this column in on any other page?

 

The column will only be filled in/updated will be a separate cron job that updates the column when the event is created ?

 

Thanks

 

Martyn

Link to comment
Share on other sites

does this look right ?

 

$data = db_select('ar_notify', 'email_sent="0"');
while ($info = mysql_fetch_assoc($data)) SendReminder($info, false);

 

If I change db_select to db_update and email_sent="1" it updates email_sent to 1 when I run the script but the above does not select any email_sent  even though they have a Zero in them ?

 

Thanks

 

Martyn

Link to comment
Share on other sites

Ive got the script to work but I cant get the $formdate to convert the timestamp to the actual time. 

 

Ive tried

$dateTimeEnd = ($info['date_field']);
        $datestampconvert = strtotime($dateTimeEnd);

but when I try to put this in an email its blank.

 

Any other way to convert timestamp from database and show it on a webpage/email ?

 

Thanks martyn

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.