Sending Email with PHP code

Discussion in 'PHP' started by elearnindia, Jun 24, 2010.

  1. #1
    I am trying to send plain/text email to gmail using the following PHP code:

    <?php
    if(isset($_POST['email'])) {
    				
           // EDIT THE 2 LINES BELOW AS REQUIRED
          $email_to = "elearnindia123@gmail.com";
          $email_subject = "Sree Vizag Marketing-Tiles Enquiry";
    													
    													
          $first_name = $_POST['name']; // required
          $email_from = $_POST['email']; // required
          $phone = $_POST['phone']; // not required
          $comments = $_POST['comments']; // required
    													
    													
          $email_message .= "First Name: ".$first_name."\n\n";
          $email_message .= "Email: ".$email_from."\n\n";
          $email_message .= "Telephone: ".$phone."\n\n";
          $email_message .= "Comments: ".$comments."\n";
    
         //create email headers
          $headers = "From: $email_from";
          'Reply-To: '.$email_from."\r\n" .
          'X-Mailer: PHP/' . phpversion();
    												
    												
         $mail_sent = @mail($email_to, $email_subject, $email_message, $headers);
    
                                                                                                     
         echo $mail_sent ? "Mail sent" : "Mail failed";
    }
    ?>
    PHP:
    Using this code the email does not show up in the gmail inbox.Any kind of help will be appreciated.

    srinivas.
     
    elearnindia, Jun 24, 2010 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Did you check other mail servers or spam folder on gmail?
    These lines seem strange:
      //create email headers
          $headers = "From: $email_from";
          'Reply-To: '.$email_from."\r\n" .
          'X-Mailer: PHP/' . phpversion();
    
    PHP:
    I think they should be something like this:
    
      //create email headers
          $headers = "From: $email_from\n";
          $headers .="Reply-To: .$email_from\n";
          $headers .= "X-Mailer: PHP/" . phpversion();
    
    PHP:
    Also did you get "Mail sent" mesage?
     
    AsHinE, Jun 24, 2010 IP
  3. langtusitinh225

    langtusitinh225 Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Last edited: Jun 24, 2010
    langtusitinh225, Jun 24, 2010 IP
  4. getlandersgetpaid

    getlandersgetpaid Guest

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, what AsHinE said, its your lines that construct the header.
      $headers = "From: $email_from\n";
          $headers .="Reply-To: .$email_from\n";
          $headers .= "X-Mailer: PHP/" . phpversion();
    Code (markup):
    Try that and echo them out to check them. Its highly likely that this is the problem.
     
    getlandersgetpaid, Jun 24, 2010 IP
  5. elearnindia

    elearnindia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello AsHinE ,

    I have added the header as:

    //create email headers
    $headers = "From: $email_from\n";
    $headers .="Reply-To: .$email_from\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    On emailing the message,I am getting "Mailsent" message.
    Still not getting the mail in theinbox of gmail is there anything more to
    done in header injection.
     
    elearnindia, Jun 25, 2010 IP
  6. elearnindia

    elearnindia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hello getlandersgetpaid,

    I modified the header with concatination as follows

    $headers = "From: $email_from\n";
    $headers .="Reply-To: .$email_from\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    Still not getting the email in the inbox of gmail.
    Is there anything more to be done in the header
    injection,mail() function is returning true.
     
    elearnindia, Jun 25, 2010 IP
  7. MayurGondaliya

    MayurGondaliya Well-Known Member

    Messages:
    1,233
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    170
    #7
    There is nothing wrong in your code. If you receive the "message sent", then your script is definitely shooting the email out. But, due to your IP address reputation or due to the lack of SMTP authentication, gmail may be ignoring your email(considering it as spam.) I would suggest adding the SMTP authentication in the phpmailer script to send out emails. Kindly find the same code below.
    function send_mail($fromname, $from, $toname, $to, $subject, $body)
    {
    	require_once('class.phpmailer.php');
    	//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    	
    	$mail             = new PHPMailer();
    	
    
    	
    	$mail->IsSMTP(); // telling the class to use SMTP
    	$mail->Host       = ""; // SMTP server
    	$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
    											   // 1 = errors and messages
    											   // 2 = messages only
    	$mail->SMTPAuth   = true;                  // enable SMTP authentication
    	$mail->Host       = ""; // sets the SMTP server
    	$mail->Port       = ;                    // set the SMTP port for the GMAIL server
    	$mail->Username   = ""; // SMTP account username
    	$mail->Password   = "";        // SMTP account password
    	
    	$mail->SetFrom($from, $fromname);
    	
    	$mail->AddReplyTo($from, $fromname);
    	
    	$mail->Subject    = $subject;
    	
    	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    	
    	$mail->MsgHTML($body);
    	
    	
    	$mail->AddAddress($to, $toname);
    	
    	
    	
    	if(!$mail->Send())
    	{
    		return 0;
    	}
    	else
    	{
    		return 1;
    	}
    }
    PHP:
     
    MayurGondaliya, Jun 25, 2010 IP
  8. elearnindia

    elearnindia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I am using PHPMailer to send email through gmail account.Thanks langtusitinh225 for your kind advice.
     
    elearnindia, Jun 26, 2010 IP
  9. elearnindia

    elearnindia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes in PHP with mail() function I am getting return "message sent" as return value.Now I am using PHPMailer for sending
    the email.Thanks,exaspring for your kind advice and code.
     
    elearnindia, Jun 26, 2010 IP
  10. blackemerald

    blackemerald Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    maybe your message filtered as spam,
    because first, like others say that's a header like from and etc then you must fill all body message.
     
    blackemerald, Jun 26, 2010 IP
  11. sudip03

    sudip03 Peon

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    i have a simple question as i am new to php . What is the difference between using phpmailer function and using simply mail() function for sending mail. how phpmailer is efficient that mail() .
     
    sudip03, Jun 26, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    phpmailer() is more reliable, and convenient as it supports more options.

    whereas mail() is unreliable namely on shared hosting, many users of that hosting may use the function, if some of them abuse it and send spam..theirs a chance of your host being blacklist which could lead to the email being sent to the trash/spam folders.
     
    danx10, Jun 26, 2010 IP
  13. seongpinas316

    seongpinas316 Peon

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    try to use whitelist services, your mail server is the problem i think it was blacklisted by spam filters.
     
    seongpinas316, Jul 10, 2010 IP
  14. sanhit

    sanhit Peon

    Messages:
    318
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    if u need php form with captcha verification, PM me i can send u the code.
     
    sanhit, Jul 16, 2010 IP
  15. rix4real

    rix4real Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I need an INBOX PhP Mailer...rix3341(at)yahoo(dot)com
     
    rix4real, Feb 25, 2011 IP
  16. rix4real

    rix4real Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Can u help me with a PHP mailer?

    I need an inbox php mailer.
     
    rix4real, Feb 28, 2011 IP
  17. rix4real

    rix4real Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Please, I need a reliable INBOX Php mailer.

    Can you help?
     
    rix4real, Feb 28, 2011 IP
  18. coldcoder

    coldcoder Well-Known Member

    Messages:
    954
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    130
    #18
    your hosting server dis-abled function mail() so it stopped any outgoing email
    my hosting was the same till i contact them and told him i'm not using for spam
     
    coldcoder, Feb 28, 2011 IP
  19. rix4real

    rix4real Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I need a functional and reliable Inbox PHP mailer. Can you help?
     
    rix4real, Feb 28, 2011 IP
  20. rix4real

    rix4real Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I need a reliable Inbox PHP mailer. can you help me?
     
    rix4real, Feb 28, 2011 IP