SwiftMailerを使用してメール本文で画像を送信しようとしています。パスワード変更などのメールを送信するためにSwiftMailerを使用しています。
SwiftMailerのドキュメントを使用して、cidメソッドまたはembedメソッドを使用しようとしました。残念ながら、これらの方法はどれも機能しません。メールに画像が送信されません。
ユーザーのパスワードを変更するためのメールを送信するために、この機能を使用しました。
public function sendResettingEmailMessage(UserInterface $user)
{
$template = $this->parameters['template']['resetting'];
$url = $this->router->generate('fos_user_resetting_reset', array('token' =>
$user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
$context = array(
'user' => $user,
'confirmationUrl' => $url,
);
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail());
}
次に、この関数を使用して電子メールのメッセージを作成します。
protected function sendMessage($templateName, $context, $fromEmail,
$toEmail)
{
$template = $this->twig->load($templateName);
$subject = $template->renderBlock('subject', $context);
$textBody = $template->renderBlock('body_text', $context);
$htmlBody = '';
if ($template->hasBlock('body_html', $context)) {
$htmlBody = $template->renderBlock('body_html', $context);
}
$message = (new \Swift_Message())
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setBody(
'<html>' .
' <body>' .
' Here is an image <img src="' .
$message-
>embed(Swift_Image::fromPath('https://via.placeholder.com/350x150')) .
'" alt="Image" />' .
' Rest of message' .
' </body>' .
'</html>',
'text/html' // Mark the content-type as HTML
);
return $this->mailer->send($message);
}
これが私のemail.html.twigです:
{% trans_default_domain 'FOSUserBundle' %}
{% block subject %}
{%- autoescape false -%}
{{ 'registration.email.subject'|trans({'%username%': user.username,
'%confirmationUrl%': confirmationUrl}) }}
{%- endautoescape -%}
{% endblock %}
{% block body_text %}
{% autoescape false %}
{{ 'registration.email.message'|trans({'%username%': user.username,
'%confirmationUrl%': confirmationUrl}) }}
{% endautoescape %}
{% endblock %}
{% block body_html %}
{% endblock %}
私の電子メールでは、本文内に単純なプレースホルダーが含まれることを期待しています。しかし、その結果、このプレースホルダーは私の電子メールに表示されませんでした。私は画像のないテキストのある本文しか持っていません。
このプレースホルダーをメールの本文に追加するにはどうすればよいですか?
返信ありがとうございます。
ドキュメントによると、これを試してみてください:
$message->attach(
Swift_Attachment::fromPath('https://via.placeholder.com/350x150')->setDisposition('inline')
)
そして、php.iniにがallow_url_fopen
設定さon
れているかどうかを確認してください
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加