Changing from php mail() to wp_mail() for submitting form

287 Views Asked by At

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: [email protected]\r\n";
    $headers    .= "Bcc: [email protected]\r\n";
    $headers    .= "Reply-To: [email protected]\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); 
    }
}
?>
1

There are 1 best solutions below

0
Steven83 On

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);