Chapter 8: Last-Minute Upgrades
262
Essentially, at the bottom of each e - mail will be a small Unsubscribe link that, when clicked, will drive
traffic back to the welcome/unsubscribe function and pass in that subscriber ’ s ID. That ID needs to be
passed to the removeSubscriber() function of the MSubcribers model in order to delete the subscriber
from the subscribers list.
What this means is that you need to add one more function to the Welcome controller. You ’ ll call it
unsubscribe() , and it will very much mimic the actions taken in the admin/subscribers/delete
controller function, except it won ’ t require a login.
Here ’ s the new function for the Welcome controller:
function unsubscribe($id){
$this- > MSubscribers- > removeSubscriber($id);
$this- > session- > set_flashdata(‘subscribe_msg’,’You have been unsubscribed!’);
redirect(‘welcome/index’,’refresh’);
}
Making Some Last - Minute Upgrades
You now have a working e - mail Newsletter tool, but it could stand a bit of polish. For example, if you
send a test e - mail, you have no choice but to create the e - mail anew before you can send it out again for
real. It would be nice if the system remembered your subject line and HTML content, so you could send
it out to the subscribers.
This is an easy fix. All you have to do is send the user right back to the form if she has sent out a test and
prompt her to send the message again for real if she is ready to go. There are any number of ways to
store the subject line and message, like session, cookie, or database. You have to remember, though, that
using a cookie or session means that you might lose some data (you ’ ll run into size limitations), so the
safest route is to store this information in a database table or log file.
Because you now know how to work with database tables, it might be a good idea to look at how to use
the File helper. The File helper contains a handful of functions that help you read, write, and manipulate
files on the server.
You ’ re going to write the subject and message from the POST data to a log file in the /tmp directory. To
simplify things later on, you ’ ll use some kind of unique delimiter, such as three pipes (|||) or two
colons (::) to separate the two. If you ’ re dealing with a test situation, you ’ re going to bounce the user
back to the form and fill in the form fields with whatever is stored in the log file.
In the following code snippet, the File helper is loaded at the very top. If there is a test situation, write
the subject and message from the POST data to the log file in /tmp and redirect to admin/subscribers/
sendmail. Finally, if you are on the sendmail() function, if the value of the message flash data variable
is set to “ Test email sent, ” do a read of the log file to pass that data into the view.
function sendemail(){
$this- > load- > helper(‘file’);
if ($this- > input- > post(‘subject’)){
$test = $this- > input- > post(‘test’);
$subject = $this- > input- > post(‘subject’);
$msg = $this- > input- > post(‘message’);
if ($test){
$this- > email- > clear();