简体   繁体   English

无法使用PHP连接到SMTP

[英]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 . 我试图与另一台主机发送电子邮件,并且每次收到此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 此代码为php邮件

$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. 那么端口是:tls = 587,ssl = 465。

Add the mail.php use the below code it's working fine for me... 添加mail.php使用下面的代码,对我来说很好用...

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() ; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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