简体   繁体   English

当我尝试使用 phpmailer 发送邮件时,出现错误

[英]When i tried to send mail using phpmailer, i am getting error

Could not instantiate mail function.无法实例化邮件功能。
Message couldnot be sentMailer Error: Could not instantiate mail function.无法发送邮件Mailer 错误:无法实例化邮件功能。

require 'PHPMailer/PHPMailerAutoload.php';
$mail =new PHPMailer;
$mail->HOST ='localhost';
$mail->PORT = 25;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'Username';
$mail->Password = 'Pass';
$mail->setFrom('info@zoeticpolymers.com');
$mail->addAddress('nitesh54546@gmail.com','Nitesh');
$mail->AddReplyTo("info@zoeticpolymers.com");
$mail->isHTMl(true);
$mail->Subject = 'PHP Mailer Subject';
$mail->Body = '<h1>You are Welcome Here.....</h1>';
if(!$mail->send()){
    echo 'Message couldnot be sent'; 
     echo 'Mailer Error: ' . $mail->ErrorInfo; die;
}else{
    echo 'Message has been sent'; die;
}

Have you tried with smtp ?你试过smtp吗? The cause of Could not instantiate mail function error may be more than one reason.导致无法实例化邮件功能错误的原因可能不止一个。 when you try to send large emails and your PHP error log contains the message Can not send a message: Too big then your mail transfer agent (Sendmail, postfix, Exim, etc) is refusing to deliver these emails.当您尝试发送大型电子邮件并且您的 PHP 错误日志包含消息无法发送消息:太大时,您的邮件传输代理(Sendmail、postfix、Exim 等)拒绝发送这些电子邮件。

The solution is to configure the MTA to allow larger attachments.解决方案是配置 MTA 以允许更大的附件。 But this is not always possible.但这并不总是可能的。 The alternate solution is to use SMTP.另一种解决方案是使用 SMTP。 You will need access to a SMTP server (and login credentials if your SMTP server requires authentication), consider the given example.您将需要访问 SMTP 服务器(如果您的 SMTP 服务器需要身份验证,则需要登录凭据),请考虑给定的示例。

$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// used only when SMTP requires authentication  
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';

Your Mistake: You used HOST as Localhost it's SMTP.mail.com change mail to your server您的错误:您使用 HOST 作为本地主机,它的 SMTP.mail.com 将邮件更改为您的服务器

You are using older version of PHPMailer also let me help you by typing the correct one您正在使用旧版本的 PHPMailer 也让我通过输入正确的来帮助您

You should download from PHPMailer GitHub Not vendor for this example您应该从 PHPMailer GitHub Not vendor 下载此示例

use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                     
    $mail->isSMTP();                                   
    $mail->Host = 'smtp.gmail.com';     //for Gmail               
    $mail->SMTPAuth   = true;             

    $mail->Username   = 'user@gmail.com';                   
    $mail->Password   = 'your Gmail pass';              


    $mail->Port       = 587;                                    // TCP port


    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');    


    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}```




**Your error was that you used HOST as localhost and older version of PHPMailer.


But use default mail() but change the PHP version to 7.3 since it's better now.**

If your code looks good, try to install PostFix on your server.如果您的代码看起来不错,请尝试在您的服务器上安装 PostFix。

sudo apt-get install postfix

It worked for me on digital ocean.它在数字海洋上对我有用。

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

相关问题 我无法使用PHPMailer发送邮件 - I am unable to send mail with PHPMailer 为什么我在Phpmailer中收到邮件延迟? - Why i am getting mail delayed in Phpmailer? 为什么我在尝试使用phpmailer发送电子邮件激活新用户帐户时收到此错误 - Why am i getting this error when trying to send email for activation of new user account with phpmailer 我正在使用 PHPMailer,但在 gmail 中未收到邮件 - I am using PHPMailer but mail not received in gmail phpmailer我没有任何附件,但是我收到邮件 - Phpmailer I am not getting any attachment, but i am receiving mail 我无法使用PHP邮件功能发送邮件...我收到错误消息 - I can't send mail using PHP mail function… I am getting an error phpmailer 在本地主机上工作,但是当我将它上传到我的 ubuntu 服务器时,它给了我错误并且邮件没有发送(我使用的是 gmail smtp) - phpmailer working on localhost but when i upload it on my ubuntu server it gives me error and mail is not sending(I am using gmail smtp) 我无法在yii 2.0中发送带有帮助的邮件哦PHPMailer - i am unable to send mail with help oh PHPMailer in yii 2.0 我试图为我的数据库播种时出错 - I am getting an error when i tried to seed my database 当我尝试为电报设置 webhook 时出现错误 - i am getting an error when i tried to set webhook for telegram
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM