Simple Unsubscribe Script [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Simple PHP Mailer Script
i run a small website and we have had requests for a mailing list....
i have found a simple free script to add email addresses to a txt file - "email.txt" (which is protected) is csv format (email1#yahoo.com,email2#yahoo.com,blah,blah,blah)
this script works flawlessly.....
however it is a nuciance to go through this text file to manually remove email addresses when users cancel their subscription to the mailing list.....
i am having trouble creating a simple script to remove email addresses....
so far i have ....
<html>
<body>
<form method="post" action="">
<p>Email Address: <input type="text" name="email"></p>
<input type="submit" value="Cancel Newsletter" name="submit">
</form>
<?php
$email = $_POST["email"];
$text = file_get_contents("email.txt");
$oldWord = "$email";
$newWord = "";
$text = str_replace($oldWord , $newWord , $text);
$fp = fopen('email.txt', 'w');
fwrite($fp, $text);
fclose($file);
?>
</body>
</html>
this works perfect to remove the email address from the list.....
however i cannot figure out how to echo a comment dependant on whether
str_replace($oldWord , $newWord , $text);
is successful at replacing the email address with an empty space....
if the code removes email address i would like "You Have Been Removed From The Flying Shuttle Newsletter."
and if the email address isnt there (code doesnt replace email with empty space)
"The Email Address You Specified Is Not On The Mailing List!"
thanks in advance for any help

You could just compare the length of the returned string.
$newText = str_replace($oldWord, $newWord, $text);
if (strlen($newText) < strlen($text)) {
// Success
} else {
// Failure
}

Related

How to send an SMS from a web server written in PHP? using Betamax url

Betamax offers link to send sms , how to convert this links to php plus html form to send sms.
Using HTML SMSlink
You can also send text messages (SMS) without using our software or accessing the website. Use the following link and fill in the desired data:
https://www.poivy.com/myaccount/sendsms.php?username=xxxxxxxxxx&password=xxxxxxxxxx&from=xxxxxxxxxx&to=xxxxxxxxxx&text=xxxxxxxxxx
Explanation of the variables:
username: your poivY username
password: your poivY password
from: your username or your verified phone number. Always use international format for the number starting with +, for instance +491701234567
to: the number you wish to send the sms to. Always use international format starting with +, for instance +491701234567
text: the message you want to send
If you know the service provider, just use the phone number and the service provider email, it's a back-end way to the world of SMS.
Check it out: Email Service Providers for Texting Through Email
Use the HTML form for user info:
<p>Enter the Service Provider and the phone number to send a text right now!!</p>
<form action="current page or another, depending on structure" onsubmit="alert('Sent, it should be received shortly...');" method="post">
<select name="inputMobileServiceProvider" required>
<!--These are just some of them-->
<option value="Verizon">Verizon</option>
<option value="ATT">AT&T</option>
<option value="TMobile">T-Mobile</option>
<option value="Sprint">Sprint</option>
</select>
<input type="tel" name="inputMobileNumber" required />
<input type="text" name="inputSubject" placeholder="Subject..." />
<textarea name="inputMessage" required></textarea>
<button>Send!!</button>
</form>
And the PHP, whether it be on any page:
<?php
$inputServiceProvider = $_POST['inputMobileServiceProvider'];
$inputMobileNumber = $_POST['inputMobileNumber'];
$inputSubject = $_POST['inputSubject'];
$inputMessage = $_POST['inputMessage'];
if ($inputServiceProvider === "Verizon") {
$serviceProvider = "#vtext.com";
} elseif ($inputServiceProvider === "ATT") {
$serviceProvider = "#txt.att.net";
} elseif ($inputServiceProvider === "TMobile") {
$serviceProvider = "#tmomail.net";
} elseif ($inputServiceProvider === "Sprint") {
$serviceProvider = "#messaging.sprintpcs.com";
} else {
echo "<script>alert('I\'m sorry but a Service Provider wasn't found, please try again!')</script>";
}
$searchArray = array(" ", "-", "(", ")", "+1")
$inputMobileNumber = str_ireplace($searchArray, "", $inputMobileNumber);
$recipientEmail = $inputMobileNumber + $serviceProvider
mail($recipientEmail, $inputSubject, $inputMessage);
?>
And remember, this is just an example. For production-use, use htmlspecialchars() and other security measures on the website. Also, make sure that there is a spam control. Your website might be in trouble if you mess with random SMS numbers, or if a user is spamming others.

Mail function not sending email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I'm pretty new to PHP and getting to grips with a (very simple) contact form. When I uploaded this to my hosting company, the PHP script does run as when I fill out and submit the form it brings back my confirmation but the email does not send. My hosting company is 123-reg. Do I have to do anything with them to allow the email to be sent?
My code is as follows:
Form Code:
<form action="action_handler.php" method="POST">
<dl>
<dt> Name:
<dd><input type="text" name="name">
<dt> Email Address:
<dd><input type="text" name="mail">
<dt> Comments:
<dd><textarea rows="5" cols "20" name="comment">
</textarea>
</dl>
<p><input type="submit"></p>
</form>
PHP Code:
<?php
$name = $_POST['name'];
$mail = $_POST['mail'];
$comment = $_POST['comment'];
$to = "someone#hotmail.co.uk";
$subject = "Contact Form Request";
$body = "You have a new message from: /n Name: $name /n Email: $mail /n/nThe request is as follows: /n/n $comment";
mail ($to,$subject,$body);
echo"<p>Thanks for your comment $name ...</p>";
echo"<p><i>$comment</i></p>";
echo"<p>We will reply to $mail</p>";
?>
Any help is much appreciated.
There are a few things wrong with your present code.
Firstly, /n which should read as \n very important. It's a syntax error.
Then, there's the missing From: which not having that in mail headers, will most likely be rejected or sent to spam.
The "from" in mail will end up being an email address from "#yourserver.xxx"
To include a From: use the following:
$header = "From: ". $name . " <" . $mail . ">\r\n";
which will show up as the person's name in the "from", yet identified as a proper email address.
More often than none, mail is sent to spam whenever From: is omitted from headers.
Therefore
mail ($to,$subject,$body);
should be modified to
mail ($to,$subject,$body,$header);
including the line above, to be inserted underneath the $body variable.
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
You can also use a conditional statement to check if mail was indeed sent:
if(mail ($to,$subject,$body,$header)) {
echo "Sent.";
} else{
echo "Sorry, check your mail logs.";
}
Once mail goes out, it's out of your server's hands and into the receiver's end. There's nothing you can do "after the fact."

How to link the form and php regex together?

I'm new to php. What I'm trying to do right now is do a email validation form using regular expression. When a user enter something silly to the text field and click submit. It will show the email is invalid. I'm using regular expression to test whether user enter the right format. Can anyone please guide me how to link the php regular expression with the form together ?
<form action= " " method = "post">
<p> Enter your email </p>
<input type ="text" name ="user_email"/>
<input type ="Submit" />
</form>
<?php
$email = " abc#gmail.com ";
$regex = "^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
if (preg_match ($regex, $email )){
echo $email . "is a valid email";
}else{
echo $email. " is an invalid email ";
}
?>
Change
$email = " abc#gmail.com ";
to
$email = $_POST['user_email'];
Then it'll take input from the form. Since you're POSTing data to the script all of the variables you've posted will end up in the superglobal $_POST.
You can link the $_POST request in several ways, for example:
$email = $_POST['user_email'];
Also, preg_match needs a beginning and ending delimiter to function properly:
$regex = "/^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/";
Result:
abc#gmail.com is an invalid email
Example:
http://ideone.com/igphq1

How to make form fields required? [duplicate]

This question already has answers here:
Making email field required in php [closed]
(4 answers)
Closed 8 years ago.
I have this existing code and I am wondering how to make the name and email field required?
<?php
if(isset($_POST['submit'])){
$to = "xxx#email.com"; // this is your Email address
$from = $_POST['gift_email']; // this is the sender's Email address
$first_name = $_POST['gift_name'];
$subject = "Free Gift Request";
$msg = "A free gift has been requested from the following:"."\n";
$msg .= "Name: ".$_POST["gift_name"]."\n";
$msg .= "E-Mail: ".$_POST["gift_email"];
$headers = "From:" . $from;
mail($to,$subject,$msg,$headers);
//echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header('Location:free_program_thankyou.php');
}
?>
For form
<input type="text" name="gift_email" required>
<input type="text" name="gift_name" required>
For Php
if(empty($_POST['gift_email']))
{
echo 'This field is required';
}else {
//Do what you want to do here
}
A two basic ways to do this:-
Within the php program check each required form field has been filled in send a new page with an error message back if it is not. Be sure to return the contents of any fields already filled in or your users will wish a plague of boils on your person.
Validate in javascript. Have a function triggered by the "onsubmit" condition which checks for all required forms fields are filled and highlights any that are not. see here
In practice a robust web site will do both. This seems like duplication however the javascript function is much more responsive and user friendly, BUT, the php server side validation cannot be gamed by turning JS off or spoofing responses.

Make mail() php file safe from SQL Injections [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After reading a lot of tutorials I just want to be sure to be on the safe side.
I made a contact formular which looks like this
<form name="contakt" accept-charset="UTF-8" method="post" action="./mail.php">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="tel" />
<input type="submit" value="Send" name="submit" />
<textarea name="message"></textarea>
</form>
I validate via jQuery if the name and message is not empty and not only full of spaces
and I check the email via jquery with the following script
function ismailornot(email) {
var regex = /^([a-zA-Z0-9_.+-])+\#(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
Now when my variables get passed and I am on my mail.php is it more then enough to check on top my of script the $_SERVER['HTTP_REFERER'] and look if those variables came from my own script ? Or can you modify $_SERVER variables too ?
Or do I have basicly to check EVERY passed variable again to be on a safe side ?
For example : http://www.w3schools.com/php/php_secure_mail.asp
is this script 1oo% safe from injections ?
Thanks for helping me out :)
The way: Check EVERY passed variable again to be on a safe side
Try this after some mods to fit your needs its a piece from Larry Ullman book :
function spam_scrubber($value) {
// List of very bad values:
$very_bad = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:','multipart-mixed:',
'content-transfer-encoding:', '<script>');
// If any of the very bad strings are in
// the submitted value, return an empty string:
foreach ($very_bad as $v) {
if (stripos($value, $v) !== false){ return '';}
}
// Replace any newline characters with spaces:
$value = str_replace(array( "\r", "\n", "%0a", "%0d"), ' ', $value);
//remove html tags:
$value = htmlentities($value,ENT_QUOTES);
// Return the value:
return trim($value);
} // End of spam_scrubber() function.
// Clean the form data:
$scrubbed = array_map('spam_scrubber', $_POST);
if(isset($from)) {
$from = $scrubbed['from'];
}else{
$from = '';
}
// Minimal form validation:
if (!empty($from) && !empty($scrubbed['comments']) ) {
// Create the body:
$body = "Name: {$from}\n\nComments: {$scrubbed['comments']}";
$body = wordwrap($body, 70);
// Send the email:
mail('YOUR_EMAIL', 'Contact Form Submission', $body, "From: {$from}");
}

Categories