I need to add a link into the body of the email message which is sent when the user's subscription is finished. This link should then send an email to an address which states that 'users name' wants to renew their subscription. So i need to do a php mail() inside of a php mail(), if you understand me! Also, after they click the link, they should get a thankyou message. The php code for the current 'sendExpiryEmail' function is below. Hope you can help! Thanks.
public function sendExpiryEmail($emails){
foreach($emails as $email){
$name = $email['name'];
$emailaddress = $email['email'];
$expirydate = date('jS F Y',strtotime($email['datetime_expire']));
$subject = "AZ China Report expiry reminder";
$body = '<p><img src="http://az-china.com/images/azchina_logo_email.jpg"></p>
<p>Dear '.$name.',<br /><br />
We hope you have been enjoying your subscription to the Black China Report.<br /><br />
We aim to meet the needs of our readers, by de-mystifying the China market, and by providing accurate, current and pertinent facts and analysis.<br />
We have some exciting new initiatives planned in the coming months.<br /><br />
Your Black China Report subscription will expire on '.$expirydate.'.<br /><br />
<strong>Renewing your subscription is easy.</strong><br /><br />
Simply click here (link to mail()) and we will send you an order form and details on how to pay.<br /><br />
If we can be any further assistance, please do not hesitate to contact us! <br /><br />
Yours sincerely, <br /><br />
Tom Martin<br /><br />
AZ China</p>';
// multiple recipients
$to = $emailaddress;
//$to = 'c23gooey@gmail.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
// Additional headers
$headers .= 'From: AZ China <tom.martin@az-china.com>' . "
";
// Mail it
mail($to, $subject, $body, $headers);
}
}
A way to handle this is to setup a lookup or junction table, where you will store a a uniquely identifying ID that you generate per renewal request with the corresponding user ID.
This table may look something like:
renewals
-------------------------------
renewid | userid
-------------------------------
[output of uniqid()] | bob
[output of uniqid()] | bill
[output of uniqid()] | sarah
...
When you run your script to gather the soon-to-expire list of accounts, add a a bit to the email step that:
while ...
$renewid = uniqid();
$renewurl = "http://.../renew.php?req=$renewid";
// Put that URL in the body of the email.
$renewsql = "
INSERT INTO renewals (
renewid, userid
) VALUES (
'$renewid', '{$email['userid']}'
)
";
if ($db->query($renewsql)) {
mail(...);
}
}
Assuming that $email['userid']
is whatever that user ID field is called, and that you've selected it in the query for $emails
.
Then, with a URL like http://.../renew.php?req=$renewid
, you might do something like this in renew.php
:
<?php
$renew = preg_replace('/^a-z0-9/i', '', $_GET['req']);
if ($renew != '') {
$which = $db->query("
SELECT userid
FROM renewals
WHERE renewid = '$renew'
LIMIT 1
");
if ($which->hasRows()) {
$userid = $which->get(0)->field('userid');
// do whatever renewal stuff with the $userid
$db->query("DELETE FROM renewals WHERE renewid = '$renewid'");
}
}
Another thing you could do is add an expires
column, so that you can periodically clean out old renewal requests. But more or less, this is the general idea.