Results 1 to 4 of 4

Thread: send mails in php

  1. #1
    Join Date
    Aug 2013
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default send mails in php

    Am trying to send mail to 3 gmail recipient account. The codes runs but could not send mail to any of the account. am using xammp server. can someone tell me what is wrong

    Thanks

    Code:
    <?php
    if(!isset($_POST['submit']))
    {
    echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    
    //Validate first
    if(empty($name)||empty($visitor_email)) 
    {
        echo "Name and email are mandatory!";
        exit;
    }
    
    if(IsInjected($visitor_email))
    {
        echo "Bad email value!";
        exit;
    }
    
    require('pdo_db.php');
    $result = $db->prepare('SELECT * FROM email_list
    order by id asc');
    
    $result->execute(array());
    
                     while ($row = $result->fetch()) 
                           {
    $addresses[]=$row['email'];
    }
    
    $email_from = 'me@gmail.com';//<== update the email address
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".
        "Here is the message:\n $message".
        
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    
    $to = implode(", ", $addresses);
    
    mail($to,$email_subject,$email_body,$headers);
    
    //echo $to;
    
    header('Location: thanks.html');
    
    
    
    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
      $injections = array('(\n+)',
                  '(\r+)',
                  '(\t+)',
                  '(%0A+)',
                  '(%0D+)',
                  '(%08+)',
                  '(%09+)'
                  );
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str))
        {
        return true;
      }
      else
        {
        return false;
      }
    }
       
    ?>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    xampp? Is this on your local machine? If so, have you installed and configured an email server?

    Also, you don't check if your call to mail was successful or not. And do you have error reporting enabled?

    ~~~~~~~~~~~~~~~~~~~
    not related to your specific question:

    You make an attempt to check the submitted email address for injection attempts, but you don't check if it a valid email address.
    You can use filter_var, with the FILTER_VALIDATE_EMAIL flag, to check. As an added benefit, this will fail if there are any line break characters, so it will also stop header injections in the email field.
    Last edited by traq; 05-01-2014 at 12:27 AM.

  3. #3
    Join Date
    Aug 2013
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i have re-write the mail as follows. it works now however, it sends email to spam instead of the mail inbox. what could be the problem.

    Code:
    <?php
    if(!isset($_POST['submit']))
    {
    	//This page should not be accessed directly. Need to submit the form.
    	echo "error; you need to submit the form!";
    }
    $message = strip_tags($_POST['message']);
    if(empty($message)) 
    {
        echo "message is mandatory!";
        exit;
    }
    
    
    
    require('pdo_db.php');
    $result = $db->prepare('SELECT * FROM email_list
    			order by id asc');
    
    		$result->execute(array());
    
                     while ($row = $result->fetch()) 
                           {
    $addresses[]=$row['email'];
    }
    
    
    $to = implode(", ", $addresses);
    
    
    $subject = "Testing";
    //$message = "Hello! mail from .";
    $from = "me@gmail.com";
    
    $headers = "From:" . $from;
    $retval = mail($to,$subject,$message,$headers);
    
       if( $retval == true )  
       {
          echo "Message sent successfully...";
       }
       else
       {
          echo "Message could not be sent...";
       }
    
    ?>
    Thanks

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Tips & Tricks to Stay Out of the Spam Folder

    Most likely cause, in this case, is item #1: "misleading header information". When the "From" header has one domain ("gmail.com", for example) but the email came from a server that doesn't belong to that domain (for example, your web host's mail server), you get spam points. This, by itself, is often enough to get your email into the spam folder.

    1) Use your website's domain name in the From header (it doesn't need to be a real account; "no-reply@your.site.example.com" is fine), or,

    2) Add a "Sender" header that uses you website's domain name.

    There may be any number of additional causes, however, so don't be surprised if this doesn't automatically work. Many are harder to solve, and much harder to diagnose. Your best bet, since you're sending to your own email account, is to whitelist the "From" email in your email client.

Similar Threads

  1. Web Form Generating 2 E-Mails
    By Dankees in forum PHP
    Replies: 0
    Last Post: 11-06-2013, 01:08 PM
  2. php script - display e-mails
    By mencel.net in forum Looking for such a script or service
    Replies: 6
    Last Post: 08-09-2010, 10:33 PM
  3. automatically sending mails
    By hemi in forum PHP
    Replies: 0
    Last Post: 10-02-2009, 02:06 PM
  4. Caution with PayPal e-mails.
    By tech_support in forum The lounge
    Replies: 15
    Last Post: 03-12-2007, 06:21 AM
  5. About Menu and sending mails!
    By newpupil1 in forum HTML
    Replies: 8
    Last Post: 09-19-2005, 10:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •