Hi,

I've installed a simple email collection form on my website which writes new email records onto a text file on my server. It works great but I would like to receive an email notification every time a new email address is collected. I'm far from being an expert so could someone help me add the relevant code to the write.php file? That would be much appreciated. Thanks a lot. 🙂 Below is the current code.

<?php

$enable_cookies = ($_GET['c'] == "1");

$file = "emails.txt";
$email = $_GET['e'];
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest"){

$email_list = file_exists("emails.txt") ? explode(",", file_get_contents("emails.txt")) : array();
array_pop($email_list);

if(!in_array($email, $email_list)){
	if(file_put_contents($file, $email.",", FILE_APPEND)){
		echo "<font color='#ffffff' face='Arial, Helvetica, sans-serif' size='-1'> Added ".stripslashes(htmlspecialchars($email)) ." to the list!</font>";
		if(substr(sprintf('%o', fileperms('emails.txt')), -4) != "0640"){
			chmod("emails.txt", 0640);
		}
		if($enable_cookies){
			setcookie("em_email", $email, time()*2, "/");
		}
	}
	else{
		echo "<font color='ffffff' face='Arial, Helvetica, sans-serif' size='-1'>Failed to add to list!</font>";
	}
}
else{
	echo "<font color='ffffff' face='Arial, Helvetica, sans-serif' size='-1'>Email already in the list!</font>";
}
}
else{
	echo "This script can only be accessed through javascript!";
}
?>

    Hi,

    Thanks but I'm not too sure what to make of this link. I'm far from being an expert and am not too sure what part of the thread I'm supposed to use. Also, what do you mean exactly by 'stick it right after the text file is updated'? How do I do that? Thanks

      rialop;11024157 wrote:

      Thanks but I'm not too sure what to make of this link.

      That link is a link to the PHP manual page for the mail() function. On that page, you'll find documentation (including code examples) that explains how the function is to be used.

      rialop;11024157 wrote:

      what do you mean exactly by 'stick it right after the text file is updated'? How do I do that?

      Locate the statement(s) in your existing code that updates the text file, and then add a call to the mail() function to send an e-mail at that point.

        Write a Reply...