PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] How can I get this PHP code to send emails?

 January 30, 2022     email, php, wordpress     No comments   

Issue

EDIT: Turns out that my problem was caused by hackers flooding my email server and that is why I wasn't receiving emails. I inserted a captcha on my form and it began working.

I have a website that uses a custom php file to submit email leads.

When someone fills out the contact form, it shows the success message, but the email never arrives in my inbox.

Here is the HTML code:

<form name="contactform" method="post" action="php/send_form_email.php">
    <fieldset>
        <label for="first_name">First Name<br>
            <input type="text" class="llonger" name="first_name" maxlength="50" size="30" placeholder="John">
        </label>
        <label for="last_name">Last Name<br>
            <input type="text" name="last_name" maxlength="50" size="30" placeholder="Smith">
        </label>
        <label for="email">Email Address<br>
            <input type="email" name="email" maxlength="80" size="30" placeholder="my@email.com">
        </label>
        <input type="submit" value="Submit" class="submitlonger">
      </fieldset>
    </form>

This is the PHP code:

<?php
if(isset($_POST['email'])) {
    $email_to = "email@website.com";
    $email_subject = "Contact Submission";
    function died($error) {
       echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo $error;
        die();
    }
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $email_message = "Form details below.\n\n";
 
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
    }
?>

Solution

looks like you need to wrap your mail function in a conditional...

if(mail($email_to, $email_subject, $email_message, $headers)){
  //display success...
}else{
  //display fail...
}

this will suppress the erroneous 'success message'..

the mail function returns true/false but true doesn't necessarily mean it worked if it returned true... see here... http://php.net/manual/en/function.mail.php

also, if it were me I'd remove the "@" at the begining of the mail function, so that you can catch any possible syntax, or other errors that may be preventing it from working...

Edit: in context example...

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

if(mail($email_to, $email_subject, $email_message, $headers)){ 
 ?>
  <!-- include your own success html here -->
  Thank you for contacting us. We will be in touch with you very soon.
 <?php
}else{
 ?>
  <!-- fail html -->
  Email could not be sent...
 <?php
}

EDIT2: pure php...

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

if(mail($email_to, $email_subject, $email_message, $headers)){ 
  echo 'Thank you for contacting us. We will be in touch with you very soon.';
}else{
  echo 'Email could not be sent...';
}


Answered By - jsuna
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing