简体   繁体   中英

Unable to connect to SMTP using PHP

I am trying to send an email with another host and every time I got this error-> Mailer Error: SMTP connect() failed . Here is my code

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->Debugoutput = 'html';

    $mail->Host = ' host IP '; 
    $mail->Port = 587;
    $mail->SMTPSecure = 'ssl'; // I had also tried with TLS

    $mail->SMTPAuth = true;
    $mail->Username = "my@username.com";
    $mail->Password = "password";

    $mail->setFrom('my@username.com', 'MY Personal');

    $mail->addAddress($To);

    if($MoreAddresses != "")
    {
        foreach($MoreAddresses as $Address)
        {
            $mail->addAddress($Address);
        }
    }

    $mail->Subject = $Subject;
    $mail->msgHTML($Body);
    $mail->AltBody = 'This email contains HTML contents.';
    if($MoreAddresses != "")
    {
        foreach($MoreAddresses as $Address)
        {
            $mail->addAddress($Address);
        }
    }

    $mail->Subject = $Subject;
    $mail->msgHTML($Body);
    $mail->AltBody = 'This email contains HTML contents.';

    if (!$mail->send()) 
    {
        return "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
        return "Message sent!";
    }

I had tried with many of question but couldn't help me well.

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail@gmail.com";
$mail->Password = "**********";
$mail->Port = "465";

That is a working configuration. try to replace what you have

This code for php mail

$to = "".$_REQUEST['txtphp_to']."";
$subject = "Nullam id dolor id nibh ultricies vehicula.";
$txt = "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.
Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam id dolor id nibh ultricies vehicula.";
$headers = "From: $s_emailid" . "\r\n";// .
if(mail($to,$subject,$txt,$headers))
{
echo "done";
}
else
{
echo "error";
}

When you use

$mail->Port = 587;
$mail->SMTPSecure = 'ssl';

then the ports are: tls=587 , ssl=465.

Add the mail.php use the below code it's working fine for me...

include "../Mail/Mail.php";

$from = "test<emai>"; 
$to = $toname."<".$tomail.">"; 
$subject = $subject;
$body =$body; 
$host ="ssl://smtp.gmail.com";
$port = "465"; 
$username = "test@gmail.com"; 
$password = "123456"; 
$headers['From'] =$from;
$headers['To'] = $to;
$headers['Bcc'] = $from;
$headers['Subject'] = $subject;
$headers['Reply-To'] = 'no-replay<no-replay>';
$smtp = Mail::factory('smtp',   
array ('host' => $host,     'port' => $port,   
'auth' => true,     'username' => $username,     'password' => $password)); 
$mail = $smtp->send($to, $headers, $body); 
if (PEAR::isError($mail)) 
{  
  $mail->getMessage() ; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM