Search code examples
phpphpmailer

PHP Mailer error: Message could not be sent.Mailer Error: SMTP connect() failed


Here is my code:

require 'phpmailertesting/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'send.one.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'myemailhidden';                 // SMTP username
$mail->Password = 'mypasswordhidden';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also     accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->From = 'myemailhidden';
$mail->FromName = 'My Name';
$mail->addAddress('[email protected]');               // Name is optional

$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

I have tried changing the port and the type of secure connection to "TSL" and "SSL" and nothing. Ive looked at the answers already and none of them solve it. Any answers? Thanks

I enabled the SMTP debugger and this is what it said "Connection: opening to ssl://send.one.com:465, t=300, opt=array ( ) 2014-12-15 15:46:40 SMTP ERROR: Failed to connect to server: Connection timed out (110) 2014-12-15 15:46:40 SMTP connect() failed"


Solution

  • Your hosting company, one.com, blocks outgoing mail ports intentionally to restrict malicious PHP scripts. The send.one.com address is meant for external mail clients such as your mobile phone, email client, etc. and not for internal mailing scripts from your website.

    As per their support document concerning sending emails from your website, you must change the host to their internal SMTP address, mailout.one.com - since this is an internal relay, you must also use port 25 and disable any security such as TLS or SSL. You must also disable authentication.

    Here is the correct configuration:

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'mailout.one.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = false; // Authentication must be disabled
    $mail->Username = 'myemailhidden';
    $mail->Password = ''; // Leave this blank
    $mail->Port = 25;                                    // TCP port to connect to