【问题标题】:attach google drive file automatically to send mail自动附加谷歌驱动器文件以发送邮件
【发布时间】:2015-08-05 16:38:30
【问题描述】:

我在 php 中,我需要自动附加一个 google 驱动器文件,以便通过邮件发送。 我现在拥有的是:

  1. HTML 表格如下:

    提示

            <span class="js-sorter-desc glyphicon glyphicon-chevron-up pull-right"></span>
            <span class="js-sorter-asc glyphicon glyphicon-chevron-down pull-right"></span>
        </th>
        <th data-field="Etiquetas" data-align="left" data-sortable="true">Etiquetas
    
            <span class="js-sorter-desc glyphicon glyphicon-chevron-up pull-right"></span>
            <span class="js-sorter-asc glyphicon glyphicon-chevron-down pull-right"></span>
        </th>
        <th data-field="Fecha" data-align="left" data-sortable="true">Fecha
    
            <span class="js-sorter-desc glyphicon glyphicon-chevron-up pull-right"></span>
            <span class="js-sorter-asc glyphicon glyphicon-chevron-down pull-right"></span>
        </th>
        <th data-field="Nombre" data-align="left" data-sortable="true">Nombre
    
            <span class="js-sorter-desc glyphicon glyphicon-chevron-up pull-right"></span>
            <span class="js-sorter-asc glyphicon glyphicon-chevron-down pull-right"></span>
        </th>
    </tr>
    <tr class="fila_filtros filtros_buscador_drive">
        <th>
            <input class="js-filter  form-control" type="text" value="">
            </th>
            <th>
                <input class="js-filter  form-control" type="text" value="">
                </th>
                <th>
                    <input class="js-filter  form-control" type="text" value="">
                    </th>
                    <th>
                        <input class="js-filter  form-control" type="text" value="">
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr class="boton_enlace_directo" data-destino="newmail" data-id="Redactar E-Mail" data-vars="var_fichero_gdrive=1kth1GhrmMEBK2cAMyiy_4Dw1qlJFNdXVuXajJ6nMTQgvar_fichero_gdriveManual KumbiaPHP 1.0 beta2var_fichero_gdriveapplication/vnd.google-apps.documentvar_fichero_gdrivevar_fichero_gdrive">
                        <td>application/vnd.google-apps.document</td>
                        <td></td>
                        <td>05/03/2015</td>
                        <td>Manual KumbiaPHP 1.0 beta2</td>
                    </tr>
                    <tr class="boton_enlace_directo" data-destino="newmail" data-id="Redactar E-Mail" data-vars="var_fichero_gdrive=1PwXa6f32BJuAC_m5X5wB9lPzoXAg__uKdUx-1efY4owvar_fichero_gdrive1392970712_Informe 2.docxvar_fichero_gdriveapplication/vnd.google-apps.documentvar_fichero_gdrivevar_fichero_gdrive">
                        <td>application/vnd.google-apps.document</td>
                        <td></td>
                        <td>15/09/2014</td>
                        <td>1392970712_Informe 2.docx</td>
                    </tr>
                    <tr class="boton_enlace_directo" data-destino="newmail" data-id="Redactar E-Mail" data-vars="var_fichero_gdrive=0B4X2ktyVTp3mVmtjR2JTSTVreE0var_fichero_gdrive148b14b859c2fa4a.htmlvar_fichero_gdrivetext/htmlvar_fichero_gdrivehttps://docs.google.com/uc?id=0B4X2ktyVTp3mVmtjR2JTSTVreE0&amp;export=downloadvar_fichero_gdrive16">
                        <td>text/html</td>
                        <td></td>
                        <td>26/09/2014</td>
                        <td>148b14b859c2fa4a.html</td>
                    </tr>
                </tbody>
            </table>
    

在每个标签中,我添加了一些数据属性以便获取它们

  1. 我把这些参数都拿好了,新邮件表单的模板也加载好了。但我不知道如何自动附加谷歌驱动器文件。我正在尝试这个:

if( isset($_REQUEST['var_fichero_gdrive']) && $_REQUEST['var_fichero_gdrive']!="" ){
    $metadata_fichero = explode("var_fichero_gdrive",$_REQUEST['var_fichero_gdrive']);  //guardamos el array en una nueva variable
    $fichero_gdrive = array(
        "name" => $metadata_fichero[1],
        "type" => $metadata_fichero[2],
        "tmp_name" => $metadata_fichero[3],
        "error" => 0,
        "size"  => $metadata_fichero[4]
    );
    $_FILES['file'] = $fichero_gdrive;
    unset($fichero_gdrive,$metadata_fichero);
    $gest->uploadAttachment();
}

有什么帮助吗?

【问题讨论】:

    标签: php email google-drive-api sendmail attachment


    【解决方案1】:

    我终于通过google drive api返回的“downloadUrl”参数获得了它。有了它,我可以访问谷歌驱动器文件内容,并且感谢字符串 $_REQUEST['var_fichero_gdrive']) (我上面提到过),我可以获得 donwloadUrl 参数,即“var_fichero_gdrive”是这样的字符串:

    var_fichero_gdrive=IDvar_fichero_gdriveNAMEvar_fichero_gdriveDOWNLOAD_URL
    

    我用ajax调用php,在php中我选择了这个变量,我使用explode来获取这些google驱动文件数据(你可以添加你想要的):

    $metadata_fichero = explode("var_fichero_gdrive",$_REQUEST['var_fichero_gdrive'];
    

    如果没有 downloadUrl 参数,我会这样做:

    if ( !isset($downloadUrl) || trim($downloadUrl) == "" ) {
            $file["downloadUrl"] = $file->exportLinks['application/pdf'];
        }
    

    借助 exportLinks,我创建了一个新的 downloadUrl。

    最后,我使用 file_put_contents 将下载的文件内容插入到 tmp 目录下的文件中:

    $lDrive       = new CGoogleDrive(TOKEN);
    $response_body = $lDrive->getDownloadFile( ID_GOOGLE_DRIVE_FILE );
    file_put_contents( $dest, $response_body );
    

    希望对您有所帮助。您需要根据您的需要调整上面的代码。 问候,

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多