<?php
/* SQL CODE:
CREATE TABLE IF NOT EXISTS `dalanmail` (
`email` varchar(80) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
*/
$settings = array();
////// Settings
$settings['mysqlhost'] = ""; // mysql host
$settings['mysqluser'] = ""; // mysql user
$settings['mysqlpass'] = ""; // Mysql pass
$settings['mysqldatb'] = "";; // Mysql database
$settings['from'] = ""; // Webmaster email
$settings['pass'] = ""; // Webmaster password to send mails
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////// Functions
function valid_email($email){
//check email format
if (filter_var($email, FILTER_VALIDATE_EMAIL) == $email){
//check if the domain has MX entries
$aux = explode('@',$email);
return checkdnsrr($aux[1],'MX');
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$mysqlconnection = mysql_connect($settings['mysqlhost'], $settings['mysqluser'], $settings['mysqlpass']);
$mysqlselectdb = mysql_select_db($settings['mysqldatb'], $mysqlconnection);
if(!$mysqlconnection){
echo 'Error: could not connect to database!<br/>';
exit;
}
if(!$mysqlselectdb){
echo 'Error: could not select database!<br/>';
exit;
}
if(isset($_GET['exec']) && $_GET['exec'] == "sendmail"){
if($_SERVER['REQUEST_METHOD'] == "POST"){
if($_POST['pass'] == $settings['pass']){
$getqry = "
SELECT *
FROM dalanmail
";
if($res = mysql_query($getqry)){
if(mysql_num_rows($res) == 0){
echo 'Noone is subscribed!<br/>';
}
else
{
while($row = mysql_fetch_assoc($res)){
mail($row['email'], htmlentities($_POST['subject']), $_POST['message'], 'Content-type: text/html; charset=iso-8859-1');
}
echo 'Mail was send!<br/>';
}
}
else
{
echo 'Error: Mysql error!<br/>';
}
}
else
{
echo 'Password is not correct!<br/>';
}
}
else
{
?>
<h1>Send mail</h1><br/>
<form action="?exec=sendmail" method="post">
Subject: <input type="text" name="subject"/><br/>
Content:<br/>
<textarea cols="50" rows="8" name="message"></textarea><br/>
Password: <input type="password" name="pass"/>
<input type="submit" value="Send"/>
</form>
<?php
}
}
else
{
if($_SERVER['REQUEST_METHOD'] == "POST"){
if($_POST['subscribe'] == "subscribe"){
if(valid_email($_POST['email']) == true){
$postqry = "
INSERT INTO dalanmail
(
email
)
VALUES
(
'". mysql_real_escape_string($_POST['email']) ."'
)
";
if(mysql_query($postqry)){
echo 'Subscribed!<br/>';
}
else
{
echo 'Error: Mysql error<br/>';
}
}
else
{
echo 'Email adres is not valid.<br/>';
}
}
elseif($_POST['subscribe'] == "unsubscribe"){
if(valid_email($_POST['email']) == true){
$deleteqry = "
DELETE FROM dalanmail
WHERE email = '". mysql_real_escape_string($_POST['email']) ."'";
if(mysql_query($deleteqry)){
if(mysql_affected_rows() == 0){
echo 'Email adress does not match to our database!<br/>';
}
else
{
echo 'You where deleted from our mailing list!<br/>';
}
}
else
{
echo 'Error: Mysql error';
}
}
else
{
echo 'Your email adress was not valid.';
}
}
else
{
echo 'Not an valid post method?<br/>';
}
}
else
{
?>
<form action="" method="post">
Email adress: <input type="text" name="email"/>
<input type="radio" name="subscribe" value="subscribe" checked="checked"/>Subscribe |
<input type="radio" name="subscribe" value="unsubscribe"/>Unsubscribe<br/>
<input type="submit" value="(un)subscribe!"/><br/>
</form>
<?php
}
}
?>
Ziehier mijn eigen nieuwsletter systeem, heel simpel, toch weer een leuke uitdaging.
Ik denk, laat ik em eerst maar even keuren.
Nou, wat vinden jullie ervan?
Edit: voorbeeld:
http://prive.alphenweer.nl/phphulp/dalanmail.php
En om mails te versturen:
http://prive.alphenweer.nl/phphulp/dalanmail.php?exec=sendmail
Wat ik nog ga doen is:
-from header zetten in de mail
-empty checks doen, helemaal vergeten (dom, dom dom dom!)