Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newsletter Script Help Needed

Status
Not open for further replies.

bad959fl

Technical User
Jun 16, 2005
11
US
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:

------------------------
<?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!
 
To send a message with the form information do something like this script:

Code:
$sender = $submitted_subscriber_email_address;
$target = "your_email@yourdomain.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);
$echo "Your registration information has been submitted.  A confirmation e-mail will be sent to the provided address shortly.  Please follow the confirmation link to activate your new account<br>";

This will allow you send html or text email.

If you would like to confirm that the e-mail is valid. Use the same type script to send a confirmation message to the user. Also have a flag in your user database or file that is initially off and can be turned on later. Assign each user a unique id upon creation of hte account. Have the e-mail contain a link to your site such as
Code:
<a href="[URL unfurl="true"]http://www.yourdomain.com/confirmation?id=123456">Confirm[/URL] your Subscription</a>
Now, when that page is visited, you activate the account associated with user 123456.

Hope this helps.
 
flubbard-

Thanks, I added your script but still receive this in the "From:" field when I receive the email:

---------------------
Sender Unspecified
---------------------

How do I have their emal address displayed, so when I reply to the email it will go directly to them?

Thanks!
 
Is the sender showing up in either place (either as the sender e-mail address or in the From: field in the message header?) The sender email should be filled from the newsletter form into the variable $sender, also verify that the header information in the message that you receive appears correct. Also, I would check to make sure that the variable that you have used to store the subscribers e-mail is being passed correctly to the e-mailing script.

Let me know if any of this helps.
 
flubbard-

The sender's email address is showing up in the body:

-------------------------------
email address: email@email.com
-------------------------------

But I need it to actually show up in the "From:" of the email address, so I can simply reply to the email and have it go to the person. When I get the email, it will show as either:

anonymous@myserver.com
or
Sender Unspecified


I have been researching this over the last few days and it seems to be tough to find any details on how to do this.

Thanks!
 

Based on what I see from the code, and the replies... how about you make this minor change to the very first line of your code that was posted -- and see if that does what you want:
Code:
$sender = $email;
If it does what you want... then you might want to just replace all instances of $sender with $email.

Good luck [smile]
Jeff
 
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:
<?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:
<?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:
<?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!
 
I'm just about finished with this script but I need a little more help. Here is the current code:

Code:
<?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['FirstName'] . "\n"); 
fclose($fp); 
} 
} 

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

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

} 

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

// this redirect is whether newsletter was checked or not 
$URL = "thankyou-newsletter.html"; 
header ("Location: $URL"); 
?>

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!
 
what is the purpose of the \r, does it work the same with only the \n at the end of the line?
 
flubbard-

I just removed the "\r" and it has the same results. Not sure exactly what this is for since another forum user helped with with this script.

 
Since your email is of content type html, you should use <br /> instead of \r\n for your line breaks.
 
Vragabond-

Works nicely. Thanks!

My last issue with this form is make sure the client enters each form field. Since I have over 20 forms with different numbers of fields for each one, whats the best way to implement this so if they dont fill out a field, they will receive an error message?

 
Submit form, check for empty values, repopulate the form with the values filled and put a notice at the top and next to each item that needs to be filled. Repeat this until all fields are filled out, when you actually move to the part of the script that does processing.
 
I have no idea how to do this? Can you provide some sample code?
 
I also want to place the "subscribers.txt" file in a secured area (cgi-bin) to protect the email addresses. I moved this file over and updated the code:

Code:
$filename = '/home/httpd/vhosts/domain.com/cgi-bin/subscribers.txt';

However, it errors out now. Is there a way to make this work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top