我正在尝试创建表单并使用wp\\U mail将其发送到我的电子邮件地址。我的wp\\u mail()代码是:
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 );
}
为了更改发件人地址,我在函数中添加了以下过滤器。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;
}
HTML格式为:
<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>
它没有将邮件表单发送到我的电子邮件。但如果我按以下方式更改过滤器,它会将表单发送到我的电子邮件:
/* 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网友:Chris Morris 整理而成
如果我没听错的话,你只是在拍摄现场遇到了麻烦?最简单的方法就是添加from 在email headers 在wp_mail 作用
下面是我的一个旧的简单电子邮件发送功能的示例:
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提供您在
wp_mail 函数,您没有设置一个,因此它接收到的是空的,并发送回一个空变量($未定义senderEmail,因此将返回未定义的)。