【问题标题】:Image doesn't show when I send an email / swiftmailer [closed]当我发送电子邮件 / swiftmailer 时,图像不显示 [关闭]
【发布时间】:2020-12-07 22:29:21
【问题描述】:

我目前正在使用 Swiftmailer 发送电子邮件。一切正常,它正在发送电子邮件,但是电子邮件没有显示我输入的徽标。这是代码:

require_once 'vendor/autoload.php';
require_once 'config/constants.php';

// Create the Transport
    $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
        ->setUsername(EMAIL)
        ->setPassword(PASSWORD);

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);


function sendVerificationEmail($userEmail, $token){
    global $mailer;
    $body = '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Verify Email</title>
</head>
<body>
<div class="not-verified-container">
    <img src="logo.svg" class="mazaw-logo-not-verified">
    <div class="container-without-logo">
        <div class="not-verified-texts">
            <h2>Account Verification</h2>
        </div>
    </div>
</div>
</body>
</html>';
    // Create a message
    $message = (new Swift_Message('Account Verification'))
        ->setFrom(EMAIL)
        ->setTo($userEmail)
        ->setBody($body, 'text/html');
// Send the message
    $result = $mailer->send($message);
}

如果有人能看看我做错了什么,我将非常感激。 谢谢!

【问题讨论】:

  • 那是因为该图像是您本地的。
  • 这个问题恕我直言是印刷错误。
  • 以下建议:&lt;img src="http://www.website.com/path/logo.svg" class="mazaw-logo-not-verified"&gt;
  • 请不要破坏这个问题。如果您想删除它,请执行此操作。只是不要用一堆废话来编辑它。我把它回滚到以前的版本。

标签: php swift image email swiftmailer


【解决方案1】:

您需要将徽标图像作为附件包含在电子邮件中。不知道在 Swiftmailer 中是如何完成的,但是当我用谷歌搜索它时,有很多例子,包括 StackOverflow 中的这个。

Swiftmailer Attachments

$uploadedFileName = CUploadedFile::getInstance($model,'career_resume'); 
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName);
$message->attach($swiftAttachment);

还有 Google 如何在 Swiftmailer 中嵌入图片。

【讨论】:

    【解决方案2】:

    嵌入应该可以正常工作

    <?php    
    $message = new Swift_Message('Your subject');
    $message->setBody(
    '<html>' .
    ' <body>' .
    '  <img src="' . $message->embed(Swift_Image::fromPath('image.png')) . '" />' .
    ' </body>' .
    '</html>',
    'text/html'
    );    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多