Preventing Spam When Using Php's Mail Function

free web hosting
Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > PHP

Preventing Spam When Using Php's Mail Function

TavoxPeru
First of all, if this is not the correct place for this topic please an Admin move it accordingly.

Recently i read at the PHPBuilder.com website this excelent article Preventing spam when using PHP's mail function that explains in a very easy way how to avoid spammers send their spam from your own server.

Generally speaking, almost all websites includes some kind of contact form which is used to send emails with the php mail() function, this contact form can be used for a lot of purposes like for example to send comments or sugestions, to report problems on your website, to register users, etc. and can be used and abused by spammers to send out their spam without your knowledge.

This article is very easy to understand and to implement, includes functions for checking valid emails and to prevent scripts to be exploited.

You can use it as a good starter point to prevent this issue to happen and I hope it helps somebody.

Best regards,

 

 

 


Reply

yordan
QUOTE
if this is not the correct place for this topic please an Admin move it accordingly.

No problem, I accept this post here.
However, on the topic subject, I would like to understand something. Do you mean that you could send mails without this kind of contact form, and having your mail being received correctly ? mellow.gif

Reply

TavoxPeru
QUOTE(yordan @ Mar 29 2008, 12:17 PM) *
No problem, I accept this post here.
However, on the topic subject, I would like to understand something. Do you mean that you could send mails without this kind of contact form, and having your mail being received correctly ? mellow.gif

Thanks yordan to move it, and i don't completely understand your question but just in case, my answer is yes.

For example, you have a page -form.php- with a contact form and other data in it, that when it is submitted goes to another page -mail.php- which receives all of the submitted data and then sends an email with the mail() php function as usual. For the sake of the example, this is the same code from the article without any kind of validation. The code of the mail.php is:

CODE
<?php
$to = "bob@domain_example.com";
$subject = "Email from website";
$message = $_REQUEST["body"];
$email = $_REQUEST["email"];

$headers = "From: $email";
mail($to, $subject, $message, $headers);
echo "Thanks for submitting.";
?>

If you don't perform any kind of validation in any of these pages, then it is very easy for a spammer to send emails with your page in this case with your mail.php page.

How??? It is very simple, first you only need to view the source code of your form to get the variable names and to where it will be redirected. The first ones are all the elements of your form and the second one is the value of the ACTION property of the form.

So, it is very easy to send a request like this:
http://your-domain.com/mail.php?body=gotcha&email=barbie@fake-domain.com%0Abcc:spam-1@some-domain.com,spam2@some-domain.com
to abuse it and send my spam.

On the other hand, if you do some kind of validation, your pages will be a lot more secure and will help you to prevent this situation. This is my code with some validation:

CODE
<?php
function contains_newlines($str_to_test) {
   if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
     echo "newline found in $str_to_test. Suspected injection attempt - mail not being sent.<br />";
     echo "here you must use the exit or die php functions to finish the script.<br /><br />";
     // exit;
   }
}

$to = "webmaster@gigasoft.astahost.com";
$subject = "Email from website";
$message = $_REQUEST["body"];
$email = $_REQUEST["email"];
$headers = "From: $email";

if($_SERVER['REQUEST_METHOD'] != "POST"){
   echo "Unauthorized attempt to access page.<br />";
   echo "here you must use the exit or die php functions to finish the script.<br /><br />";
   //exit;
}

contains_newlines($email);

// mail($to, $subject, $message, $headers);
echo "to = $to<br / >subject = $subject<br / >body = " . $_REQUEST['body'] . "<br / >message = $message<br / >email = " . $_REQUEST['email'] . "<br />headers = $headers<br /><br />";
echo "mail ($to, $subject, $message, $headers)<br /><br />";
echo "Thanks for submitting.";
exit;
?>

You can test both of this issues by going to:
  1. My test mail page without any validation
  2. My test mail page with some validation
BTW, both pages do not send any email really.

Best regards,

 

 

 


Reply

yordan
OK, now I see, thanks a lot Tavox.

Reply

Quatrux
Well, by doing a little validation and even programming in a "good" way, these kind of problems won't happen, of course, I know one thing: when you do something for yourself or you're still learning and quite well, you try to do different things, but when you're working and doing for somebody else, not always you have time to do it in a very "perfect" way, the main thing for most clients are that it would work, usually they don't care about the code, or what language it is or how it's possible and for that reason, I really can say that there are lots of "bad" scripts/programs written out there, to get money and to make it work..

I saw some really bad scripts, especially written in php, the main things as I said that they would work, and they do! When things like frameworks appeared, it's a little safer for people who write their applications in Zend framework or any other good framework, it is more secure, it saves time too and you have a better application, the bad thing about it in my opinion, that there are thousands of copies in some library directory of for example zend frameworks biggrin.gif they are there, even though only 4% of them are used..

To conclude, for example I remember I always wanted to write "the best way" in my sites or cms and I even do Today, but when I started doing something not for myself I understood that the main thing is to make it work, I still prefer to make a good application though, but time is money, but with experience I think still most of them are quite good, even written fast biggrin.gif I remember I thought to write for others, you need to comment and write it that other people who might try to edit them or something, that it would be as easier as possible for them, but in most cases, if you wrote the application, bigger chances are that they or he/she will ask support from you again and not from any other guy for support, so you can write it your style or by how you like it biggrin.gif

Reply

TavoxPeru
Yordan, no problem and i'm glad that now you see it better.

Quatrux, you are right, TIME IS MONEY, and when you work for someone else it is a thing that counts a lot, other thing that also counts a lot is that IT MUST WORK. Related to good and bad scripts, always both of them will exists and we can't do anything about them, but for ours, yes we can.

I know that nothing is perfect and never will be, but i'm the kind of person that always try to do my best effort in anything i do, and when it is about programming a bit more, because i know that i can improve my code. I think that it is better to first try in your personal projects and then with the experience that you gain with it apply to your professional work which pay the bills.

BTW, my code posted here is to much simple and i only make it for testing purposes, also, i know that it is not correctly coded, it is not complete and finally I must complete and improve it shortly.

So, please be honest with me and tell me what do you think about it???

Best regards,

Reply

iGuest
Is there any way to block incoming spam mails using PHP script
Preventing Spam When Using Php\'s Mail Function

The script you provided is really good to block out going spam mails from the server on which your sript is running,, but how to block the in coming spams mails on your server. Is there any useful script for that also...
???

Regards muryam

-question by Muryam

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. php block spam preg_match - 2.16 hr back. (2)
  2. php mail without spam - 2.33 hr back. (1)
  3. email function php prevent spam - 5.56 hr back. (1)
  4. why mail goes as spam in php - 6.08 hr back. (1)
  5. spam mail in php - 7.55 hr back. (1)
  6. how to prevent spam email sending through php mail function - 8.61 hr back. (1)
  7. php mail function spam - 5.25 hr back. (2)
  8. php send mail avoid spam - 24.63 hr back. (1)
  9. php spam - 25.11 hr back. (1)
  10. php mail function spam filter - 30.26 hr back. (1)
  11. php email spam - 33.03 hr back. (1)
  12. php mail function avoid spam - 34.04 hr back. (1)
  13. cpanel block mail function - 40.08 hr back. (1)
  14. php send main as spam - 7.47 hr back. (2)
Similar Topics

Keywords : preventing, spam, phps, mail, function

  1. Calendar And The Date () Function
    Making Math Simple With Modular Arithmetic (0)
  2. Sending Authorised Mail Using Imap_mail Function
    Mailing through imap_mail(). (2)
    hi, if you have an IMAP account then you can send the mail using the imap_mail() function of php.
    it is similar to mail() function but is an authorative way.. because your email account will require
    authorisation while sending an email here goes an example.. ==================================
    imap_mail (PHP 3>= 3.0.14, PHP 4 , PHP 5) imap_mail -- Send an email message Description bool
    imap_mail ( string to, string subject, string message ]]] ) This function allows sending of
    emails with correct handling of Cc and Bcc receivers. Returns TRUE on success or FAL....
  3. Sending Mail Using PHP's Mail() Function
    Send mail from any account 2 any account (2)
    hi, It is possible to send mail from any account to any account using the php's built in
    mail() function.. for which is very easy to write the coding... wt you are supposed to do is just
    pass the parameters like from, to, subject, message .. and attachments if any, and your email will
    be sent in no time.. as Astahost.com supports the php scripts you can use it if you already have
    an account here... ============================================== Example 1. Sending mail.
    mail("joecool@example.com", "My Subject", "Line 1\nLine 2\nLine 3"); ?> ....

    1. Looking for preventing, spam, phps, mail, function

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for preventing, spam, phps, mail, function
advertisement




Preventing Spam When Using Php's Mail Function



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE