在Wordpress中发送电子邮件 [英] Sending emails in Wordpress

查看:203
本文介绍了在Wordpress中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从早期获得了Wordpress,HTML电子邮件表单和一些PHP内容。
我得到了一个名为联系人的页面,但我不知道如何在Wordpress中正确发送电子邮件。



1)使用什么动作属性值



2)保存文件中的PHP代码?在哪里放那个文件?如何将其与动作属性值进行链接?

 < form role =formmethod =POSTaction =# > 
... blah blah ...
< / form>


<?php

//检查表单提交 - 如果不存在,则发回联系表单
if(! isset($ _ POST [save])|| $ _POST [save]!=contact){
header(Location:contact.php);出口;
}

//获取发布的数据
$ name = $ _POST [name];
$ email_address = $ _POST [email];
$ phone_num = $ _POST [phone];
$ message = $ _POST [message];

//写电子邮件内容
$ header。=MIME-Version:1.0\\\
;
$ header。=Content-Type:text / html; charset = utf-8\\\
;
$ headers。=From:。 $ EMAIL_ADDRESS;

$ message =Name:$ name\\\
;
$ message。=电子邮件地址:$ email_address\\\
;
$ message。=电话:$ contact_phone\\\
;
$ message。=消息:\\\
$ message;

$ subject =Zprávaz webu;
$ subject ==?utf-8?B? 。 base64_encode($ subject)。 ?=;

$ to =thenemecek@gmail.com;

//发送电子邮件
wp_mail($ to,$ subject,$ message,$ header);

//将用户返回到表单
header(Location:contact.php?s =。urlencode(谢谢你的信息));出口;

?>


解决方案

虽然你可以它喜欢通过将 sendmail.php 文件放在网站根目录中提到的评论者,这不是WordPress的方式。您可以使用联系表格7 等插件,使用代码构建自己的插件或将您的表单/邮件处理代码完全整合到您的主题中。



假设您想将代码整合到您的主题中,您将首先复制文件 page.php 并将其命名为 page-contact.php 。然后,在文件的顶部:

 <?php 

/ *
模板名称:联系
* /

//检查表单提交 - 如果不存在,然后发回联系人表单
if(isset($ _ POST [保存])&& $ _POST [save] ==contact){
//触发器动作/函数'contact_send_message'
do_action('contact_send_message');
}

全局$ contact_errors;

get_header(); ?>

...您的页面的其余部分...
...检查(bool)$ contact_errors并显示表单或感谢结果...

如果您在WordPress中创建的页面具有slug 联系人此模板将自动显示,否则您可以在页面编辑器中选择页面模板。



然后,在主题的 functions.php 您添加一个新功能和一个新的 add_action 调用处理POST并处理邮件:

  function contact_send_message(){

$ contact_errors = false;

//获取发布的数据
$ name = $ _POST [name];
$ email_address = $ _POST [email];
$ phone_num = $ _POST [phone];
$ message = $ _POST [message];

//写电子邮件内容
$ header。=MIME-Version:1.0\\\
;
$ header。=Content-Type:text / html; charset = utf-8\\\
;
$ header。=From:。 $ EMAIL_ADDRESS;

$ message =Name:$ name\\\
;
$ message。=电子邮件地址:$ email_address\\\
;
$ message。=电话:$ contact_phone\\\
;
$ message。=消息:\\\
$ message;

$ subject =Zprávaz webu;
$ subject ==?utf-8?B? 。 base64_encode($ subject)。 ?=;

$ to =thenemecek@gmail.com;

//使用wp_mail()
发送电子邮件如果(!wp_mail($ to,$ subject,$ message,$ header)){
$ contact_errors = true;
}

}
add_action('contact_send_message','contact_send_message');

请注意,这是一个非常基本的例子。在完美的世界中,您可以添加字段验证,POST验证,并使用 wp_nonce_field 。 HTH


I got Wordpress, HTML email form and some PHP stuff from earlier. I got the page called "Contact", but I don't know how to properly send emails in Wordpress.

1) what action attribut value to use

2) save PHP code in file? where to put that file? how to link it from action attribut value?

<form role="form" method="POST" action="#">
... blah blah ...
</form>


<?php

// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST["save"]) || $_POST["save"] != "contact") {
    header("Location: contact.php"); exit;
}

// get the posted data
$name = $_POST["name"];
$email_address = $_POST["email"];
$phone_num = $_POST["phone"];
$message = $_POST["message"];

// write the email content
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$headers .= "From:" . $email_address;

$message = "Name: $name\n";
$message .= "Email Address: $email_address\n";
$message .= "Telefon: $contact_phone\n";
$message .= "Message:\n$message";

$subject = "Zpráva z webu";
$subject = "=?utf-8?B?" . base64_encode($subject) . "?=";

$to = "thenemecek@gmail.com";

// send the email
wp_mail($to, $subject, $message, $header);

// send the user back to the form
header("Location: contact.php?s=".urlencode("Thank you for your message.")); exit;

?>

解决方案

While you can do it like the commenters mentioned by just putting the sendmail.php file in the website root, it's just not the WordPress way of doing so. You can either use a plugin like Contact Form 7, build your own plugin with your code or fully integrate your form/mail processing code into your theme.

Assuming you want to integrate your code into your theme, you would first duplicate the file page.php and name it page-contact.php. Then, at the top of the file:

<?php

/*
Template Name: Contact
*/

// check for form submission - if it doesn't exist then send back to contact form
if ( isset($_POST["save"]) && $_POST["save"] == "contact" ) {
    // Trigger action/function 'contact_send_message'
    do_action( 'contact_send_message' );
}

global $contact_errors;

get_header(); ?>

... rest of your page ...
... check for (bool) $contact_errors and either display the form or a thank you result ...

If the page you created in WordPress has the slug contact this template will be displayed automatically, otherwise you can choose the page template in the page editor.

Then, in your theme's functions.php you add a new function and a new add_action call which handles the POST and processes the mail:

function contact_send_message() {

    $contact_errors = false;

    // get the posted data
    $name = $_POST["name"];
    $email_address = $_POST["email"];
    $phone_num = $_POST["phone"];
    $message = $_POST["message"];

    // write the email content
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: text/html; charset=utf-8\n";
    $header .= "From:" . $email_address;

    $message = "Name: $name\n";
    $message .= "Email Address: $email_address\n";
    $message .= "Telefon: $contact_phone\n";
    $message .= "Message:\n$message";

    $subject = "Zpráva z webu";
    $subject = "=?utf-8?B?" . base64_encode($subject) . "?=";

    $to = "thenemecek@gmail.com";

    // send the email using wp_mail()
    if( !wp_mail($to, $subject, $message, $header) ) {
        $contact_errors = true;
    }

}
add_action('contact_send_message', 'contact_send_message');

Please note that this is a very rudimentary example. In a perfect world you would add field validation, POST verification and make use of the wp_nonce_field. HTH

这篇关于在Wordpress中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆