Вопрос

I am trying to use the following code to send an email from a WordPress plugin

include_once(ABSPATH . WPINC . '/class-phpmailer.php'); 
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->ContentType = 'text/plain'; 
            $mail->IsHTML(false);
            $mail->SetFrom(($enquiry_informations['display_email_address']!="")?$enquiry_informations['display_email_address']:"", "Autoquote");
            $mail->AddAddress($customer_email_address, $customer_email_name);
            $mail->Subject = $enquiry_informations['enquiry_autoresponse_subject'];
            $mail->Body = $autoresponse_msg;
            if($enquiry_informations['enquiry_autoresponse_attachment']!==NULL&&$enquiry_informations['enquiry_autoresponse_attachment']!==""){
                $mail->addAttachment(plugin_dir_path(__FILE__) . "attachments/" . $enquiry_informations['enquiry_autoresponse_attachment']);
            }
            $info = $mail->Send();
        if($info){
        echo "Sent";
}else{
    echo "Failed";
    echo $mail->ErrorInfo;
}

however, I receive the following error: The following From address failed: root@localhost : Called Mail() without being connected. I did a little bit of googling and found out it could be something to do with protocol. This is from a wordpress plugin so I would like the code to be flexible(so that it can be used anywhere. So the varying protocol cannot get into the way.)

Это было полезно?

Решение

You did not pass all necessary values when using it. When you use isSMTP() method you have to provide the following information also:

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

Read here how to use the class properly.

Другие советы

I think the best way around is to use the mail helper within Wordpress.

http://codex.wordpress.org/Function_Reference/wp_mail

The error is probably caused because you are missing the right SMTP configuration, I don't know and couldn't find how you can adapt the Wordpress configuration though..

Hoping this helps you out a little!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top