www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 06-18-2004, 03:45 PM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    Send Email To List

    I run a non-profit web site for our local environmental managers group. I send out a newsletter once a month, however sometimes other needs to send it out. I need a PHP script where we can go to the web site, type in the message and it will send to the entire group. So it would need to hold a list of email addresses. I am not looking for anything fancy, but being a non-profit organization, I would like to find something free.

    Any suggestions?

    Thanks,
    Jim
    Reply With Quote
      #2  
    Old 06-19-2004, 05:12 PM
    zachzach zachzach is offline
    Registered User
     
    Join Date: Feb 2003
    Location: Rhode Island, United States of America
    Posts: 460
    Ok, here we go. Create a file called emailgroup.php
    I will not be making a login system for you...you'll have to figure that out yourself, or ask me again. Here's the code for email.php:

    PHP Code:
    <?PHP
    if(isset($_POST['mode'])) {
    $email_addresses = array("email_to@this.person","email_to@this.person","email_to@this.person","email_to@this.person","email_to@this.person","email_to@this.person","email_to@this.person","email_to@this.person");
    foreach(
    $email_addresses as $email_address) {
    mail($email_address, $subject, $message, "From: [email]your@email.com[/email]");
    }
    exit;
    }
    ?>
    <form action="/email.php" method="POST"><p>
    <label for="subject">Subject:</label><br /><input type="text" name="subject" id="subject"></p><p>
    <label for="message">Message:</label><br /><textarea id="message" name="message"></textarea></p><p>
    <input type="submit" name="mode" value="Email Group"><br />
    <input type="reset" value="Clear">
    </p></form>
    Hope that helps
    Reply With Quote
      #3  
    Old 06-19-2004, 06:12 PM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,093
    zachzach:

    That's ok if you want to get denied from pumping email addresses messages. You probably want to pause sending after a certain number. Also, it would be better for the user if the email addresses were in a separate file.

    Like this:

    emails.txt

    Then create a PHP file:

    Code:
    <?php
    
    $message="some message";
    $emails="emails.txt";
    $from="youremail@mail.com";
    $subject="some subject";
    $counter=0;
    
    $fp=fopen($emails,"r") or die("Cannot open file.");
    
    while(!feof($fp)){
         $email=trim(fgets($fp,4096));
    
          if($counter%10==1){
            sleep(2);
          }
          mail($email,$subject,$message,"From:$from");
         $counter++;
    }
    
    fclose($fp);
    
    ?>
    __________________
    User is a four letter word!
    Reply With Quote
      #4  
    Old 06-19-2004, 06:18 PM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    I added my groups email addresses and changed the from emial address, then tested it. I got "error 404: File not found". Upon looking at the code further, I noticed it references <form action="/email.php" method="POST">. I don't have the file "email.php". Can you tell me where to get it?

    Other than this, I believe this is exactly what I am looking for. Very simple.

    Thanks,
    Jim
    Reply With Quote
      #5  
    Old 06-19-2004, 07:21 PM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,093
    Take the / out in from of the form action. And make sure the file you created with the code is named "email.php"
    __________________
    User is a four letter word!
    Reply With Quote
      #6  
    Old 06-19-2004, 07:29 PM
    zachzach zachzach is offline
    Registered User
     
    Join Date: Feb 2003
    Location: Rhode Island, United States of America
    Posts: 460
    other person whgo corrected my code(lol):
    It was just a quicky code. I didn't spend much time on it, I was just trying to show the person a extreamly simplistic way of sending them
    Reply With Quote
      #7  
    Old 06-19-2004, 08:23 PM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    This worked great! Thanks.

    If it's not asking too much, one other thing I would like to add is that when I click the send button, after it's done it's work sending the emails, I would like for it to take me to a particular web page. Can you show me how to modify the script to do that?

    Thanks again for the help.

    Jim
    Reply With Quote
      #8  
    Old 06-19-2004, 08:47 PM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    crh3675 - I like your ideas, so I tested it that way. Only problem is that each email address gets the note twice and it only shows the $Message. I wanted this to be a form, for our group organizations's officers to send out news to the whole chapter. So I was assuming your mod would work this way. Below is my code. See if I screwed something up. Also do I need to set the $counter to the number of emails to sleep on or have you preset that to 10 below?

    Thanks,
    Jim

    code

    <?php

    $message="QCACHMM News";
    $emails="emails.txt";
    $from="info@queencitychmm.org";
    $subject="QCACHMM News";
    $counter=0;

    $fp=fopen($emails,"r") or die("Cannot open file.");

    while(!feof($fp)){
    $email=trim(fgets($fp,4096));

    if($counter%10==1){
    sleep(2);
    }
    mail($email,$subject,$message,"From:$from");
    $counter++;
    }

    fclose($fp);

    ?>
    <form action="groupemail.php" method="POST"><p>
    <label for="subject">Subject:</label><br /><input type="text" size="50" name="subject" id="subject"></p><p>
    <label for="message">Message:</label><br /><textarea rows="15" cols="75 id="message" name="message"></textarea></p><p>
    <br>
    <input type="submit" name="mode" value="Send Group Email"><br />

    </p></form>
    Reply With Quote
      #9  
    Old 06-20-2004, 10:20 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,093
    Here is the updated code:

    Code:
    <?php
    
    if(isset($_POST["mode"]) || isset($mode)){
    
            $redirect="http://yoursite.com/";
    	$body=isset($_POST["message"]) ? $_POST["message"] : $message;
    	$emails="emails.txt";
    	$from="info@queencitychmm.org";
    	$emailsubject=isset($_POST["subject"]) ? $_POST["subject"] : $subject;
    	$counter=0;
    
    	$fp=fopen($emails,"r") or die("Cannot open file.");
    
    	while(!feof($fp)){
    		$email=trim(fgets($fp,4096));
    
    		if($email!=""){
    
    			if($counter%10==0){
    				sleep(2);
    			}
    			mail($email,$mailsubject,$body,"From:".$from);
    			echo "Email sent to : $email<br>";
    
    			$counter++;
    		}
    	}
    
    	fclose($fp);
            header("Location: $redirect");
    
    }
    ?>
    <form action="emails.php" method="POST"><p>
    <label for="subject">Subject:</label><br /><input type="text" size="50" name="subject" id="subject"></p><p>
    <label for="message">Message:</label><br /><textarea rows="15" cols="75 id="message" name="message"></textarea></p><p>
    <br>
    <input type="submit" name="mode" value="Send Group Email"><br />
    
    </p></form>
    You can change the 10 in $counter%10 to any number that you want. If you hvae a list of 100 emails you probably want to do 25 at a time so it would be $counter%25
    __________________
    User is a four letter word!

    Last edited by crh3675; 06-20-2004 at 10:27 AM.
    Reply With Quote
      #10  
    Old 06-20-2004, 03:18 PM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    crh3675 - This works great but there is one problem. Each email address in the "emails.txt" receives the form results twice. That is they receive two email notes, not one with the text twice. Just a little more help and it will be perfect!

    Thanks again for all your help.

    Jim
    Reply With Quote
      #11  
    Old 06-20-2004, 03:29 PM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,093
    Not sure why you would get 2 emails. I tested the script with no problem. I can look into it more. One thing I noticed was a misspelled variable:

    mail($email,$mailsubject,$body,"From:".$from);


    $mailsubject should be $emailsubject
    __________________
    User is a four letter word!
    Reply With Quote
      #12  
    Old 06-20-2004, 04:11 PM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    crh3675 - I made the typo change and that fix the problem. I am now only getting one email for each address. Go figure.

    However there is a little bug, which I can live with unless there is a quick fix. When I send the message from the form, it gives me the email addresses it sent the mail to at the top. That great, I like that confirmation. However, if I immediately send out another message (with the emails still listed at the top), it sends the old message again as well as the new message. It's like it holds the old message in cache. Clicking "refresh" does not reset the form (i.e. no sent emails at the top). I have to totally get off the page, then get back on to reset it. Now like I said, that is minor and something I can live with. I rarely send two messages back to back.

    Thank you again for all your help.

    Jim
    Reply With Quote
      #13  
    Old 06-20-2004, 11:08 PM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,093
    Clicking the "refresh" button will re-post the form data and in fact submit to the list again. When you go back and reload the form page using the browser "reload" button, you are re-posting the data. What you need to do is NOT refresh the page but to just go to the URL in the browser. This holds true with all form posting on internet websites. You can do some workarounds using session variables to prevent multiple submissions but that would just complicate things. By the way, you can call me Craig. Glad I could help.
    __________________
    User is a four letter word!
    Reply With Quote
      #14  
    Old 06-21-2004, 11:58 AM
    jereece jereece is offline
    Registered User
     
    Join Date: Jul 2003
    Posts: 25
    Thanks Craig. This makes sense. I really apprciate the help. I could not have done it without you. As I build our web site I am thinking of more things I want to do, but I know so little about PHP. It's great to have you guys as a resource.

    Thanks again,
    Jim
    Reply With Quote
      #15  
    Old 06-22-2004, 08:20 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,093
    Feel free to email me at webnerd at comcast dot net if you need any other help
    __________________
    User is a four letter word!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 10:12 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.