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

Wordpress send XML file to email

我正在努力实现这个结果:我正在使用联系表 7,我希望当你点击 "Submit" 时,你不会收到明文电子邮件,而是一个 .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 直接发送到电子邮件地址?

提前致谢,

卢卡

生成文件后,函数可以使用 wp_mail 函数发送它。

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);

   // then send email
   $to = "sendto@example.com";
   $subject = "The subject";
   $body = "The email body content";
   $headers = "From: My Name <myname@example.com>" . "\r\n";

   $attachments = array( "cf7outputtest.xml" );

   wp_mail( $to, $subject, $body, $headers, $attachments );
}