简体   繁体   English

在php中使用gmail api发送带附件的邮件

[英]sending mail with attachment using gmail api in php

When I'm trying to send mail using php.text also send as an attachment with the name noname.html.当我尝试使用 php.text 发送邮件时,也将其作为名称为 noname.html 的附件发送。 please provide a solution for me.请为我提供解决方案。 I have used this library to send email https://github.com/google/google-api-php-client .我已经使用这个库发送电子邮件https://github.com/google/google-api-php-client My code is like this.any help will be appreciated.我的代码是这样的。任何帮助将不胜感激。

$client->setAccessToken($_SESSION['gmail_access_token']);            
    $objGMail = new Google_Service_Gmail($client);

   $strMailContent = 'This is a test mail which is <b>sent via</b> using Gmail API client library.<br/><br/><br/>Thanks,<br/><b>Premjith K.K..</b>';
   // $strMailTextVersion = strip_tags($strMailContent, '');

    $strRawMessage = "";
    $boundary = uniqid(rand(), true);
    $subjectCharset = $charset = 'utf-8';
    $strToMailName = 'NAME';
    $strToMail = 'name@gmail.com';
    $strSesFromName = 'Premjith GMAIL API';
    $strSesFromEmail = 'premji341800@gmail.com';
    $strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');

    $strRawMessage .= 'To: ' .$strToMailName . " <" . $strToMail . ">" . "\r\n";
    $strRawMessage .= 'From: '.$strSesFromName . " <" . $strSesFromEmail . ">" . "\r\n";

    $strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
    $strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
    $strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";

    $filePath = 'abc.pdf';
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
    $mimeType = finfo_file($finfo, $filePath);
    $fileName = 'abc.pdf';
    $fileData = base64_encode(file_get_contents($filePath));

    $strRawMessage .= "\r\n--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n";            
    $strRawMessage .= 'Content-ID: <' . $strSesFromEmail . '>' . "\r\n";            
    $strRawMessage .= 'Content-Description: ' . $fileName . ';' . "\r\n";
    $strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
    $strRawMessage .= chunk_split(base64_encode(file_get_contents($filePath)), 76, "\n") . "\r\n";
    $strRawMessage .= '--' . $boundary . "\r\n";

    $strRawMessage .= "\r\n--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: text/plain; charset=' . $charset . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
   // $strRawMessage .= $strMailTextVersion . "\r\n";

    $strRawMessage .= "--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= $strMailContent . "\r\n";

    //Send Mails
    //Prepare the message in message/rfc822
    try {
        // The message needs to be encoded in Base64URL
        $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
        $msg = new Google_Service_Gmail_Message();
        $msg->setRaw($mime);
        $objSentMsg = $objGMail->users_messages->send("me", $msg);

        print('Message sent object');
       // print($objSentMsg);

    } catch (Exception $e) {
        print($e->getMessage());
        unset($_SESSION['access_token']);
    }

When I change the code line $strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\\r\\n";当我更改代码行$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\\r\\n"; $strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\\r\\n"; to $strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\\r\\n"; to $strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\\r\\n"; $strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\\r\\n"; Mail Send as html only with out attachment.邮件仅以 html 格式发送,不带附件。 Please help me请帮帮我

I have change my code like this its worked fine for me.我已经像这样更改了我的代码,它对我来说很好用。 Thank you all for the support.谢谢大家的支持。

$client->setAccessToken($_SESSION['gmail_access_token']);            
    $objGMail = new Google_Service_Gmail($client);

   $strMailContent = 'This is a test mail which is <b>sent via</b> using Gmail API client library.<br/><br/><br/>Thanks,<br/><b>Premjith K.K..</b>';
   // $strMailTextVersion = strip_tags($strMailContent, '');

    $strRawMessage = "";
    $boundary = uniqid(rand(), true);
    $subjectCharset = $charset = 'utf-8';
    $strToMailName = 'NAME';
    $strToMail = 'name@gmail.com';
    $strSesFromName = 'Premjith GMAIL API';
    $strSesFromEmail = 'premji341800@gmail.com';
    $strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');

    $strRawMessage .= 'To: ' .$strToMailName . " <" . $strToMail . ">" . "\r\n";
    $strRawMessage .= 'From: '.$strSesFromName . " <" . $strSesFromEmail . ">" . "\r\n";

    $strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
    $strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
    $strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";

    $filePath = 'abc.pdf';
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
    $mimeType = finfo_file($finfo, $filePath);
    $fileName = 'abc.pdf';
    $fileData = base64_encode(file_get_contents($filePath));

    $strRawMessage .= "\r\n--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n";            
    $strRawMessage .= 'Content-ID: <' . $strSesFromEmail . '>' . "\r\n";            
    $strRawMessage .= 'Content-Description: ' . $fileName . ';' . "\r\n";
    $strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
    $strRawMessage .= chunk_split(base64_encode(file_get_contents($filePath)), 76, "\n") . "\r\n";
    $strRawMessage .= "--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= $strMailContent . "\r\n";

    //Send Mails
    //Prepare the message in message/rfc822
    try {
        // The message needs to be encoded in Base64URL
        $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
        $msg = new Google_Service_Gmail_Message();
        $msg->setRaw($mime);
        $objSentMsg = $objGMail->users_messages->send("me", $msg);

        print('Message sent object');
       // print($objSentMsg);

    } catch (Exception $e) {
        print($e->getMessage());
        unset($_SESSION['access_token']);
    }

The above code works fine for a single attachment.上面的代码适用于单个附件。

If you want to send multiple attachments just follow this code this allowed to send your mail body part with multiple attachments.如果您想发送多个附件,只需按照此代码即可发送带有多个附件的邮件正文部分。

$client->setAccessToken($_SESSION['gmail_access_token']);            
$objGMail = new Google_Service_Gmail($client);
$strMailContent = 'This is a test mail which is <b>sent via</b> using Gmail API client library.<br/><br/><br/>Thanks,<br/><b>Premjith K.K..</b>';
$strRawMessage = "";
$boundary = uniqid(rand(), true);
$subjectCharset = $charset = 'utf-8';
$strToMailName = 'NAME';
$strToMail = 'name@gmail.com';
$strSesFromName = 'Premjith GMAIL API';
$strSesFromEmail = 'premji341800@gmail.com';
$strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');

$strRawMessage .= 'To: ' .$strToMailName . " <" . $strToMail . ">" . "\r\n";
$strRawMessage .= 'From: '.$strSesFromName . " <" . $strSesFromEmail . ">" . "\r\n";

$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";
$strRawMessage .= "\r\n--{$boundary}\r\n";
$strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
$strRawMessage .= "Content-Transfer-Encoding: base64" . "\r\n\r\n";
$strRawMessage .= $sentMailData->body . "\r\n";
$strRawMessage .= "--{$boundary}\r\n";

foreach ($files as $key => $filePath) {
    if($filePath!=""){
        $array = explode('/', $filePath);
        $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
        $mimeType = finfo_file($finfo, $filePath);
        $fileName = $array[sizeof($array)-1];
        $fileData = base64_encode(file_get_contents($filePath));

        $strRawMessage .= "\r\n--{$boundary}\r\n";
        $strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n";            
        $strRawMessage .= 'Content-ID: <' . $sentMailData->email. '>' . "\r\n";            
        $strRawMessage .= 'Content-Description: ' . $fileName . ';' . "\r\n";
        $strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "\r\n";
        $strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
        $strRawMessage .= chunk_split(base64_encode(file_get_contents($filePath)), 76, "\n") . "\r\n";
        $strRawMessage .= "--{$boundary}\r\n";
    }
}

//Send Mails
//Prepare the message in message/rfc822
try {
    // The message needs to be encoded in Base64URL
    $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    $objSentMsg = $service->users_messages->send("me", $msg);
    echo '<pre>'; print_r($objSentMsg); echo '</pre>';
    if($sentMailData->attachments!=""){
        $files = explode(',', $sentMailData->attachments);
        foreach ($files as $key => $filePath) {
            if($filePath!=""){
                @unlink($filePath);
            }
        }
    }
    echo '<pre>'; print_r($sentMailData); echo '</pre>';
} catch (Exception $e) {
    print($e->getMessage());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM