4

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 = "<b><span style='color: red;'>Name:</span></b> $name_title $sender\n<br/>
                 <b>Email:</span></b> $senderEmail\n\n<br/><br/>
                 <b>Message:</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</div><div class="form-input"><input type="text" name="sendername"/></div>
    </div>

    <div class="label-input-wrapper">
        <div class="form-label">E-Mail</div><div 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 "my@email.com";
}

/* 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?

  • It looks like a problem with the scope of your variables, check for example this PHP doc page. PS: I don't see any validation/sanitazion in your example! – birgire Sep 3 '14 at 9:17
  • I am checking the code. If all ok I'll move to validation part. and I do not find the variable errors here... – Riffaz Starr Sep 3 '14 at 9:18
  • I think you should use WP_DEBUG while testing on your dev install, but your $senderEmail and $sender variables are not defined within your functions, since these variables are defined in the global scope. – birgire Sep 3 '14 at 9:28
13

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('my_email@email.com', $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).

  • Woooh... that's great man.. this line save me.. $headers = array('From: '.$_POST['sendername'].' <'.$_POST['senderEmail'].'>'); and I removed wp_mail_fromfilter and wp_mail_from_name Now I'm getting exact the from sender's name and it's sender email on mail header.. – Riffaz Starr Sep 3 '14 at 9:30
5

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.

  • 1
    Please edit your answer, and add an explanation: why could that solve the problem? Note the the OP wanted to use a specific variable in these functions. – fuxia Feb 21 '17 at 9:07
  • worked for me! thanks. I would like just to add that function.php.is found in your theme directory under /wp-content/themes/ – shady sherif Apr 14 '17 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.