[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

Unsubscribe Email

Many web sites have an "unsubscribe@abc.com" as a way for people to "opt out" of being sent emails.

I want to set that up on a site & "intercept" with a php script ant emails sent to "unsubscribe" & manage them with some programming logic.

How can I do that?
0
Richard Korts
Asked:
Richard Korts
  • 2
2 Solutions
 
wwwdeveloper2Commented:
In the e-mail that is sent out include a link to your scripted page with a query string containing the e-mail address to be unsubscribed.

Such as yoursite.com/unsubscribe.php?email=somebody@e-mail.com

In the unsubscribe.php file use a get to grab the email address from the query string.

$_GET['email'];

Run your SQL to update your database to either remove the e-mail account from your e-mail list or flag it as no longer wanting to receive e-mails.
0
 
wwwdeveloper2Commented:
What server are you running your php on?  Here is a good tutorial to achieve what you want to do:

http://thedrupalblog.com/configuring-server-parse-email-php-script

Basically you need to parse the e-mail received to grab the sending e-mail address.....then do your php logic.

0
 
Ray PaseurCommented:
The thing you're describing is called an email pipe script.  It is started automatically when emails sent to the pipe address are received.

Here is a teaching example of how I have implemented the email pipe in the past.  There is a little work to do because the pipe script might not be in your web root directory tree (to prevent accidental triggering).  If you put it outside of the WWW root, you will want to look carefully at the variables in phpinfo() to be sure that your access to files and data bases is correctly structured.

In this teaching example, the moving parts would start at line 55.  You would parse the $raw_email variable, extract the "from" address and the unsubscribe signal string, (make sure they match so you do not have a security exposure) and update the data base.  A nice touch might be to respond with an automated email that says, "You have been unsubscribed."

If you get an unsubscribe notice that does not contain a valid email address or that does not contain a matching signal string, do not respond to it.  Discard it silently.  A response to a bogus unsubscribe notice is a sure way to get your site branded as a spammer!
#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = '';


// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now