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

Fran_3

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!!!

PHP:
<?php
$msg = "First line of text\nSecond line of text";
$SentBy = "abc@gmailX.com";
$SentTo = "xyz@gmailX.com";

$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 = "abc@gmailX.com";
$SentTo = "xyz@gmailX.com";

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

try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your_email@gmail.com'; // 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

//Recipients
$mail->setFrom($SentBy);
$mail->addAddress($SentTo);

// Content
$mail->isHTML(true);
$mail->Subject = 'My subject';
$mail->Body = $msg;

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Make sure to replace 'your_email@gmail.com' 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.
 
Hello,

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

Code:
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->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'example@gmail.com';
    $mail->Password = 'Password';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    // Recipients
    $mail->setFrom('correo@gmail.com', 'my name');
    $mail->addAddress('destination@correo.com');

    // 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 = '';
    $mail->send();
}
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 https://github.com/PHPMailer/PHPMailer and update the necessary credentials there.

I hope this helps you.
 
Back
Top