Gmail Api撰写并发送带有多个附件的电子邮件

问题描述 投票:0回答:1

下面在#php #laravel中提到了我用于生成电子邮件的代码

        $message = new Google_Service_Gmail_Message();
        $mixedBoundaryString = 'MixedBoundaryString';
        $relatedBoundaryString = 'RelatedBoundaryString';
        $alternativeBoundaryString = 'AlternativeBoundaryString';
        $subject = $request['subject'];
        $to = $request['to'];
        $message_text = $request['contentText'];
        $request['cc'] = str_replace(" ", "", $request['cc']);
        $cc_tmp = explode(';', $request['cc']);
        $cc = implode(", ", $cc_tmp);
        $request['bcc'] = str_replace(" ", "", $request['bcc']);
        $bcc_tmp = explode(';', $request['bcc']);
        $bcc = implode(", ", $bcc_tmp);
        $stripTagsMessageText = strip_tags($message_text);
        $raw = "To: {$to}
Cc: {$cc}
Bcc: {$bcc}
Subject: {$subject}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"{$mixedBoundaryString}\"

--{$mixedBoundaryString}
Content-Type: multipart/related; boundary=\"{$relatedBoundaryString}\"

--{$relatedBoundaryString}
Content-Type: multipart/alternative; boundary=\"{$alternativeBoundaryString}\"

--{$alternativeBoundaryString}
Content-Type: text/plain;charset=\"utf-8\"
Content-Transfer-Encoding: quoted-printable

{$stripTagsMessageText}

--{$alternativeBoundaryString}
Content-Type: text/html;charset=\"utf-8\"
Content-Transfer-Encoding: quoted-printable

{$message_text}

--{$alternativeBoundaryString}--
";
        if (isset($request['attachments']) && count($request['attachments']) > 0) {
            foreach ($request['attachments'] as $attachment) {
                if (!is_null($attachment)) {
                    $location = public_path() . '/email_attachments/sent/';
                    $filename = str_random(20) . '-' . $attachment->getClientOriginalName();
                    $file_location = $attachment->move($location, $filename);
                    $mimetype = $attachment->getClientMimeType();
                    $attachmentName = $attachment->getClientOriginalName();
                    $base64Attachement = chunk_split(base64_encode(file_get_contents($location . $filename)), 76, "\n");
                    $raw .= "
--{$relatedBoundaryString}
Content-Type: {$mimetype};name=\"{$attachmentName}\"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename=\"{$attachmentName}\"
Content-ID: <{$to}>

{$base64Attachement}";
                }
            }
        }
        $raw .= "
--{$relatedBoundaryString}--

--{$mixedBoundaryString}--";
        $message->setRaw($this->base64url_encode($raw));
        return $message;

如果我在base64url_encode之前打印预览数据,它将提供以下内容

To: [email protected]
Cc: [email protected]
Bcc: [email protected]
Subject: Multiple Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MixedBoundaryString"

--MixedBoundaryString
Content-Type: multipart/related; boundary="RelatedBoundaryString"

--RelatedBoundaryString
Content-Type: multipart/alternative; boundary="AlternativeBoundaryString"

--AlternativeBoundaryString
Content-Type: text/plain;charset="utf-8"
Content-Transfer-Encoding: quoted-printable

Dear All,I’m writing to resign from my position as Web Developer, effective August 20, 2019.I’ve recently decided to go back to school, and my program starts in early September. I’m tendering my resignation now so that I can be as helpful as possible to you during the transition.I’ve truly enjoyed my time working with you and everyone else on our team at LMK. It’s rare to find a customer service role that offers as much opportunity to grow and learn and such a positive, inspiring team of people to grow and learn with.I’m particularly grateful for your guidance while I was considering furthering my education. Your support has meant so much to me.&nbsp;Please let me know if there’s anything I can do to help you find and train my replacement.Thanks, and best wishes,

--AlternativeBoundaryString
Content-Type: text/html;charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<p style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">Dear All,</p><p style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">I’m writing to resign from my position as <span style="font-weight: bold;">Web Developer</span>, effective August 20, 2019.</p><p style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">I’ve recently d<span style="text-decoration-line: underline;">ecided to go back to school, and my program starts in early September</span>. I’m tendering my resignation now so that I can be as helpful as possible to you during the transition.</p><blockquote style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">I’ve truly enjoyed my time working with you and everyone else on our team at LMK. <span style="text-decoration-line: line-through;">It’s rare to find a customer service role that offers as much opportunity to grow</span> and learn and such a positive, inspiring team of people to grow and learn with.</blockquote><p style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">I’m particularly grateful for your guidance while<span style="font-style: italic;"> I was considering furthering my education. </span>Your support has meant so much to me.&nbsp;</p><p style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">Please let me know if there’s anything I can do to help you find and train my replacement.</p><p style="box-sizing: border-box; margin: 0.875rem 0px; padding: 0px; font-family: Rubik, Arial, sans-serif; color: rgb(34, 34, 34); font-size: 17px;">Thanks, and best wishes,</p>

--AlternativeBoundaryString--

--RelatedBoundaryString
Content-Type: image/jpeg;name="22366329_1711794525581687_2834310312601824228_n.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename="22366329_1711794525581687_2834310312601824228_n.jpg"
Content-ID: <[email protected]>

/9j/4AAQSkZJRgABAgAAAQABAAD/7QCcUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAIAcAmcAFFJj
aVVyMDd0cHBaLTRWNkpodlZMHAIoAGJGQk1EMDEwMDBhYjYwMzAwMDA5NzEyMDAwMGM2MmEwMDAw
......
......
+QEQIiJ5HAAAnx/4H/k+v/sXI/TA/wDqP/Rx/wDiv+DjL/i//kOBgT6//WmTJ/jn/Bz/APe/+l9Y
aP8A+Ag3zx/+OZP/AMKfCcQ5e/2//ifdQ79XBRGr2Cvc/wD4lQYFLfP26xaoGV5P/wCJMCFF4C/8
+uEIDAz/2Q==

--RelatedBoundaryString
Content-Type: image/png;name="1561271605_apple-icon-60x60.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename="1561271605_apple-icon-60x60.png"
Content-ID: <[email protected]>

iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAM
8klEQVRogc2b23Mc1Z3HP7/T3XOf0UgjybpL1lh3WQEvhkCCMQV4F8gSCCSQha192D9iH/Zh/4p9
......
......
/EkeG5vg5yvL/O8nH3N++TOubW7gq42+IbFHTvqeEK7DiDCaLzAyM88T48f56coy/37hPd789CM2
arXDJUfaxB8BJejO2uWjqaEAAAAASUVORK5CYII=

--RelatedBoundaryString--

--MixedBoundaryString--

一旦我编码并发送电子邮件在发件人部分:

enter image description here

并且在接收部分中,如下面的屏幕快照所示接收邮件

enter image description here

请帮助我找出电子邮件的问题预先感谢

php laravel gmail gmail-api email-attachments
1个回答
0
投票

我复制并尝试了您的代码,但遇到了同样的问题。对我来说解决的是实际将Content-ID从附件更改为附件。

因此,不要像原始字符串那样:

--{$relatedBoundaryString}
Content-Type: {$mimetype};name=\"{$attachmentName}\"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename=\"{$attachmentName}\"
Content-ID: <{$to}>

您可以尝试将Content-ID转换为图像唯一,您可以尝试使用已经定义的变量,例如attachmentNamefilename

--{$relatedBoundaryString}
Content-Type: {$mimetype};name=\"{$attachmentName}\"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename=\"{$attachmentName}\"
Content-ID: <{$attachmentName}>
© www.soinside.com 2019 - 2024. All rights reserved.