duanmu0834 2019-03-07 10:04
浏览 65
已采纳

使用PHP的PostmarkClient发送内嵌图像时出现问题

I have an existing email that gets sent to users when they forget their password. I want to add inline images to this email to show people that are less tech savvy where to click, etc.

Here is my code:

$htmlBody = 'Here is a <span style="background-color: yellow;font-weight: bold;">one-time use</span> password.  If not used within 72 hours, it will expire and you will need to request another temporary password. ';
        $htmlBody .= '<br><br>Please follow the steps below carefully to reset your password.<br>';
        $htmlBody .= '<ol><li><strong>Log out of the template manager</strong> (right click on the template manager in the task bar and choose Exit from the menu).</li>';
        $htmlBody .= '<img src="data:image/png;base64,' . base64_encode('ZmlsZTovQzovd2FtcDY0L3d3dy90YWJ1bGEvaW1nL2V4aXRfZnJvbV90ZW1wbGF0ZV9tYW5hZ2VyLnBuZw==') . '" width="325" height="149">';
        $htmlBody .= '<li>Copy the password below and use it to sign in to your account on <a href="https://########/">####</a>.';
        $htmlBody .= '<ul><li style="list-style-type: none;padding-top:5px;padding-bottom:5px;">Password: <span style="background-color: yellow;">' . $newpassword . '</span>';
        $htmlBody .= '</li></ul></li>';
        $htmlBody .= '<li>Once you log in, you will be prompted to change your password.  A message will appear in red at the top of the ##### page if you changed your password successfully.</li>';
        $htmlBody .= '<li>Once you have changed your password, you will automatically be logged out and have to log in again with your new password.</li>';
        $htmlBody .= '<li>Update your password manager with your new password, if you use one.</li>';
        $htmlBody .= '<li>Log in to the template manager using your new password.</li>';
        $htmlBody .= '<li>If you use a separate password manager for the template manager, update your password there too.</li></ol>';
        $htmlBody .= '<br><br>Thank you,<br>eScribers';
        $email = [
            'tag'         => 'temporaryPasswordSent',
            'to'          => $transcriber->email,
            'from'        => '#####',
            'from_name'   => '####',
            'subject'     => 'Your temporary #### password',
            'html'        => $htmlBody
        ];
        $emailClient = new emailClient();
        $emailClient->sendEmail($email);

The email gets sent, but I get an empty box that appears in the email, not the image. I have tried many times to convert the image to base64, but that doesn't seem to help. Can anyone tell me what I am doing wrong? Thank you.

EDIT: Anyone? I am really struggling to figure this out. I don't think it's a permission thing because I echoed the images out on the log in page and they showed up with no problem.

  • 写回答

1条回答 默认 最新

  • douyazi1129 2019-03-11 09:52
    关注

    A coworker finally figured this out. Here is the code if it could help anyone else:

        $attachments = $this->setForgotPasswordAttachmentImages();
        $htmlBody = 'Here is a <span style="background-color: yellow;font-weight: bold;">one-time use</span> password.  If not used within 72 hours, it will expire and you will need to request another temporary password. ';
        $htmlBody .= '<br><br>Please follow the steps below carefully to reset your password.<br>';
        $htmlBody .= '<ol><li><strong>Log out of the template manager</strong> (right click on the template manager in the task bar and choose Exit from the menu).</li><br />';
        $htmlBody .= '<br /><img src="cid:exit_from_template_manager.png" width="325" height="149"><br />';
        $htmlBody .= '<li>Copy the password below and use it to sign in to your account on <a href="https://####/">###</a>.';
        $htmlBody .= '<ul><li style="list-style-type: none;padding-top:5px;padding-bottom:5px;">Password: <span style="background-color: yellow;">' . $newpassword . '</span>';
        $htmlBody .= '</li></ul></li>';
        $htmlBody .= '<br /><img src="cid:log_in_to_###.jpg" width="185" height="175"><br /><br />';
        $htmlBody .= '<li>Once you log in, you will be prompted to change your password.';
        $htmlBody .= 'A message will appear in red at the top of the ### page if you changed your password successfully.</li>';
        $htmlBody .= '<br /><img src= "cid:password_successfully_changed_message.png" width="190" height="309"><br /><br />';
        $htmlBody .= '<li>Once you have changed your password, you will automatically be logged out and have to log in again with your new password.</li>';
        $htmlBody .= '<li>Update your password manager with your new password, if you use one.</li>';
        $htmlBody .= '<li>Log in to the template manager using your new password.</li>';
        $htmlBody .= '<br /><img src="cid:login_to_template_manager.png" width="361" height="298"><br /><br />';
        $htmlBody .= '<li>If you use a separate password manager for the template manager, update your password there too.</li></ol>';
        $htmlBody .= '<br><br>Thank you,<br>eScribers';
        $data = [
            'From' => '####',
            'To' => $transcriberEmail,
            'Subject' => 'Your temporary #### password',
            'Tag' => 'temporaryPasswordSent',
            'HtmlBody' => $htmlBody,
            'Attachments' => $attachments
        ];
        $emailClient = new emailClient();
        return $emailClient->sendEmailWithInlineAttachments($data);
    

    And:

    public function setForgotPasswordAttachmentImages()
    {
        $attachment = [];
        $attachment[] = PostmarkAttachment::fromFile(FCPATH . 'img\exit_from_template_manager.png', 'exit_from_template_manager.png', 'image/jpg',
            'cid:exit_from_template_manager.png');
    
        $attachment[] = PostmarkAttachment::fromFile(FCPATH . 'img\log_in_to_###.jpg', 'log_in_to_###.jpg',
            'image/png', 'cid:log_in_to_###.jpg');
    
        $attachment[] = PostmarkAttachment::fromFile(FCPATH . 'img\login_to_template_manager.png', 'login_to_template_manager.png',
            'image/png', 'cid:login_to_template_manager.png');
    
        $attachment[] = PostmarkAttachment::fromFile(FCPATH . 'img\password_successfully_changed_message.png', 'password_successfully_changed_message.png',
            'image/png', 'cid:password_successfully_changed_message.png');
        return $attachment;
    }
    

    And:

    public function sendEmailWithInlineAttachments($data) {
        $client = new PostmarkClient($this->api_key);
        return $client->sendEmailBatch([$data])[0];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?