Hy to all, and sorry for my english
I am trying to make a sort of script to post to a random email loaded from a csv list of emails to post a random html file from a random folder.
(Many years ago i coded hard in VB 6 and dot net but now I feel too old for php sintax. I could not manage it. 🙁
I realise the simplest part - to send the html email, but not from the file.
I think , in the general informatic speaking I must to :
"scan" the names of the files.html inside the folder ( DIR) and to store them in an array.
Randomly to choose one or more names ( as variables)
To load the html content of that file in a variable ( string character)
to insert that variable inside the content of the email....to send them..
function RandomFile($folder='', $extensions='.*'){
// fix path:
$folder = trim($folder);
$folder = ($folder == 'dir') ? './' : $folder;
// check folder:
if (!is_dir($folder)){ die('invalid folder given!'); }
// create files array
$files = array();
// open directory
if ($dir = @opendir($folder)){
// go trough all files:
while($file = readdir($dir)){
if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){
// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open the folder "'.$folder.'"');
}
if (count($files) == 0){
die('No files where found :-(');
}
// seed random function:
mt_srand((double)microtime()*1000000);
// get an random index:
$rand = mt_rand(0, count($files)-1);
// check again:
if (!isset($files[$rand])){
die('Array index was not found! very strange!');
}
// return the random file:
return $folder . $files[$rand];
?>
ALSO
i have another variant :
<?
function getRandomFile($start_dir)
{
/*
returns the name of one random file from within a directory
*/
chdir($start_dir);
$dir = opendir('.');
while (($myfile = readdir($dir)) !==false)
{
if ($myfile != '.' && $myfile != '..' && is_file($myfile) && $myfile != 'resource.frk')
{
$files[] = $myfile;
}
}
closedir($dir);
chdir('../');
srand ((float) microtime() * 10000000);
$file = array_rand($files);
return $files[$file];
}
?>
<?
$includeDir = 'randomquotes';
$quoteFile = getRandomFile($includeDir);
include("{$includeDir}/{$quoteFile}");
?>
<?
also what will be the form of that variable to insert in the mail content to display the html file loaded inside it?
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: [email]webmaster@example.com[/email]\r\nReply-To: [email]webmaster@example.com[/email]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Thanks for any sugestions