使用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.

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];
}