PHP Email Script- Add unsubscribe link and Tracking

Hello,

I have a PHP email script that works I just can’t figure out adding the unsubscribe link and tracking.

<?php
$formMessage = "";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
if (isset($_POST['cusername']))
{


    // Gather the posted form variables into local PHP variables
    $senderName = $_POST['cusername'];
    $senderEmail = $_POST['cemail'];
    $senderMessage = $_POST['msg'];
    // Make sure certain vars are present or else we do not send email yet
    if (!$senderName || !$senderEmail ) {

         $formMessage = "The form is incomplete, please fill in all fields.";

    } else { // Here is the section in which the email actually gets sent

        // Run any filtering here
        $senderName = strip_tags($senderName);
        $senderName = stripslashes($senderName);
        $senderEmail = strip_tags($senderEmail);
        $senderEmail = stripslashes($senderEmail);
        $senderMessage = strip_tags($senderMessage);
        $senderMessage = stripslashes($senderMessage);
        // End Filtering


        // Begin Email Message Body
      $message = 'THE MESSAGE


';

$message .= "</body></html>";
        // Set headers configurations
	 $to= 'you';
	
     $bcc = "$contacts";
	 $subject = "bret";
	
	$headers = "MIME-Version: 1.0\\r\
";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
	$headers .= "Bcc: $bcc\\r\
";
	$headers .= "From: no-reply@optiontradepit.com\\r\
";
	

	

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






        $formMessage = "Thanks, your message has been sent.";
        $senderName = "";
        $senderEmail = "";
        $senderMessage = "";
    } // close the else condition

} // close if (POST condition
?>

I’m assuming you are storing your addresses in a database… Just store a unique id with it and create a hyperlink from it. When the user clicks it, have it go to a script where it gets the ID from the URL with $_GET

For example, your link might be:
http://domain.com/unsubscribe.php?id=1234

Then delete them from the database. You can track them the same way.

The problem I have is the $contacts is a comma separated list uploaded to the database. Each email does not have a unique variable. How would I create a unique variable for each email.

You could generate an unsubscribe link including the email. In your unsubscribe php code, you can search that email in your data and remove it.

How would I go about doing that? I am newer to php

  • In your unsubcribe.php get your $contacts from the database.
  • Use str_replace to replace ‘unsubscribing-email@hisdomain.com;’ with ‘’
  • Put your $contacts back to your database.

Keep in mind that this is not secure. This will allow anyone who have this unsubscribe link to use it to unsubscribe any e-mail they want.

Also, I would recommend you to change the way you store those emails in the database.

Btw, congratulations on getting multiple top results in google about this topic, with the same question on many forums and sites :rofl:

To what database did you upload the contacts? If you put the information in MySQL, you can also get that information and modify it. If you did put the information in a database, what is the schema for the database?

The csv file should have been separated into several parts that can be retrieved individually.