-2

Recently the hostingprovider has disabled for its customers sending forms with php mail(). Users can submit a form which looks like this, which always worked fine. How do i change this mail() to send with wp_mail(). I have already installed WP Mail SMTP which is working, but this is a custom form that has to be changed sending with wp_mail() instead of mail(). Thanks!

<?php

session_start();
if(!isset($_SESSION['set']))
{
    $_SESSION['set'] = 1;
    $oConnect       = mysqli_connect('localhost', 'userlogin', 'userpass');
    $content    = str_replace(array_keys($replace), array_values($replace), $content);

    //DATA pulled from DB 

    $to         = $_POST['txt_email']; 
    $subject    = 'Your request';
    $headers    = "From: info@domainexample.com\r\n";
    $headers    .= "Bcc: info@domainexmaple.com\r\n";
    $headers    .= "Reply-To: info@domainexample.com\r\n";
    $headers    .= "MIME-Version: 1.0\r\n";
    $headers    .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    // send mail
    if(!empty($to)){
       mail($to,$subject,$content,$headers); 
    }
}
?>
El Gucs
  • 897
  • 9
  • 18
Steven83
  • 9
  • 2
  • Take a look at [the PHPMailer docs on WordPress](https://github.com/PHPMailer/PHPMailer/wiki/Using-PHPMailer-in-WordPress). – Synchro Mar 19 '23 at 12:15
  • 1
    What have you tried, and how exactly did it not work? `wp_mail` takes the same parameters in the same order, according to its documentation, as you already have there in your `mail` call. – CBroe Mar 20 '23 at 08:00

1 Answers1

1

Problem solved! The reason why wp_mail did not work and therefore plugins like WP Mail SMTP also didn't work was because wp-load was not called. If this is not called, then only mail() function works and not wp_mail().

To solve this, simply put this directly after <?php:

require_once("../wp-load.php");

By adding this, the wp_mail() function gets called instead of mail() if you use something like wp_mail($to,$subject,$headers);

El Gucs
  • 897
  • 9
  • 18
Steven83
  • 9
  • 2