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