Register Login

PHPMailer: Called Mail() without being connected error

While working with PHPMailer class, you might encounter an error –

Mailer error: The following From address failed: abc@gmail.com: Called Mail() without being connected.

This error occurs when you are using an SMTP relay service to send your emails. In this post, we will learn all about this problem and its resolution.

What causes the error?

Let us look at the reasons for this error.

Incorrect SMTP settings

There can be a possibility that the settings you are using for sending emails are incorrect. Incorrect SMTP settings in Gmail can lead to this error. Here are the correct settings to send an email –

function send_external_mail($to, $from, $subject, $message, $cc = null, $fromName = true, $Precedence = null, $attachment = null) {
global $dev;
if (!empty($dev))
return;

$mail = new PHPMailer();

$mail->ClearAllRecipients();

$mail->SMTPKeepAlive = false;
$mail->From = ''info@myemail.com';
if (!empty($Precedence)) {

$mail->AddCustomHeader('Precedence:' . $Precedence);

$mail->Priority = 1;
}
if ($fromName === false) {

$mail->FromName = My Name';
} else {

$mail->FromName = 'My Name';
}
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Mailer = 'smtp';

$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@myemail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
// TCP port to connect to

if (is_array($to)) {
foreach ($to as $to_id) {

$mail->AddAddress($to_id, $to_id);
}
} else {

$mail->AddAddress($to, $to);
}
if (!empty($cc) && is_array($cc)) {
foreach ($cc as $cc_id) {

$mail->AddCC($cc_id, $cc_id);
}
} else if (!empty($cc)) {

$mail->AddCC($cc, $cc);
}
if (!empty($attachment) && is_array($attachment)) {
foreach ($attachment as $filepath) {

$mail->AddAttachment($filepath);
}
} else if (!empty($attachment)) {

$mail->AddAttachment($attachment);
}
if ($mail->Send()) {
return TRUE;
} else {
return FALSE;
}
}

The SMTP Port is blocked

If you are looking at a “Called Mail() without being connected“ error on your screen, it means that the hosting may have blocked the outgoing connection. This happens as some hosting providers block some ports. Others may block outgoing PHP socket connections, which causes this error to pop up.

In our case, we tried to send an email via our Gmail id using the PHPmailer class and received the error.

How to Fix the Error?

Solution 1: Open SMTP Port

Ask your hosting provider to open an SMTP port or open an SMTP port by enabling the fsockopen() command. Notify your provider about the error and give them your SMTP host, protocol name, and port. This will help them to enable the outgoing socket connection.

Solution 2: Use Gmail API to Send Email

Use the link given below to configure the Gmail API to send an email –

https://developers.google.com/gmail/api/guides/sending

Conclusion

The steps mentioned above will help you solve the error. Additionally, you can also check whether the outgoing mail port is open in your firewall.