1

画像を添付ファイルとして電子メールに埋め込む方法は知っていますが、それは本当にやりたいことではありません。私がやりたいことは、メッセージに画像を埋め込み、それを電子メール メッセージ自体の本文内で使用することです。これを行うか、添付ファイルを参照してメッセージで使用することは可能ですか?

これについても心配する必要がありますか?ネットワーク上の画像へのリンクを介してそれらをロードする方が有益ですか? これにより、サーバーの負荷が軽減されるので、メールを開くたびにサイトから直接ダウンロードする必要がなくなり、帯域幅が少し減少すると考えていました。

注:このタスクを達成するための簡単な方法のためだけに、メール フレームワークやライブラリなどを探しているわけではありません。

4

3 に答える 3

3

SwiftMailer やその他のライブラリを使用するように人々が声をかけてきます。しかし、それはそれほど難しいことではありません。それは本当に厄介ですが、難しいことではありません。以下は私が使用する関数です。少し匿名化しましたが、何も壊れていないことを願っています。HTMLメッセージ(別の関数から取得)で、添付の画像に次のようにリンクします。

$output .= '<p><img src="cid:' . $linkID . '" alt="graph" /></p>';

以下$linkIDの関数で使用されているのと同じ値です。

public function sendEmail($emailAddress)
{
    $random_hash = md5(date('r', time()));

    $htmlString = $this->getHTML($random_hash);

    $message = "--mixed-$random_hash\n";
    $message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n";
    $message .= 'MIME-Version: 1.0' . "\n";
    $message .= '--alt-' . $random_hash . "\n";
    $message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";

    // text message
    $message .= strip_tags($htmlString) . "\n\n";

    $message .= '--alt-' . $random_hash . "\n";
    $message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n";
    // $message .= 'MIME-Version: 1.0' . "\n";
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";

    // html message
    $message .= $htmlString . "\n\n";

    $message .= '--alt-' . $random_hash . '--' . "\n";

    // graph image
    if($this->selectedGraph != 0)
    {
        $graphString = $this->getGraph();
        $graphString = chunk_split(base64_encode($graphString));

        $linkID = 'graph-' . $random_hash . '-image';

        $message .= '--mixed-' . $random_hash . "\n";
        $message .= 'MIME-Version: 1.0' . "\n";
        $message .= 'Content-Transfer-Encoding: base64' . "\n";
        $message .= 'Content-ID: ' . $linkID . "\n";
        $message .= 'Content-Type: image/gif; name="graph.gif"' . "\n";
        $message .= 'Content-Disposition: attachment' . "\n\n";

        $message .= $graphString;
        $message .= '--mixed-' . $random_hash . '--' . "\n";

    }
    else
    {
        $message .= '--mixed-' . $random_hash . '--' . "\n";
    }


    $headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto;
    $headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0";

    $flags = '-f ' . BOUNCED_EMAIL_ADDRESS;

    return mail($emailAddress, $this->subject, $message, $headers, $flags);

}

やる価値があるかどうかは別の問題です。画像を添付すると、メッセージを送信するために帯域幅が使用されます。帯域幅を分散して使用するよりも、一度に使用した方がよいのではないでしょうか? そうでないかもしれない。

受信者ごとに異なる画像を送信する必要があるまで、添付画像を使用しませんでした. これらすべての画像をサーバーに保存して管理するつもりはありませんでした (そして、それらに一意の名前を付けようとしました)。

于 2010-06-22T22:50:10.710 に答える
2
Content-Type: multipart/related;
  type="text/html";
  boundary="Boundary1"


--Boundary1
Content-Type: multipart/alternative;
boundary="Boundary2"

--Boundary2
Content-Type: text/html; charset = "utf-8"
Content-Transfer-Encoding: 8bit

<img src="cid:content_id_from_attachment">

--Boundary2--

--Boundary1
Content-Type: image/jpeg; name="somefilename.jpg"
Content-Transfer-Encoding: base64
Content-ID: <content_id_from_attachment>
Content-Disposition: inline; filename="somefilename.jpg"

//binary data

--Boundary1--

もちろん、境界、文字セット、Content-Transfer-Encoding はすべて選択可能です。PHPMailer パッケージは、これを自動的に行うことができます: http://www.ustrem.org/en/article-send-mail-using-phpmailer-en/

次の画像も参照してください: http://mailformat.dan.info/body/html.html

于 2010-06-22T22:57:59.907 に答える
-4

これを行う唯一の方法は、次のように URL で画像を参照することだと思います。

<img src="http://www.youwebsite.com/yourimage.jpg" alt="This is a description" />
于 2010-06-22T22:48:07.457 に答える