SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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\n";
        
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        
    $headers .= "Bcc: $bcc\r\n";
        
    $headers .= "From: no-reply@optiontradepit.com\r\n";
        
       
        

        
    mail$to$subject$message$headers );

          
           


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

    // close if (POST condition
    ?>

  2. #2
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Peterborough, Ontario, Canada
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Have I helped you? You could help me.
    Like my business on facebook!
    Text message polls with real-time, live results.

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by husBalthaZar View Post
    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

  6. #6
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    - 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

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bbizzl View Post
    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.
    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.
    Each day is a learning experience.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •