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

时间:2019-03-07 10:04:32

标签: php email

我有一个现有的电子邮件,当用户忘记密码时会发送给用户。我想在此电子邮件中添加内嵌图像,以向那些不太了解技术的人显示点击位置等。

这是我的代码:

$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);

电子邮件已发送,但我在电子邮件中出现一个空框,而不是图像。我已经尝试过多次将图像转换为base64,但这似乎无济于事。谁能告诉我我在做什么错?谢谢。

编辑:有人吗?我真的很难弄清楚这一点。我认为这不是允许的事情,因为我在登录页面上回显了图像,并且图像没有问题。

1 个答案:

答案 0 :(得分:0)

一个同事终于想通了。如果可以帮助其他人,请参见以下代码:

const sanitize = text => text.replace(/[(,:;\-–#\.\s]*$/, '');

console.log(sanitize('this is a nice,:;-–#.'));
console.log(sanitize('this is a nice ( '));
console.log(sanitize('this is a nice ( #'));
console.log(sanitize('this is a nice (:- '));

并且:

    $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);

并且:

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