70

PHP を使用して、画像付きの HTML 形式のメールを送信するにはどうすればよいですか?

メールでアドレスに送信される、いくつかの設定と HTML 出力を含むページが必要です。私は何をすべきか?

主な問題は、ファイルを添付することです。どうやってやるの?

4

8 に答える 8

157

それは非常に簡単です。サーバーに画像を残し、PHP+CSSをそれらに送信します...

$to = 'bob@example.com';

$subject = 'Website Change Reqest';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';


mail($to, $subject, $message, $headers);

メーラーと受信者に、電子メールに(うまくいけば)整形式のHTMLが含まれていることを伝え、解釈する必要があるのはこの行です。

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

これが私が情報を得たリンクです..(リンク...

ただし、セキュリティが必要になります...

于 2012-06-28T06:19:17.217 に答える
19

画像の絶対パスを使用してHTMLをコーディングする必要があります。絶対パスとは、サーバーに画像をアップロードする必要があることを意味し、画像のsrc属性では、このような直接パスを指定する必要があります<img src="http://yourdomain.com/images/example.jpg">

以下はあなたの参照のためのPHPコードです:-それはhttp://www.php.net/manual/en/function.mail.phpから取られました

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
  <p>Here are the birthdays upcoming in August!</p>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";


// Mail it
mail($to, $subject, $message, $headers);
?>
于 2012-06-28T06:21:34.590 に答える
11

私はこのコードを持っています、そしてそれは私のサイトのために完全に実行されます

  public function forgotpassword($pass,$name,$to)
    {
        $body ="<table  width=100% border=0><tr><td>";
        $body .= "<img width=200 src='";
        $body .= $this->imageUrl();
        $body .="'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
        $body .='<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
        $body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
        $body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:support@pms.com" target="_blank">support@pms.com</a></td></tr>';
        $body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
        $subject = "Forgot Password";
        $this->sendmail($body,$to,$subject);
    }

メール機能

   function sendmail($body,$to,$subject)
        {
            //require_once 'init.php';


            $from='testing@gmail.com';      
            $headersfrom='';
            $headersfrom .= 'MIME-Version: 1.0' . "\r\n";
            $headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headersfrom .= 'From: '.$from.' '. "\r\n";
            mail($to,$subject,$body,$headersfrom);
        }

画像のURL機能は、1つの機能だけで変更した画像を変更したい場合に使用します。パスワードを忘れたなどのメール機能がたくさんあります。そこにユーザーを作成します。画像のURL機能を使用してパスを直接設定できます。

function imageUrl()
    {
        return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
    }
于 2012-06-28T06:22:54.963 に答える
2

おそらく最も簡単な方法は、Zend Framework または CakePHP や Symphony などの他のフレームワークを使用することです。

標準mail機能でもできますが、写真の貼り付け方については少し知識が必要です。

または、画像を添付するのではなく、サーバー上で画像をホストするだけです。HTML メールの送信については、mail関数のドキュメントに記載されています。

于 2012-06-28T06:19:41.673 に答える
1

PHPMailerを使用し、

HTML メールを送信するには、 $mail->isHTML() のみを設定する必要があり、HTML タグで本文を設定できます

ここによく書かれたチュートリアルがあります:

rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/

于 2013-07-16T11:38:44.620 に答える