简体   繁体   English

为什么我不能使用 PHPMailer 发送电子邮件?

[英]Why can I not send email using PHPMailer?

Here is my code.这是我的代码。 I really need to know what is wrong with my code.I am getting this error:- "SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting "我真的需要知道我的代码有什么问题。我收到此错误:- “SMTP 连接()失败。https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

<?php
require_once 'PHPMailer/class.phpmailer.php';
    //sending mail for verification
    $mail = new PHPMailer();

$mail->IsSMTP(); 
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; 
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password


$mail->From = "support@mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid@yahoo.com", "Name");

$mail->AddReplyTo("myemail@gmail.com", "Information");




$mail->IsHTML(true);                                  

$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 "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

My credentials are correct.I did follow the link in the error message and tried almost everything mentioned there but nothing seems to work for me.我的凭据是正确的。我确实按照错误消息中的链接进行了操作,并尝试了那里提到的几乎所有内容,但似乎没有任何效果对我有用。 This is the shortest and trimmed version of my original code, the idea is to tell that even this is not working for me.这是我原始代码的最短和修剪版本,这个想法是告诉我即使这对我也不起作用。

I found the solution my this problem.我找到了解决这个问题的方法。 Here are the changes I made and it works perfectly fine.这是我所做的更改,它运行良好。

<?php
require_once 'PHPMailer/class.phpmailer.php';
require_once 'assets/import/PHPMailer/class.smtp.php'; //I added this here which wasn't added in original script
    //sending mail for verification
    $mail = new PHPMailer();

$mail->IsSMTP(); 
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; 
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password


$mail->From = "support@mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid@yahoo.com", "Name");

$mail->AddReplyTo("myemail@gmail.com", "Information");

// Adding SMTPOption is the main thing that solved my problem. I got this from troubleshooting page of PHPMailer. And they
// recommended to do this only if every other option fails. 

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


$mail->IsHTML(true);                                  

$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 "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

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

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