PHP email sending functionality is malfunctioning

Please fill in the following details:

Full Name

Answer №1

Ensure that your HTML form includes the opening form tag at the beginning. The form should start with <form method="post" action="mail.php">

In the mail.php file, include the following script:

$fullname = $_POST['fullname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$department = $_POST['department'];
$consult = $_POST['consult'];
$date = $_POST['date'];
$request = $_POST['request'];
$time = $_POST['time'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c2dfc6cad7cbc2e7c2cac6cecb89c4c8ca">[email protected]</a>";
$subject = "message goes here";
$message="Name: ".$fullname."<br/>Age: ".$age,"<br/>Gender",$gender."<br/>Phone".$phone."<br/>Dept:".$department;
if(mail ($to, $subject,$message, "From: " .$fullname)){
    echo "Success";
} else {
    echo "not sent";
}

Make sure your PHP mail server is properly configured.

If testing on localhost on a Windows/Linux machine, configure SMTP settings to enable mail sending. For testing on a live server, contact your service provider to enable mail sending.

Answer №2

If you haven't specified your environment, it may be worth trying to send the mail from the command line to see if it goes through. It's possible that your server is unable to use PHP mail. Consider using SMTP mail as an alternative option. Check out this reliable mail class library: https://github.com/PHPMailer/PHPMailer. Hopefully, this suggestion proves helpful.

Answer №3

If you want to properly receive and reply to emails from your server, make sure to add the From address and a Reply-to header. It seems that instead of having a From address on your server, you have the sender's full name from the form.

To update the message part as suggested by @nana-partykar, replace $message with:

    $message="Name: ".$fullname."<br/>Age: ".$age,"<br/>Gender",$gender."<br/>Phone".$phone."<br/>Dept:".$department;

    $your_email = 'your_email@your_server.com';
    $headers = "From: $your_email" . "\r\n";
    // Additional headers
    $headers .= "Reply-To: $email" . "\r\n";
    if(mail($to,$subject,$message,$headers)){
    echo "Success";
    } else {
    echo "not sent";
    }

You can also set the sendmail user at the beginning of your script:

    ini_set('sendmail_from', 'your_email@your_server.com'); 

For validation purposes, clean up the submitted email to allow only letters, numbers, and certain characters:

    $email = preg_replace("[^a-zA-Z0-9@._-]",'',$_POST['email']);

Ensure the email address is not empty before sending the mail:

    if($email != ""){

    if(mail....to the end

    }

When storing received information in a database, be cautious of SQL injection attacks. Refer to this link on how to prevent them:

How can I prevent SQL injection in PHP?

It's important to get everything working correctly before implementing security measures!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please include the document with a name that contains spaces

I am facing an issue where I cannot attach files with spaces in the name. However, when a file with no space in the name is successfully attached. I am using CodeIgniter for this purpose, uploading the file to the server before attaching it. I use the help ...

issue with eval() function

I am attempting to convert a JSON string from my .php file using the eval() function, but it is not working. The browser console shows a SyntaxError: expected expression, got '<'... However, when I comment out the line where eval() is used an ...

Incorporate Live Data into Google Charts Using Ajax Response for a Dynamic Visualization

I am struggling to successfully load a responsive Google Line Chart after an Ajax call. I have attempted to place the entire Google Chart code within the success part of the Ajax call, but it does not seem to work as expected. Below is my current Ajax code ...

Executing JavaScript function by clicking on <img>

I've been developing a website similar to YouTube, and I'm facing difficulties with the Like/Dislike feature for comments. Currently, I have implemented it in the following way: when a user clicks on an image (thumbsUp.png or thumbsDown.png), a ...

Convert text into a clickable link

Creating a form with numerous text fields, some of which require numerical input. The main goal is to have users enter a tracking number, order number, or any other type of number that, when submitted, will open a new URL in a separate window with the spec ...

Fluctuating updated site (ajax)

What method do you recommend for maintaining the data in a table on a page current? Currently, I am using a timer that connects to the server via ajax every 2 seconds to check for updates. Is there a way to trigger an event or function only when the cont ...

Retrieving Information from Ajax Response Following a Successful Insert Query in Codeigniter

I am trying to use ajax method to insert form data into a database and then redirect it to the next page. I have successfully passed the data in ajax and inserted it into the database table. However, I am facing an issue with getting the generated referenc ...

Transmit a data point from JavaScript to PHP

I am looking to transfer the address value to a different PHP page <script> var userAddress = place.formatted_address; document.getElementById('af').innerHTML = userAddress; </script> ...

utilize jQuery to load webpage with an HTML dropdown element

Querying the Campaigns: // Getting the campaigns $campaigns = $wpdb->get_results( "SELECT * FROM tbl_campaigns ORDER BY campaignID DESC", OBJECT_K ); // Displaying the Cam ...

Issues with sending an AJAX POST request to a PHP script

Hello, I am trying to send a variable from an AJAX JavaScript file to a PHP file. Here is what I have done so far: var request = createRequest(); var deletenode = node.id; window.alert("nodeid=" + deletenode); var vars = "deletenode ...

Utilizing Bootstrap Modal to Display PHP Data Dynamically

Modals always pose a challenge for me, especially when I'm trying to work with someone else's code that has a unique take on modals that I really appreciate (if only I can make it function correctly). The issue arises when the modal is supposed ...

Unexpected error 500 (Internal Server Error) occurred due to BadMethodCallException

Using Vue 2.0 and Laravel 5.4, I am working on creating a matching system that includes a dynamic Vue component. For example, when someone likes another person, it should immediately show that the like has been sent or if the like is mutual, it should indi ...

Using jQuery to dynamically insert a table row with JSON data into dropdown menus

I'm working on a web form that includes a table where users can add rows. The first row of the table has dependent dropdowns populated with JSON data from an external file. Check out the code snippet below: // Function to add a new row $(function(){ ...

JavaScript doesn't automatically redirect after receiving a response

Hey there, I'm attempting to implement a redirect upon receiving a response from an ajax request. However, it seems that the windows.href.location doesn't execute the redirect until the PHP background process finishes processing. Check out my cod ...

Since switching to PHP 5.5 from version 3.x, I have noticed that it is attempting to interpret my JavaScript comment within a script tag in a PHP include file

After a long break from working with PHP, I recently encountered an issue with an older website I built using PHP and the include function. The site was functioning perfectly until the web host updated PHP to version 5.5, causing a strange bug where it see ...

Utilizing JSON for Google Charts

Although I have no prior experience with Google Charts, I am currently attempting to graph temperature data collected from sensors placed around my house. Unfortunately, I keep encountering an Exception error. I suspect the issue lies in the JSON format no ...

Guide on automatically attaching a file to an input file type field from a database

Currently, I am implementing a PHP file attachment feature to upload files. Upon successful upload, the system stores the filename with its respective extension in the database. The issue arises when trying to retrieve and display all entries from the ...

What is the process for sharing your website content on Google Blogger?

I am currently developing a dashboard for a small business website and I would like to implement a feature that allows users to post directly to their Blogger blog from the dashboard of their own website. This eliminates the hassle of having to switch betw ...

Add the AJAX response to the dropdown menu options

Currently in my project, I am utilizing Laravel as a backend. The scenario is such that once the corresponding page loads, it triggers an ajax request to fetch vehicle data which consists of vehicle model and plate number properties. My aim is to display t ...

Deleting an element from HTML using jQuery

In the midst of creating a system that allows users to construct their own navigation structure, I have encountered a stumbling block. The idea is that when a user lands on the site, they are presented with a list of available topics from which they can ch ...