how to set from address according to the form input email address for wp_mail()?

I am trying to create form and send it to my e-mail address using wp_mail. my wp_mail() code is:

if($_POST["submit"]) {
    $to="my email";
    $subject ="My subject";     
    $sender=$_POST["sendername"];
    $senderEmail=$_POST["senderEmail"];
    $message=$_POST["message"];
    $name_title=$_POST["name_title"];   

    $mailBody = "bspan style='color: red;'Name:/span/b $name_title $sender\nbr/
                 bEmail:/span/b $senderEmail\n\nbr/br/
                 bMessage:/b $message";

    $mail_sent = wp_mail( $to, $subject, $mailBody );
    }

and for changing the from address I've added the following filters into my functions.php

/* adding support for html emails*/
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
function wpse27856_set_content_type(){
    return "text/html";
}

/* from address */
add_filter( 'wp_mail_from', 'my_mail_from' );
function my_mail_from( $email )
{
    return $senderEmail;
}

/* from name*/
add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name )
{
    return $sender;
}

and the form HTML is:

form id="" name="" action="?php echo get_permalink(); ?" method="post"
div class="form-input"
    select name="name_title" class="name-title-input"
    option value="" selected="selected"Select Title/option
    option value="Mr"Mr/option
/div
div class="label-input-wrapper"
        div class="form-label"Name/divdiv class="form-input"input type="text" name="sendername"//div
    /div

    div class="label-input-wrapper"
        div class="form-label"E-Mail/divdiv class="form-input"input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required //div
    /div
    input type="submit" value="Submit" name="submit"
/form 

It's not sending the mail form to my email. but It sends form to my email if I change the filters as following:

/* from address */
add_filter( 'wp_mail_from', 'my_mail_from' );
function my_mail_from( $email )
{
    return "[email protected]";
}

/* from name*/
add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name )
{
    return "my name";
}

So how can make it possible to get the senders email on from header?

Topic html-email wp-mail forms Wordpress

Category Web


If I'm understanding you right you just are having trouble getting the from set? The easiest way would just be adding the from in the email headers in the wp_mail function.

Here is an example of one of my old simple email sending functions that works:

function contact_send() {
    $title   = 'New message Received';
    $headers = array('From: '.$_POST['full_name'].' <'.$_POST['email_address'].'>');
    $message = '<h1>My message</h1>';

    //Send the email
    add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
    $email = wp_mail('[email protected]', $title, $message, $headers);
    remove_filter('wp_mail_content_type', 'set_html_content_type');

    return $email;
}

The wp_mail_from hook provides the email you have already set in your wp_mail function, you have not set one so it was receiving blank and sending back an empty variable ($senderEmail was not defined so would return undefined).


For this filtering the email address:

add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name ) {
    return "My Name";
}

Now add a filter for the name of the email address:

add_filter( 'wp_mail_from', 'my_mail_from' );
function my_mail_from( $email ) {
    return "change-this-to-your-email-address";
}

into your function.php.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.