View Full Version : mail script newsletter
chechu
03-30-2009, 02:15 PM
Hey all,
I have this php script that I use for my contact form, and it works perfectly.
Now I'd like to have a script subscribing to a newsletter, that just sends me the email adres filled in the area, also checking if it is a valid mailadres.
Here's the mailform code I have, just for information:
<?php
if ($_POST["action"] == "send"){
if ($_POST[naam] != " je naam" and $_POST[naam] != "" and $_POST[email] != " je e-mail adres" and $_POST[email] != "" and $_POST[bericht] != "") {
mail ("info@domain.com", "via site",
"
Naam: ".$_POST['naam']."
E-mail: ".$_POST['email']."
Bericht: ".$_POST['bericht']."
",
"From: ".$_POST['naam']." <".$_POST['email'].">");
$subject = "your message to";
$msg = "
Dear $_POST[naam],
Thanks for your message. Will get back to you shortly.
This was your message:
$_POST[bericht]
";
mail($_POST[email], $subject, $msg);
echo 'thanks for your feedback !';
}
else{
echo 'please fill in all fields';
}
}
?>
Can anyone help me out, please ?
Do you just want something emailing you the email they put in, or a whole system that inserts the email in a db, then a little admin form for you where you can send everybody an email, or what. xD
chechu
03-31-2009, 09:00 AM
Just something that mails me the adress that the visitor put in the field, nothing more. I don't expect many subscribers, so wouldn't bother for now ;)
Schmoopy
03-31-2009, 09:16 AM
Here it is:
<?php
include('includes/functions.php');
$myemail = "name@mail.com"; // Edit this with your email address
$mysubj = "New User"; // Set the subject of mail that you will receive from users
if ($_POST["action"] == "send"){
if ($_POST[naam] != " je naam" and $_POST[naam] != "" and $_POST[email] != " je e-mail adres" and $_POST[email] != "" and $_POST[bericht] != "") {
mail ("info@domain.com", "via site",
"
Naam: ".$_POST['naam']."
E-mail: ".$_POST['email']."
Bericht: ".$_POST['bericht']."
",
"From: ".$_POST['naam']." <".$_POST['email'].">");
$subject = "your message to";
$msg = "
Dear $_POST[naam],
Thanks for your message. Will get back to you shortly.
This was your message:
$_POST[bericht]
";
if(checkEmail($_POST['email']))
{
mail($_POST[email], $subject, $msg);
mail($myemail, $mysubj, $_POST['email']); // Sends the mail to you
}
else
die('Invalid Email');
echo 'thanks for your feedback !';
}
else{
echo 'please fill in all fields';
}
}
?>
checkEmail function:
function validEmail($email)
{
if(!preg_match('/^[^0-9][A-z0-9_-]+[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$/',$email))
return false;
else
return true;
}
Put the function in an includes file somewhere, like "includes/functions.php". If for whatever reason you don't / can't do this then just copy the function straight into the top of the page.
Found the checkMail function through google but it seems to work well enough. It doesn't check for DNS records but I mean as you said your not expecting 1000s of subscribers and people potentially makings bots to spam your site, for what you want it should be sufficient
You could always have the $mysubj variable dynamic, but I don't know how you want it done, so I've just left it as it is.
Good Luck :)
chechu
03-31-2009, 10:07 AM
No, no no ! The code you gave has "name", "email","message" etc !
I just need a form that sends me the email that is written in the input box, nothing more (and maybe a confirmation mail to the sender).
So there is no "name" and "message" !
Schmoopy
03-31-2009, 10:48 AM
Oh so their email address is the title of the email itself? I see... in that case...
<?php
include('includes/functions.php');
$myemail = "name@mail.com"; // Edit this with your email address
if ($_POST["action"] == "send"){
if ($_POST[naam] != " je naam" and $_POST[naam] != "" and $_POST[email] != " je e-mail adres" and $_POST[email] != "" and $_POST[bericht] != "") {
mail ("info@domain.com", "via site",
"
Naam: ".$_POST['naam']."
E-mail: ".$_POST['email']."
Bericht: ".$_POST['bericht']."
",
"From: ".$_POST['naam']." <".$_POST['email'].">");
$subject = "your message to";
$msg = "
Dear $_POST[naam],
Thanks for your message. Will get back to you shortly.
This was your message:
$_POST[bericht]
";
if(checkEmail($_POST['email']))
{
mail($_POST[email], $subject, $msg);
mail($myemail, $_POST['email'],''); // Sends just the email to you with a blank message
}
else
die('Invalid Email');
echo 'thanks for your feedback !';
}
else{
echo 'please fill in all fields';
}
}
?>
Hope this is what you're looking, I may be missing something but I think this is what you mean :)
chechu
03-31-2009, 10:58 AM
The codes below refer to "name" or "message", which there isn't in the form !
Maybe you could start from scratch, and not look at the code I attached in my first post, please.
This is all I need: how to send a filled in email adress (to subscribe to a newsletter), and send a confirmation mail to the filled in adress.
if ($_POST[naam] != " je naam" and $_POST[naam] != "" and $_POST[email] != " je e-mail adres" and $_POST[email] != "" and $_POST[bericht] != "") {
Naam: ".$_POST['naam']."
E-mail: ".$_POST['email']."
Bericht: ".$_POST['bericht']."
"From: ".$_POST['naam']." <".$_POST['email'].">");
$subject = "your message to";
$msg = "
Dear $_POST[naam],
Thanks for your message. Will get back to you shortly.
This was your message:
$_POST[bericht]
";
Schmoopy
03-31-2009, 11:11 AM
Oh I see, I was going on the assumption that you wanted to keep the other code :p, right... So there's just an input box with the email so:
<?php
function checkEmail($email)
{
if(!preg_match('/^[^0-9][A-z0-9_-]+[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$/',$email))
return false;
else
return true;
}
$subj = "Subscription Notice"; // Subject of the email the user receives
$msg = "Thanks for subscribing to my newsletter, you will be added to the list soon"; // Enter message of the email here the user receives
$mymail = 'myemail@email.com'; // Change to your email
if (isset($_POST['email']) && !$_POST['email'] == '') // Check fields not empty and have been submitted
{
if(checkEmail($_POST['email'])) // Make sure email is valid
{
mail($_POST['email'], $subj, $msg); // Send mail to user
mail($mymail, $_POST['email'], ''); // Send mail to you
die('Email added, please check your inbox for confirmation');
}
else
die('Invalid Email');
}
else
die('Please fill in the email field');
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.