Automatic Emailer code - send email to list of email address in text file - CODE

Discussion in 'PHP' started by le007, Mar 12, 2012.

  1. #1
    Hi all,

    Found this code online but can't activate it. I'm the head of a Tennis Club and I want to send the same email out to everyone, sort of a mail-shot but will need to edit it every so often. How can I "call" this code to work. I have a list of email addresses in a file called contacts.txt.

    Can someone help please?

    <?php
    
    if (!isset($_POST['frubmit'])){
    echo <<<HERE
    <form enctype='multipart/form-data' method='post' action='email.php'>
    <input type="submit" name="frubmit" value="GO! Start Emailing!" style="cursor: pointer; margin-left: 5px; margin-top: 1px; background: #20226C; color: white; 
    
    width: 200px; height: 18px; font-size: 14px; border :0px;" />
    </form>
    HERE;
    }
    
    // if it has been submitted then do this
    if (isset($_POST['frubmit'])){
    
    $headers .= "From: <Info@mywebsite.com>\n";  // your email client will show the person's email address like normal
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n"; // sets the mime type
    $subject = "FAO Principal"; // this is the subject of the email
    
    $myFile="contacts.txt";
    
    $msg = "Dear Sir/Madam,
    
    Our timetable for this years Tennis season will be advertised on our website soon.
    \n
    http://www.mywebsite.com
    \n
    
    Many thanks,
    Leo";
    
    
    
    $msg =  wordwrap( $msg, 1024 );
    
    $fh = fopen($myFile, 'r') or die("Couldn't open file");
    
    // set number of contacts by reading number of lines in file into a variable
    $numberoftypes = count(file($myFile));
    
    //get the data into a large string
    $data = fread($fh, filesize($myFile));
    fclose($fh);
    
    $array1 = explode("\r\n", $data);
    
    echo "<A href='email.php'>START AGAIN</a><BR><BR>";
    
    
    for ( $botar = 0; $botar <= $numberoftypes - 1; $botar += 1 ) {
    
    $recipient = $array1[$botar];
    
    echo "Mailing $recipient &nbsp;&nbsp;&nbsp; - ";
    $j++;
    if ($j==4){echo "<BR>\n";$j=0;}
    
    mail($recipient, stripslashes($subject), stripslashes($msg), $headers); // the mail() function sends the message
    
    }
    
    }
    ?>
    
    PHP:

     
    le007, Mar 12, 2012 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I think your problems are in the file handling right? Have you done echo's to see where you come unstuck?

    I'd recommend http://nz.php.net/manual/en/function.file-get-contents.php to read the file and then use explode("\n", $filecontents) to create an array. No need to know how many, just use foreach
     
    sarahk, Mar 12, 2012 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Hi Sarah,

    Thanks for your reply - any chance of an example of code please?

    I guess the easiest thing to do is just have a link of email.php rather than just run it, I suppose either would work.
    Is there a limit to the amount of email addresses I could parce through the code?

    Thanks again,
    Leo
     
    le007, Mar 12, 2012 IP
  4. maxamity

    maxamity Greenhorn

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Do you mean call as put it as a function, and then call that function? I don't quite understand what you mean.
     
    maxamity, Mar 12, 2012 IP
  5. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    I basically just need to know how to execute this script. Can I just link to it as per a normal php file?

    EG: "<a href="email.php">Click here to send out the email</a>"
    Thanks
     
    le007, Mar 13, 2012 IP
  6. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #6
    Is this one large mail send? Why don't you use a 3rd party service like Mailchimp that specialise in this sort of thing?
     
    Alex Roxon, Mar 13, 2012 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Because I prefer to use my own code etc. I'm trying to learn php ",)

    Can I just reference it from a regular <a href?
     
    le007, Mar 13, 2012 IP
  8. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #8
    You prefer to use your own code, that you found online?

    To answer your question, no - you couldn't. You're attempting to send the data after receiving POST data. A simple link won't work, unless you change the script.
     
    Alex Roxon, Mar 13, 2012 IP
  9. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #9
    Yeah, true, but the rest of the site is my own work. I'm only starting on PHP, a complete novice and I did edit certain parts of this code, I'm just trying to learn it in its entirety. Can you please edit it so that it will be " linkable " - I basically want to just send out the same email to various email addresses.

    Not even sure if this is the correct code as I haven't gotten it to work yet. If there is an easier script (php script, not mailchimp) then that would be great!
     
    le007, Mar 13, 2012 IP
  10. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #10
    Hi, can someone please tell me how to execute this file, I can't just name it email.php and <a href> it?

    Thanks
     
    le007, Mar 14, 2012 IP