0

I've been working on a contact email form on my own localhost in wordpress before I move it onto the actual website. This is how i've been sending the mail on my localhost:

require_once(ABSPATH . WPINC . '/class-phpmailer.php');
$pmail = new PHPMailer();
$pmail->From = strip_tags($email);
$pmail->FromName = $fullname;
$pmail->Subject = $subject;
$pmail->IsHTML(true);
$pmail->AddAddress('example@gmail.com', 'Simon');
$pmail->AddAttachment($filepath,$filename);
$pmail->Body = $message;
if($pmail->Send()) my_contact_form_generate_response("success", $message_sent);
else my_contact_form_generate_response("error", $message_unsent);
echo $pmail->Errorinfo;

I've tried adding this SMTP code but im not sure if I did it correctly or if thats how you do it.

Adding this:

$pmail->Host     = "smtp.gmail.com"; // SMTP server
$pmail->Port = 465;
$pmail->SMTPAuth = true;
$pmail->Username = "my@gmail.com";
$pmail->Password = "mypass";
$pmail->isSMTP();
$pmail->SMTPSecure = 'ssl';

The error message i've been getting is "Could not instantiate mail function".

When I added the SMTP my error was "SMTP connect() failed."

Simon2233
  • 113
  • 1
  • 9
  • 2
    Possible duplicate of: http://stackoverflow.com/questions/1944631/could-not-instantiate-mail-function-why-this-error-occuring – Jamie Bicknell Jun 28 '16 at 17:45
  • Where is your website hosted? Could be an issue with the host blocking outbound mail instead of the code. – Naterade Jun 28 '16 at 17:55
  • Why not use `wp_mail()`? – Jared Jun 28 '16 at 17:56
  • @Naterade Im trying to add a page to the website for an internship I'm in right now so I'm not quite sure – Simon2233 Jun 28 '16 at 18:05
  • @Jrod I ended up using PHPmailer because i wanted html in my emails and i found adding attachments easier. Not sure if it was the best choice but it worked for me. – Simon2233 Jun 28 '16 at 18:06
  • Make sure that your hosting company has open the smpt ports – lavb Jun 28 '16 at 18:16
  • @MageWH is it 100% necessary to use SMTP when hosted on a server? – Simon2233 Jun 28 '16 at 18:22
  • Suggestions: [get the latest version](https://github.com/PHPMailer/PHPMailer), [read the docs](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting), base your code on [the examples provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/tree/master/examples). It's quite likely that `wp_mail` uses PHPMailer internally - I've not looked at it recently. – Synchro Jun 28 '16 at 18:57
  • It's not strictly necessary to use SMTP, but you generally should as it's fastest way and gives you more control and feedback, but you need a mail server to send through, local or remote. – Synchro Jun 28 '16 at 18:58
  • Where is your website hosted? – lavb Jun 28 '16 at 22:45

0 Answers0