Wordpress 将 XML 文件发送到电子邮件

我正在尝试实现此结果:我正在使用联系表 7,我希望当您单击“提交”时,您不会收到纯文本电子邮件,而是包含所有信息的 .xml 文件。作为 xml 文件的解决方法,我正在使用该函数:


add_action( 'wpcf7_before_send_mail', 'CF7_pre_send' );


function CF7_pre_send($cf7) {

   $output = "";

   $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>Name: " . $_POST['your-name'];

   $output .= "Email: " . $_POST['your-email'];

 $output .= "Message: " . $_POST['your-message'];


 file_put_contents("cf7outputtest.xml", $output);

}

这样就生成了 xml 文件,但我只能将其保存在 wordpress 目录或特定路径中。有没有办法或解决方法将此 xml 直接发送到电子邮件地址?


慕尼黑8549860
浏览 104回答 1
1回答

宝慕林4294392

生成文件后,函数可以使用wp_mail函数发送它。add_action( 'wpcf7_before_send_mail', 'CF7_pre_send' );function CF7_pre_send($cf7) {&nbsp; &nbsp;$output = "";&nbsp; &nbsp;$output .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>Name: " . $_POST['your-name'];&nbsp; &nbsp;$output .= "Email: " . $_POST['your-email'];&nbsp;$output .= "Message: " . $_POST['your-message'];&nbsp;file_put_contents( "cf7outputtest.xml", $output);&nbsp; &nbsp;// then send email&nbsp; &nbsp;$to = "sendto@example.com";&nbsp; &nbsp;$subject = "The subject";&nbsp; &nbsp;$body = "The email body content";&nbsp; &nbsp;$headers = "From: My Name <myname@example.com>" . "\r\n";&nbsp; &nbsp;$attachments = array( "cf7outputtest.xml" );&nbsp; &nbsp;wp_mail( $to, $subject, $body, $headers, $attachments );}
打开App,查看更多内容
随时随地看视频慕课网APP