Newsletter Script Help Needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

Newsletter Script Help Needed

Post by bad959fl »

Hey Everyone-

I would like some help on this script I have been putting it together for my newsletter signup page on my site. This current script below works for writing the client's email address and name into a text file (subscribers.txt). However, it still does not send me an email with the contents of several text boxes on the newsletter signup page. How do I:

#1. Receive an email with all of the info from the form (subject, age, city, etc.)

#2. Have the client's email address in the "from" area on the notification (not anonymous@server.com).

#3. Have email verification on signup to make sure they enter a valid address.

The existing code is below:

------------------------

Code: Select all

<?php 
// see if newsletter was checked 
$email = trim(@$_POST['email'] ); 
//@suppresses the warning when email is not set 
if ( isset($_POST['newsletter']) &&!empty($email) ) { 
$filename = 'subscribers.txt'; 
$name_arr = explode( ' ', $_POST['FullName'] ); 
$first_name = $name_arr[0]; 
$file_arr = file($filename); 
$found = false; 
foreach ($file_arr as $value) { 
if (strpos($value, $_POST['email'])!==FALSE ) { 
//this change was necessary: check manual for strpos 
$found = true; 
break; 
} 
} 
if (!$found) { 
$fp = fopen($filename, 'a+');//a+ means "create when it's not present" 
fwrite($fp, $_POST['email'] . ',' . $_POST['FullName'] . "\n"); 
fclose($fp); 
} 
} 
// this redirect is whether newsletter was checked or not 
$URL = "thankyou-newsletter.html"; 
header ("Location: $URL"); 
?>
------------------------

Thanks!

d11wtq | LikeBurrito says... use

Code: Select all

tags please[/color]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

use PHP tags...

take a look at the mail() function.

ex:

Code: Select all

$body = "This is an email body sample with some post data
name: ".$_POST['name']."
email address: ".$_POST['email'];
//etc...
mail("bob@smith.com","my subject",$body);
to put in the link they must use to validate their account. You'll want to just add a hyperlink and use a url param that ties to their record on the db. As soon as they click it, whola, they're authenticated.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

For email verification, use a regular expression
Something similar to this would work quite nicely.

Code: Select all

if($_POST['action'] == "submittingform")
{
  $check = "true";
  if(!eregi("^(.+)@(.+)\\.(.+)$", $_POST['email'])) // Checks for valid email address.
  {
    echo "Your email address does not appear to be valid."; // display error message
    $check = "false";
  }
  // other checks if necessary
}
if($check != "true")
{
 // an invalid email address was entered, so display the form again
}
To make a "FROM" field appear in your email

Code: Select all

$subject = "Subscription to your newsletter";
$email_body = "Bob has just subsribed.  All other info from the form can be written in here.";
$additional_headers = "From: ".$_POST['personsemail']."\r\n";
$additional_headers .="Content-type: text/html; charset=iso-8859-1\r\n";
mail($recipient, $subject, $email_body, $additional_headers);
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

Post by bad959fl »

scrotaye-

I just added the code to make a "FROM" field appear in the email but its still showing up as:

-------------------------
From: Sender Unspecified
-------------------------

I need this to be the person's email address which they filled in the "email" box on the form.

Thanks!
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

Post by bad959fl »

Burrito-

Thanks, it looks like the body is showing up fine. Although, since I have about 20 different forms with over 30 input boxes per form, I would have to enter each listings in the body area.

I tried adding this code to pull any input element from the form but it doesnt seem to be working:

Code: Select all

foreach($HTTP_POST_VARS as $key => $value) { 

$message .= $key . ': ' . $value; 
$message .= "\n"; //Note the double quotes

}
I used a little different PHP script with the above code and it seemed to work fine. Here is the script:

Code: Select all

<?php 
// ************Begin Configure*************** 
//Put where you want the email to go 
$mailto = "me@myemail.com"; 
//Put your subject in here 
$subject = "My subject here."; 
//Put where to redirect to after sending the email 
$redirect = "thankyoupage.html"; 
// ************End Configure**************** 


foreach($HTTP_POST_VARS as $key => $value) { 

$message .= $key . ': ' . $value; 
$message .= "\n"; //Note the double quotes

} 
if (@mail($mailto, $subject, $message)) { 

header("Location: $redirect"); 
} else { 
// This echo's the error message if the email did not send. 
// You could change the text in between the <p> tags. 
echo('<p>Mail could not be sent. Please use your back button to try again.</p>'); 
} 
?>
How do I incorporate this element into my existing script?

Thanks!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I would think you need to set $message before your loop so that you can concat the rest of the stuff to it.

usually just doing the concat will make it throw errors.

try this:

Code: Select all

$message = "";
foreach(...){
  $message .= "..";
}
to incorporate that into your existing stuff, just create your $message var with whatever info you want to start with, then run your loop and concat whatever you want to it. Finally, just dump the var into your mail() function and watch it go :P
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

Post by bad959fl »

Ok, I'm just about there :) I have different scripts that seem to do everything I need but I'm not sure how to combine them to work correctly.

Here is the code to email me the with the client's email address in the "From:" field when I receive the message:

Code: Select all

<?php
$sender = $email;
$target = "me@myemail.com";
$subject = "New subscriber added to the newsletter";
$message = "Enter your message here including desired $variables and formatting from the subscription form such as
subscriber: $subscriber\ne-mail: $email\n and the like.";
$headers = "From: $sender\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
// send the message and give the confirmation back to the submitter
mail ($target, $subject, $message, $headers);
?>
Here is the code to send me all of the form's input items (text boxes, dropdowns, check boxes, etc.):

Code: Select all

<?php 
// ************Begin Configure*************** 
//Put where you want the email to go 
$mailto = "me@myemail.com"; 
//Put where to redirect to after sending the email 
$redirect = "thankyou-newsletter.html"; 
// ************End Configure**************** 

$headers = "From: $sender\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

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

foreach($HTTP_POST_VARS as $key => $value) { 

$message .= $key . ': ' . $value; 
$message .= "\n"; //Note the double quotes

} 
if (@mail($mailto, $subject, $message)) { 

header("Location: $redirect"); 
} else { 
// This echo's the error message if the email did not send. 
// You could change the text in between the <p> tags. 
echo('<p>Mail could not be sent. Please use your back button to try again.</p>'); 
} 
?>
Finally, here is the core of the code which checks for duplicate email addresses and if not found, adds the email to a text file:

Code: Select all

<?php 
// see if newsletter was checked 
$email = trim(@$_POST['email'] ); 
//@suppresses the warning when email is not set 
if ( isset($_POST['newsletter']) && !empty($email) ) { 
$filename = 'subscribers.txt'; 
$name_arr = explode( ' ', $_POST['FullName'] ); 
        $first_name = $name_arr[0]; 
        $file_arr = file($filename); 
        $found = false; 
        foreach ($file_arr as $value) { 
             if (strpos($value, $_POST['email'])!==FALSE ) { 
             //this change was necessary: check manual for strpos 
                 $found = true; 
                break; 
          } 
        } 
        if (!$found) { 
         $fp = fopen($filename, 'a+');//a+ means "create when it's not present" 
         fwrite($fp, $_POST['email'] . ',' . $_POST['FullName'] . "\n"); 
         fclose($fp); 
          } 
 } 
// this redirect is whether newsletter was checked or not 
$URL = "thankyou-newsletter.html"; 
header ("Location: $URL"); 
?>
Any thoughts how I can mix all these to have all elements work. Thanks!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

<?php
if($_POST['action'] == "sendemail")
{
  $redirect = "thankyou-newsletter.html";
  // ######## see if newsletter was checked###### //

  $email = trim(@$_POST['email'] ); //@suppresses the warning when email is not set
  if ( isset($_POST['newsletter']) && !empty($email) )
  {
    $filename = 'subscribers.txt';
    $name_arr = explode( ' ', $_POST['FullName'] );
    $first_name = $name_arr[0];
    $file_arr = file($filename);
    $found = false;
    foreach ($file_arr as $value)
    {
      if (strpos($value, $_POST['email'])!==FALSE )
      { //this change was necessary: check manual for strpos
        $found = true;
        break;
      }
    }
    if (!$found)
    {
      $fp = fopen($filename, 'a+');//a+ means "create when it's not present"
      fwrite($fp, $_POST['email'] . ',' . $_POST['FullName'] . "\n");
      fclose($fp);
    }
  }

  // send email containing form data

  $sender = $email;
  $target = "me@myemail.com";
  $subject = "New subscriber added to the newsletter";
  foreach($HTTP_POST_VARS as $key => $value)
  {
    $message .= $key . ': ' . $value;
    $message .= "\n"; //Note the double quotes
  }
  $headers = "From: $sender\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";// send the message and give the confirmation back to the submitter
  mail ($target, $subject, $message, $headers);
  header("Location: $redirect");
} ELSE
{ ?>
<form action="<? echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="action" value="sendemail">
<? // put the rest of your form here ?>
<input type="submit" value="Submit">
</form>
<? }
?>
I just gave it a quick run though, that should work, although I won't make any guarantees :P

However, you did not include the regex to make sure the email was valid. How come?
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

Post by bad959fl »

scrotaye-

Thanks for the info but it still does not seem to be working. Here are the three areas which are not functional:

1. Sender's email is not in the "From:" header in the email I receive.

2. There is a "Submit" button after submitting the form. I already have a submit button on my existing form, so this is redundant.

3. Contents of the form input (text, dropdowns, etc.) is not included in the body of the email I receive.

Thanks!
bad959fl
Forum Newbie
Posts: 14
Joined: Thu Jun 16, 2005 2:26 pm

Post by bad959fl »

I'm just about finished with this script but I need a little more help. Here is the current code:

Code: Select all

<?php 
// see if newsletter was checked 
$email = trim(@$_POST&#1111;'email'] ); 
//@suppresses the warning when email is not set 
if ( isset($_POST&#1111;'newsletter']) &&!empty($email) ) { 
$filename = 'subscribers.txt'; 
$name_arr = explode( ' ', $_POST&#1111;'FullName'] ); 
$first_name = $name_arr&#1111;0]; 
$file_arr = file($filename); 
$found = false; 
foreach ($file_arr as $value) { 
if (strpos($value, $_POST&#1111;'email'])!==FALSE ) { 
//this change was necessary: check manual for strpos 
$found = true; 
break; 
} 
} 
if (!$found) { 
$fp = fopen($filename, 'a+');//a+ means &quote;create when it's not present&quote; 
fwrite($fp, $_POST&#1111;'email'] . ',' . $_POST&#1111;'FirstName'] . &quote;\n&quote;); 
fclose($fp); 
} 
} 

//send the email to the admin 
$headers = ''; 
$headers .= &quote;MIME-Version: 1.0\n&quote;; 
$headers .= &quote;Content-type: text/html; charset=iso-8859-1\n&quote;; 
$headers .= &quote;X-Priority: 1\n&quote;; 
$headers .= &quote;X-MSMail-Priority: High\n&quote;; 
$headers .= &quote;X-Mailer: PHP\n&quote;; 
$headers .= &quote;From: \&quote;&quote;.$_POST&#1111;'FullName'].&quote;\&quote; <&quote;.$_POST&#1111;'email'] .&quote;>\n&quote;; 
//$headers .= &quote;Bcc: $some_address\n&quote;; 
$to      = &quote;me@myemail.com&quote;; 
$subject = &quote;New user signed up&quote;; 
$message = ''; 
//add elements as desired 
foreach($HTTP_POST_VARS as $key => $value) { 

  $message .= $key . &quote;: &quote; . $value . &quote;\r\n&quote;; //Note the double quotes 

} 

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

// this redirect is whether newsletter was checked or not 
$URL = &quote;thankyou-newsletter.html&quote;; 
header (&quote;Location: $URL&quote;); 
?>
The thing I need help with is when the form field results come back to me via email, they consist of 1 long line of wrapped text. How do do I add a break on each line (form field) so its easier to read? Here is an example:


Currently:
-------------------------
FirstName: ken email: ken@yahoo.com Referred: yahoo Searching: workouts subscribe_x: 25 subscribe_y: 10
-------------------------


I would like the results to look like this in the email:
-------------------------
FirstName: ken

email: ken@yahoo.com

Referred: yahoo

Searching: workouts

subscribe_x: 25 subscribe_y: 10
-------------------------


Thanks!
Post Reply