mail(to,subject,message,headers); Not Working. H E L P !


New member
I've been chasing this for a few days and really need some kind soul to help me out.

In a "Contact Form" I'm trying to allow the user to email the system admin.
Both the SentBy email and SentTo email addresses are actually gmail addresses...
but shown as gmailX in the example below.

I've Googled and read many threads but I guess I haven't stumbled on the right one so any help would be greatly appreciated.

Why doesn't the code below work?
It seems to run on the GoDaddy Linux Server but the email never arrives at the SentTo address.

Thanks for any help!!!

$msg = "First line of text\nSecond line of text";
$SentBy = "";
$SentTo = "";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <'.$SentBy.'>' . "\r\n";
// send email
mail($SentTo,"My subject",$msg,$headers);
Here's an updated version of your code that includes error handling and uses PHPMailer for sending emails (if SMTP configuration is required):<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php'; // Adjust the path to autoload.php as needed

$msg = "First line of text\nSecond line of text";
$SentBy = "";
$SentTo = "";

// Create a new PHPMailer instance
$mail = new PHPMailer(true);

try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->Host = ''; // SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = 'your_password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above


// Content
$mail->Subject = 'My subject';
$mail->Body = $msg;

echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
Make sure to replace '' and 'your_password' with your actual Gmail address and password. Also, ensure that you've installed PHPMailer library through Composer, and adjust the path to autoload.php accordingly.

I am experiencing the same problem. please SMTP to send mail.

require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';
require_once 'PHPMailer/Exception.php';

$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 0;
    $mail->Host = '';
    $mail->SMTPAuth = true;
    $mail->Username = '';
    $mail->Password = 'Password';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    // Recipients
    $mail->setFrom('', 'my name');

    // Attachments
    $mail->addAttachment('optional file');         // Add files, is optional

    // Content
    $mail->isHTML(true);// Set email format to HTML
    $mail->Subject = utf8_decode("subject");
    $mail->Body    = utf8_decode("mail content");
    $mail->AltBody = '';
catch (Exception $e) {
    $error = $mail->ErrorInfo;

Composer may be used to add libraries, or you can get them straight from the GitHub library path at and update the necessary credentials there.

I hope this helps you.