PHP Mail
I'm new to PHP and so far I've written code that allows users to input their emails into a database... It is a text file in the directory.
I need another function to go through and send an email to each line (containing the addresses).
How do I create this kind of loop?
Edit- I know how to send the mail, I just need to know how to load it up and split them up into seperate strings and use each of those as the $email
[457 byte] By [
LTsK8eR2gO] at [2007-11-19 22:48:04]

# 1 Re: PHP Mail
Do you need help with the loop, or getting the email addresses into an array?
If you need help with the loop, its simply
for($i=0;$i<count($emails);$i++)
// mail stuff for $emails[$i];
I assume your problem is loading the array, $emails, with the addresses. By looking at your edit, that's the indication I get. How is it in the text file? If it is just a string, you can get the contents (different ways that will be provided once you say how the file is), and then use explode as:
$emails = explode("|",$txtFile); // if separated by a pipe, for example
# 2 Re: PHP Mail
Nah I'm a Java/C/C++ coder, I know how the loops work. :)
They're actually seperated by a line:
email@domain.ext
email2@domain2.ext
email3@domain3.ext
You see..
Would it be:
[code]$emailsArray = explode("\n", $txtFile);[/quote]
Also, what is the difference between double and single quotes in PHP? In Java, etc, they're to tell the difference between a char and a string...
# 3 Re: PHP Mail
I had a very comprehensive reply about an hour ago, but my electricity went out for an hour, so I'll just do this quickly.
The major difference between single and double quotes in PHP is the following:$var = "Hello";
echo "$var"; // prints Hello
echo '$var'; // prints $var
For your problem:$fl = fopen("emails.txt","r");
$lst = "";
if(filesize("emails.txt") > 0)
$lst = fread($fl, filesize("emails.txt"));
fclose($fl);
$emails = explode("\n",$lst);
for($i=0;$i<count($emails);$i++)
// mail stuff for $emails[$i];You might need to use "\r\n" in place of "\n".
There is an alternative file way of doing this I'm not familar with that peejavery could probably provide an example, but this way will hold up (but typically is better when it isn't called very frequently - I think fopen more memory intensive or something like that).
There are bound to be errors in my untested code, so just tell me about them.
# 4 Re: PHP Mail
You're amazing, thank you. Done and done.
EDIT - What is the difference between "\r\n" and "\n"
Is it an OS thing? Also, if I just went ahead and used "\r\n" would it be the 100%-safe way of doing things?
(Sorry to hear about your electricity--I hate it when that happens.)
# 5 Re: PHP Mail
Oh, and another question...
say in folder "xkkk" I have "xkkk.php":
<?php
function func1() { echo "sup"; }
?>
Now, inside folder "xkkk" I have another folder called "xk1" in which "xk1.php" exists:
<?php
func1();
?>
How do I get that to work? Calling functions in other directories...
# 6 Re: PHP Mail
You can include the other php file: <? include("/xkkk/xkkk.php") ?>
However, I'm not sure you can include files from directories like that. Perhpas this would work:
<? include("../xkkk/xkkk.php") ?>
# 7 Re: PHP Mail
EDIT - What is the difference between "\r\n" and "\n"
Is it an OS thing? Also, if I just went ahead and used "\r\n" would it be the 100%-safe way of doing things?
You are correct in your assumption of it being an OS difference.
Windows - \r\n
Mac - \r
# 8 Re: PHP Mail
K, so what about "\n"? It worked fine when I used it to seperate the emails in the text file, so can I still use that as the delimiter when putting the strings into the array?
# 9 Re: PHP Mail
K, so what about "\n"? It worked fine when I used it to seperate the emails in the text file, so can I still use that as the delimiter when putting the strings into the array?
\n is a cheaters method. It is not standard, but it usually works universally.
The major difference is that \n is line return and \r is line carriage.