Hullo, I have been browsing through the internet on how best I can send an email from my website; and it seems like most were directing me to PHPMailer.
I added the php mailer folder onto my server, and now am attempting to test whether am email can really be sent. However, I keep getting this error message;

Message was not sent.Mailer error: The following From address failed: sample_mail@gmail.com : Called Mail() without being connected;

Here i sthe code I am using to send the email;

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "smtp.gmail.com"; // SMTP server
$mail->From     = "samle1@gmail.com";
$mail->AddAddress("sample2@rocketmail.com");
$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

Can someone help me out on how to solve that error?

Dani AI

Generated

The error "Called Mail() without being connected" means PHPMailer never completed an SMTP handshake. In your first snippet you pointed at smtp.gmail.com but did not enable TLS, a port, or authentication; in the later snippet you switched to Host "localhost" on port 25, which likely worked only because your server’s local MTA accepted and relayed the message (not Gmail). For Gmail, you must authenticate and use TLS on the correct port, and the From should be your Gmail address (or a verified alias) to avoid SPF/DMARC issues.

Try a minimal Gmail setup (PHPMailer 5.x style), then enable debug only while testing:

require 'PHPMailer_5.2.4/class.phpmailer.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host       = 'smtp.gmail.com';
$mail->Port       = 587;          // or 465 with 'ssl'
$mail->SMTPSecure = 'tls';        // or 'ssl' for 465
$mail->SMTPAuth   = true;
$mail->Username   = 'yourname@gmail.com';
$mail->Password   = 'your-app-password';
$mail->setFrom('yourname@gmail.com', 'Your Name'); // or a verified alias
$mail->addAddress('recipient@example.com');
$mail->Subject    = 'Test email';
$mail->Body       = 'Hello world';
$mail->SMTPDebug  = 2;            // remove in production

Notes:

  • As of May 30, 2022, Gmail no longer allows "less secure apps". Use 2-Step Verification with an App Password or OAuth2. See and .
  • If port 587/465 is blocked by your host, use their SMTP server or an email API. See PHPMailer docs at GitHub.

Recommended Answers

All 4 Replies

gmail does not use the standard smtp ports and from needs to be your existing email address. Then you need to use authentication and provide your password. There are several examples already in this forum.

@Pristaeas, So; what do I replace?? and with what??

I'm sorry, but it is the first time I am attempting to send an email from my website.

Thanx , I followed the link you gave,and it gave me an idea, although did not solve my dilema.
However, I was able to get it to work. Here is the code I used [In case someone else faces the same challenge I faced];

require("PHPMailer_5.2.4/class.phpmailer.php");
    $email = $_POST['email'];
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Host = "localhost";
    $mail->From = "sender@domain.com";
    $mail->FromName  =  "My name";
    $mail->AddAddress("recipient_1@domain.com");
    $mail->AddAddress("recipient_2@domain.com");
    $mail->SMTPAuth = "true";
    $mail->Username = "mygmailemail@gmail.com";
    $mail->Password =  "mygmailpassword";
    $mail->Port  =  "25";

    $mail->Subject = "Test email";
    $mail->Body = "" This is a test email.;
    $mail->WordWrap = 50;

    if(!$mail->Send())
    {
        echo 'Message was not sent.';
        echo 'Mailer error: ' . $mail->ErrorInfo;
    }
    else
    {
        echo 'Email has been sent!!';
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.