2011-09-02 43 views
5

添付ファイル付きのHTMLメールを送信するPHPスクリプトがあります。しかし、添付ファイルを電子メール本文の<img>タグで表示することはできません。添付ファイルはpostcard.png、サーバー上の元のファイル名は4e60348f83f2f.pngです。画像URLにさまざまなものを指定してみました:cid:postcard.pngcid:4e60348f83f2f.pngpostcard.png4e60348f83f2f.png何も動作しません。PHPでインライン添付画像付きのHTMLメールを送信する方法

私はこれはそれの代わりに私が使用することができ、インライン添付ファイルの分離した添付ファイルになりので、私が間違ってやっている重要な部分は、ここにあると思い

Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="$fname" // i.e.: "postcard.png" 

私はそれを使用するように変更してみましたCIDは、私は実際にそれを行う方法がわからない、とすべてで、このdidntの」作品:ここ

Content-Transfer-Encoding: base64 
Content-ID: <$fname> // i.e.: postcard.png 

は完全なコードです。(それは、PHP mail()ページ内のコメントからthis codeに基づいています。)

<?php 
$to  = "[email protected]"; 
$email = "[email protected]"; 
$name = "Namename"; 
$subject = "An inline image!"; 
$comment = "Llookout <b>Llary</b> it's <br> the <b>Ll</b>andllord!<br><img src='cid:postcard.png'><br><img src='cid:4e60348f83f2f.png'><img src='postcard.png'><br><img src='4e60348f83f2f.png'>"; 

$To   = strip_tags($to); 
$TextMessage =strip_tags(nl2br($comment),"<br>"); 
$HTMLMessage =nl2br($comment); 
$FromName =strip_tags($name); 
$FromEmail =strip_tags($email); 
$Subject  =strip_tags($subject); 

$boundary1 =rand(0,9)."-" 
    .rand(10000000000,9999999999)."-" 
    .rand(10000000000,9999999999)."=:" 
    .rand(10000,99999); 
$boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-" 
    .rand(10000000000,9999999999)."=:" 
    .rand(10000,99999); 

$filename1 = "4e60348f83f2f.png"; //name of file on server with script 
$handle  =fopen($filename1, 'rb'); 
$f_contents =fread($handle, filesize($filename1)); 
$attachment=chunk_split(base64_encode($f_contents)); 
fclose($handle); 

$ftype  ="image/png"; 
$fname  ="postcard.png"; //what the file will be named 


$attachments=''; 
$Headers  =<<<AKAM 
From: $FromName <$FromEmail> 
Reply-To: $FromEmail 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
    boundary="$boundary1" 
AKAM; 

$attachments.=<<<ATTA 
--$boundary1 
Content-Type: $ftype; 
    name="$fname" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="$fname" 


$attachment 

ATTA; 


$Body  =<<<AKAM 
This is a multi-part message in MIME format. 

--$boundary1 
Content-Type: multipart/alternative; 
    boundary="$boundary2" 

--$boundary2 
Content-Type: text/plain; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$TextMessage 
--$boundary2 
Content-Type: text/html; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$HTMLMessage 

--$boundary2-- 

$attachments 
--$boundary1-- 
AKAM; 

// Send email 
$ok=mail($To, $Subject, $Body, $Headers); 
echo $ok?"<h1> Mail sent!</h1>":"<h1> Mail not sent!</h1>"; 
?> 
+2

[PHPMailer](http://phpmailer.worxware.com)または[Swiftmailer](http://swiftmailer.org)を使用してください。どちらも、最初からMIMEメッセージを作成するのとは違って、まったく痛みのないインライン添付を可能にします。 –

+0

手で行うことは不可能ではありませんが、SwiftmailerやPHPMailerを使用する方が面倒なことはありません。 - [PHPMailerで電子メールを送信 - ボディに画像を埋め込む]の可能な複製(http://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body) – mario

+1

@Marc B:私は気にしない、PHPMailerで一度入力したハングを引っ張ってしまった。 –

答えて

14

私は結局のところ、非常に簡単であることが分かった。 This pageは私がそれを理解するのに役立ちましたが、私はそれを下で行うために必要な部分を実証します。

まず第一には、境界文字列の作成があります、そして画像は、正しくエンコードとチャンク:電子メールのHTML部分で

// Create a boundary string. It needs to be unique (not in the text) so ... 
// We are going to use the sha1 algorithm to generate a 40 character string: 
$sep = sha1(date('r', time())); 

// Also now prepare our inline image - Also read, encode, split: 
$inline = chunk_split(base64_encode(file_get_contents('figure.gif'))); 

が、画像はこのように参照されている(境界文字列を使用して):

--PHP-related-{$sep} 
Content-Type: image/gif 
Content-Transfer-Encoding: base64 
Content-ID: <PHP-CID-{$sep}> 
{$inline} 

<img src="cid:PHP-CID-{$sep}"> 

次に、あなたは、このように、インライン添付ファイル用のHTML部分下記のメールの別の部分を作成します

...それはそうです! PHPmailerやその他のライブラリを実装するよりも簡単です。これがすべての場合です。より複雑な作業のために間違いなく、それらのライブラリの1つを入手したいと思うでしょう。

+0

この回答に基づいて添付することはできません。このようにします:http://pastebin.com/z8LqAHCB –

0

PHPMailerの使用をお勧めしますが、私はこの簡単な修正を提案したくありません。

imgタグの中に絶対URLを入れると、HTMLメールを送信すれば動作します。

+1

ありがとう、しかし、私は、電子メールを送信した直後にサーバーからイメージを削除しているので、多くの電子メールクライアントが電子メールでホットリンクを許可しないため、機能しません。 – brentonstrine

+0

この方法を使用すると、画像はさまざまな電子メールクライアントでブロックされます – ljelewis

関連する問題