【问题标题】:How do I attach a pdf file to a Gravity Forms Notification?如何将 pdf 文件附加到重力形式通知?
【发布时间】:2016-08-31 23:14:24
【问题描述】:

重力表单提供了一种从文件上传器附加文件的方法(请参见下面的代码),但是我如何更改此代码以简单地从隐藏字段值附加我自己的 PDF 文件,或者只是将 pdf 文件粘贴到此代码中?我尝试了几件事,但没有奏效。任何帮助将不胜感激!

 add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
 function change_user_notification_attachments( $notification, $form, $entry ) {

//There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'User Notification' ) {

    $fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );

    if(!is_array($fileupload_fields))
        return $notification;

    $attachments = array();
    $upload_root = RGFormsModel::get_upload_root();
    foreach( $fileupload_fields as $field ) {
        $url = $entry[ $field['id'] ];
        $attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
        if ( $attachment ) {
            $attachments[] = $attachment;
        }
    }

    $notification['attachments'] = $attachments;

}

return $notification;
  }

【问题讨论】:

    标签: forms pdf gravity


    【解决方案1】:

    基于该代码,这样的事情应该可以工作。将 $url 值替换为 PDF 的 URL。

    add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
    function change_user_notification_attachments( $notification, $form, $entry ) {
    
        if ( $notification['name'] == 'User Notification' ) {
            $url = 'http://yoursite.com/path/to/file.pdf';
            $notification['attachments'][] = $url;
        }
    
        return $notification;
    }
    

    【讨论】:

    • 据我所知,此代码不会附加文件,因为邮件程序无法从 URL 获取文件。附件应始终作为绝对服务器路径传递,例如 /path/to/file.txt 而不是 URL https://yoursite.com/file.txt
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2012-09-30
    • 2017-11-15
    • 2012-11-22
    相关资源
    最近更新 更多