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 - "; $j++; if ($j==4){echo "<BR>\n";$j=0;} mail($recipient, stripslashes($subject), stripslashes($msg), $headers); // the mail() function sends the message } } ?> PHP:
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
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
Do you mean call as put it as a function, and then call that function? I don't quite understand what you mean.
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
Is this one large mail send? Why don't you use a 3rd party service like Mailchimp that specialise in this sort of thing?
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?
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.
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!
Hi, can someone please tell me how to execute this file, I can't just name it email.php and <a href> it? Thanks