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: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!