SMTP-verbinding () Mislukte PHPMAILER – PHP

Ik ben nieuw voor PHP. Ik probeerde mezelf een voorbeeld-e-mail te sturen via Phpmailer. Ik gebruik de SMTP-server van Gmail. Ik probeer een voorbeeldmail van mijn Gmail-account naar mijn Yahoo-account te sturen. Maar ik krijg de foutmelding: Mailer Error: SMTP connect() failed.
Hier is de code:

<?php
require "class.phpmailer.php";
$mail = new PHPMailer(); 
$mail->IsSMTP();                              // send via SMTP
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true;                       // turn on SMTP authentication
$mail->Username = "[email protected]";        // SMTP username
$mail->Password = "mypassword";               // SMTP password
$webmaster_email = "[email protected]";       //Reply to this email ID
$email="[email protected]";                // Recipients email ID
$name="My Name";                              // Recipient's name
$mail->From = $webmaster_email;
$mail->Port = 465;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50;                         // set word wrap
$mail->IsHTML(true);                          // send as HTML
$mail->Subject = "subject";
$mail->Body = "Hi,
This is the HTML BODY ";                      //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

Ik gebruik WAMP Server op een Windows 7 64-bit machine. Wat kan de probleem zijn?
Help me alsjeblieft om dit op te lossen. Bedankt!


Antwoord 1, Autoriteit 100%

U moet de Hostparameter

toevoegen

$mail->Host = "ssl://smtp.gmail.com"; 

Controleer ook of je open_sslhebt ingeschakeld.

<?php
echo !extension_loaded('openssl')?"Not Available":"Available";

Antwoord 2, autoriteit 93%

De oplossing van dit probleem is eigenlijk heel eenvoudig. eigenlijk begint Google een nieuw autorisatiemechanisme voor zijn gebruiker te gebruiken.. je hebt misschien een andere regel in de foutopsporingsconsole gezien waarin je wordt gevraagd om in te loggen op je account met een willekeurige browser.! dit komt door het nieuwe XOAUTH2authenticatiemechanisme dat Google sinds 2014 gaat gebruiken.
onthoud.. gebruik de ssl over poort 465niet, maar ga voor tls over 587. dit komt alleen door het XOAUTH2-authenticatiemechanisme. als u ssl boven 465 gebruikt, wordt uw verzoek teruggestuurd.

wat je echt moet doen is .. log in op je Google-account en open het volgende adres
https://www.google.com/settings/security/lesssecureapps
en vink inschakelenaan. je moet dit doen om je verbinding te laten maken met de google SMTP omdat volgens het nieuwe authenticatiemechanisme google alle verzoeken van al die applicaties terugstuurt die geen standaard coderingstechniek volgen. Gaan..
hier is de code die prima werkte voor mij..

require_once 'C:\xampp\htdocs\email\vendor\autoload.php';
define ('GUSER','[email protected]');
define ('GPWD','your password');
// make a separate file and include this file in that. call this function in that file.
function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->SMTPAutoTLS = false;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}

Antwoord 3, autoriteit 71%

Een bijna identiek probleem opgelost door deze regels toe te voegen aan de standaard PHPMailer-configuratie. Werkt als een tierelier.

$mail->SMTPKeepAlive = true;   
$mail->Mailer = “smtp”; // don't change the quotes!

Ik kwam deze code tegen (van Simon Chen) terwijl ik hier een oplossing zocht, https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#comment-89


Antwoord 4, autoriteit 29%

Problemen oplossen

U heeft deze code toegevoegd:

$mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );

En inschakelen van minder veilige apps toestaan:
“zal meestal het probleem voor PHPMailer oplossen, en het maakt uw app niet echt aanzienlijk minder veilig. Naar verluidt kan het een uur of langer duren voordat het wijzigen van deze instelling van kracht wordt, dus verwacht geen onmiddellijke oplossing”

Dit werkt voor mij!


Antwoord 5, autoriteit 12%

Als iemand het probleem nog steeds niet kan oplossen, controleer dan de volgende thread en volg het antwoord van callmebob.

PHPMailer – SMTP-FOUT: wachtwoordopdracht mislukt bij het verzenden van e-mail vanaf mijn server


Antwoord 6, autoriteit 6%

Je mist de richtlijn waarin staat dat de verbinding SSL gebruikt

require ("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->SMTPAuth = true;     // turn of SMTP authentication
$mail->Username = "YAHOO ACCOUNT";  // SMTP username
$mail->Password = "YAHOO ACCOUNT PASSWORD"; // SMTP password
$mail->SMTPSecure = "ssl";
$mail->Host = "YAHOO HOST"; // SMTP host
$mail->Port = 465;

Voeg dan de andere delen toe

$webmaster_email = "[email protected]";       //Reply to this email ID
$email="[email protected]";                // Recipients email ID
$name="My Name";                              // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50;                         // set word wrap
$mail->IsHTML(true);                          // send as HTML
$mail->Subject = "subject";
$mail->Body = "Hi,
This is the HTML BODY ";                      //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

Als een kanttekening, ik heb problemen gehad om Body + AltBody samen te gebruiken, hoewel ze verondersteld worden te werken. Als resultaat heb ik de volgende wrapper-functie geschreven die perfect werkt.

<?php
require ("class.phpmailer.php");
// Setup Configuration for Mail Server Settings
$email['host']          = 'smtp.email.com';
$email['port']          = 366;
$email['user']          = '[email protected]';
$email['pass']          = 'from password';
$email['from']          = 'From Name';
$email['reply']         = '[email protected]';
$email['replyname']     = 'Reply To Name';
$addresses_to_mail_to = '[email protected];[email protected]';
$email_subject = 'My Subject';
$email_body = '<html>Code Here</html>';
$who_is_receiving_name = 'John Smith';
$result = sendmail(
    $email_body,
    $email_subject,
    $addresses_to_mail_to,
    $who_is_receiving_name
);
var_export($result);
function sendmail($body, $subject, $to, $name, $attach = "") {
  global $email;
  $return = false;
  $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
  $mail->IsSMTP(); // telling the class to use SMTP
  try {
    $mail->Host       = $email['host']; // SMTP server
//    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = $email['host']; // sets the SMTP server
    $mail->Port       = $email['port'];                    // set the SMTP port for the GMAIL server
    $mail->SMTPSecure = "tls";
    $mail->Username   = $email['user']; // SMTP account username
    $mail->Password   = $email['pass'];        // SMTP account password
    $mail->AddReplyTo($email['reply'], $email['replyname']);
    if(stristr($to,';')) {
      $totmp = explode(';',$to);
      foreach($totmp as $destto) {
        if(trim($destto) != "") {
          $mail->AddAddress(trim($destto), $name);
        }
      }
    } else {
      $mail->AddAddress($to, $name);
    }
    $mail->SetFrom($email['user'], $email['from']);
    $mail->Subject = $subject;
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
    $mail->MsgHTML($body);
    if(is_array($attach)) {
        foreach($attach as $attach_f) {
            if($attach_f != "") {
              $mail->AddAttachment($attach_f);      // attachment
            }
        }
    } else {
        if($attach != "") {
          $mail->AddAttachment($attach);      // attachment
        }
    }
    $mail->Send();
  } catch (phpmailerException $e) {
    $return = $e->errorMessage();
  } catch (Exception $e) {
    $return = $e->errorMessage();
  }
  return $return;
}

Antwoord 7, autoriteit 6%

Als alles mislukt, moet je voor Gmail de toegang tot apps van derden inschakelen om verbinding te maken met je Gmail-account.

https://www.google.com/settings/security/lesssecureapps/ / draai het
op


Antwoord 8, autoriteit 6%

Ik heb het opgelost …

https://github.com/PHPMailer/PHPMailer/tree/5.2-stable

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$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 = '[email protected]';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
//$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;                                    // TCP port to connect to
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'User');     // Add a recipient
$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';
}

Antwoord 9, Autoriteit 6%

Zorg ervoor dat u de juiste parameters hebt gepasseerd
bijv. De juiste uitgaande serverhost, gebruikersnaam en wachtwoord

$mail->SMTPDebug = false;
$mail->Host = 'email_smtp_host'
$mail->SMTPAuth = false
$mail->Username = 'username'
$mail->Password = 'password'
$mail->SMTPSecure 'tls'
$mail->Port = '587'

Antwoord 10, Autoriteit 6%

$mail->SMTPKeepAlive = true;   
$mail->isSendMail(); // instead of  isSMTP();

Op gedeelde hosting kan uw mailserver af en toe verbindingskwesties ervaren.
Hier heb ik oplossing gevonden

[bewerken]

Hier is de koppelingsinhoud, voor het geval de link met een of andere reden werkt (het gebeurt)

PHPMaileren SMTPAAN 1AND1 Gedeelde hosting

Ik gebruik PHPMailerop mijn 1and1 Gedeelde hostingaccount. Onlangs kon ik geen e-mail meer verzenden.

Ik heb geprobeerd foutopsporing en dit is de fout die PHPMailergooit:

Taalreeks is mislukt om te laden: Connect_Host

Na Googling voor een oplossing en verschillende SMTP-servers, accounts en SMTP-poorten proberen, besloot ik om over te schakelen naar sendmailEn het werkte als een charme! Alles wat ik moest doen, was om $mail->isSmtp()te vervangen door:

$mail->isSendMail()

sendmailbevindt zich op de standaardlocatie op 1and1 Servers: /usr/sbin/sendmail, dus er was geen verandering van instellingen vereist.

Conclusie:

1and1 Sluit waarschijnlijk de uitgaande SMTP-poorten op de gedeelde hostservers. Als u PHPMailergebruikt, gebruikt u SMTP-modus niet meer.


Antwoord 11

Als u VPS en met HTTPD-service gebruikt, controleer dan of uw HTTPD_CAN_SENDMAIL AAN is.

getsebool -a | grep mail

om in te stellen

setsebool -P httpd_can_sendmail on

Antwoord 12

Probeer deze regel toe te voegen aan uw script. Dit werkte voor mij!

$mail->Mailer = “smtp”;


Antwoord 13

Dit is meestal een resultaat van de server die geen SSLV2- of SSLV3-aansluitingen accepteert, wat een standaard is voor CPANEL / WHM ten gunste van TLS alleen verbindingen. U kunt dit controleren door naar WHM & GT; & GT; Service Configuration & GT; & GT; Exim Configuration Manager – & GT; Opties voor OpenSSL


Antwoord 14

$mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );

Antwoord 15

<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once "PHPMailer/src/Exception.php";
require_once "PHPMailer/src/PHPMailer.php";
require_once "PHPMailer/src/SMTP.php";
$mail = new PHPMailer();
$mail ->isSMTP();
$mail ->Host ="smtp.gmail.com";
$mail -> SMTPAuth = true;
$mail ->Username = '[email protected]';
$mail ->Password = 'password';
//$mail ->SMTPSecure=PHPMailer::ENCRYPTION_STARTTLS;
$mail ->Port = '587';
$mail ->SMTPSecure ='tls';
$mail ->isHTML(true);
$mail ->setFrom('[email protected]','email send name');
$mail ->addAddress('[email protected]');
$mail ->Subject = 'HelloWorld';
$mail ->Body = 'a test email';
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
if ($mail->send()){
    echo "Message has been sent successfully";
}else{
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

zorg ervoor dat u minder veilige app-instellingen in Gmail configureert met behulp van deze link https://www .google.com/settings/security/lesssecureappsen zorg ervoor dat je PHPMailer-master.zip hebt gedownload van github

werkt prima met MAMP-server lokaal 🙂

Other episodes