简体   繁体   中英

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.

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 ?吗? The cause of error may be more than one reason.错误的原因可能不止一个。 when you try to send large emails and your PHP error log contains the message then your mail transfer agent (Sendmail, postfix, Exim, etc) is refusing to deliver these emails.您的邮件传输代理(Sendmail、postfix、Exim 等)拒绝发送这些电子邮件。

The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication), consider the given example.

$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

You are using older version of PHPMailer also let me help you by typing the correct one

You should download from PHPMailer GitHub Not vendor for this example

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.

sudo apt-get install postfix

It worked for me on digital ocean.

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