Unable to understand why am I not sending mail to my host mail using PHP mail() function [duplicate]


April 2019


66 time


This question already has an answer here:

All I'm trying to do is just send an extremely basic e-mail to my host mail. The hosting service I'm currently using is https://www.ionos.com/ and I have a registered email with it. My website is already hosted and running. The code I'm trying to execute is this:


    <form class="" action="/contactform.php" method="post">
        <input type="text" name="name" value="">
        <input type="text" name="mail" value="">
        <textarea name="message"></textarea>
        <button type="submit" name="submit">Submit</button>


if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $mail = $_POST['mail'];
    $message = $_POST['message'];

    $mailTo = '[email protected]';
    $headers = 'From '.$mail;
    $text = "You've received an e-mail from ".$name."\n\n".$message;

    mail($mailTo, $name, $text, $headers);
    header("Location: ../index.php");

The e-mail I'm trying to send the message is [email protected] as is seen in the code. Sadly I'm not receiving anything and I don't have any clue as to why. The PHP code is not giving any errors.

2 answers


Quite likely, you need a registered account with authentication. But the mail() function does not allow such authentication.

You probably want to install a suitable class such as PHPMailer. Simply supply the instance with your authentication information for your mail service, and you should be good.

If it still does not work, you can use the debug mode of PHPMailer to investigate. If you find yourself in such a situation, chances are that your Web provider does not allow emails to be sent by a PHP process, probably to avoid abuse and spamming. In that case, you need to talk to the provider and have the function enabled, so that the mail() function will work, and possibly will need to add a custom header to identify the email sender to your Web provider for accounting/monitoring purposes. As an alternative, they might supply you with a user/password pair suitable for use with PHPMailer.


Try this:

$headers = 'From: '.$mail;

mail($mailTo, $name, $text, $headers, "-f ".$mail);