开发者

Send smtp mail in php with HTML page attach as a text

I have a requirement of sending mail using smtp server in php.

Now I am able to send the mail using smtp for a plain text.

but I have a requirement where I need to attach an HTML page, which includes set of images.

Now for that I am trying the following code :

<?php
require_once "Mail.php";
$to      = 'test@test.com';
$from    = 'test@test.com';
$subject = $_POST['subject'];
$body = $_POST['message'];

$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);

if (is_uploaded_file($fileatt)) {
    echo("<p>Inside 1</p>");
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    // Generate a boundary string
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    array_push(&$headers, 'MIME-Version: 1.0');
    array_push(&$headers, 'Content-Type: multipart/mixed;');
    array_push(&$headers, " boundary=\"{$mime_boundary}\"");

    echo("<p>Inside 2</p>");

    $body = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
    $body . "\n\n";

    echo("<p>Inside 3</p>");

    $data = chunk_split(base64_encode($data));

    echo("<p>Inside 4</p>");

    $body .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
              "--{$mime_boundary}--\n";

    echo("<p>Inside 5</p>");
}
$host = "user@test.com";
$username = "user@test.com";
$password = "user";
$smtp = Mail::factory('smtp',
    array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
} else {
    echo("<p>Message successfully sent!</p>");
}
?>

Now this code works fine for me, and it's sending the mail to the target email address.

But when I open this email in the inbox, it's showing me the following text in the mailbox:

This is a multi-part message in MIME format.

--==Multipart_Boundary_x368d72fe1ff44518e90537abdb4bf029x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

test 1011

--==Multipart_Boundary_x368d72fe1ff44518e90537abdb4bf029x
Content-Type: text/html;
 name="mailing.html"
Content-Transfer-Encoding: base64

PCFET0NUWVBFIGh0bWwgUFVCTE开发者_如何学编程lDICItLy9XM0MvL0RURCBYSFRNTCAxLjAgVHJhbnNpdGlvbmFs
Ly9FTiIgImh0dHA6Ly93d3cudzMub3JnL1RSL3hodG1sMS9EVEQveGh0bWwxLXRyYW5zaXRpb25h
bC5kdGQiPg0KPGh0bWwgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGh0bWwiPg0KPGhl
YWQ+DQo8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsg
Y2hhcnNldD11dGYtOCIgLz4NCjx0aXRsZT5QZWVrYWJvb0JlYW5zPC90aXRsZT4NCjxzdHlsZSB0
eXBlPSJ0ZXh0L2NzcyI+DQo8IS0tDQpib2R5IA0KYTpsaW5rIHsNCgl0ZXh0LWRlY29yYXRpb246
IG5vbmU7DQp9DQphOnZpc2l0ZWQgew0KCXRleHQtZGVjb3JhdGlvbjogbm9uZTsNCn0NCmE6aG92
ZXIgew0KCXRleHQtZGVjb3JhdGlvbjogbm9uZTsNCn0NCmE6YWN0aXZlIHsNCgl0ZXh0LWRlY29y
YXRpb246IG5vbmU7DQp9DQotLT4NCjwvc3R5bGU+PC9oZWFkPg0KDQo8Ym9keSBzdHlsZT0ibWFy
Z2luLWxlZnQ6IDBweDsNCgltYXJnaW4tdG9wOiAwcHg7DQoJbWFyZ2luLXJpZ2h0OiAwcHg7DQoJ
bWFyZ2luLWJvdHRvbTogMHB4OyI+DQo8dGFibGUgd2lkdGg9IjEwMjQiIGJvcmRlcj0iMCIgYWxp
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suY29tL3BhZ2Vz
L1BlZWthYm9vLUJlYW5zLzE2ODgyMDU5NTA0MD9yZWY9c2VhcmNoJnNpZD02OTE4MDMxMTcuMTA3
ODQ2MDQ5NC4uMSI+PGltZyBzcmM9ImltYWdlcy9zdWJzY3JpYmVyLWh0bWxfMjMuanBnIiBhbHQ9
IiIgd2lkdGg9IjQ3IiBoZWlnaHQ9IjQ4IiBib3JkZXI9IjAiPjwvYT48L3RkPg0KICAgICAgICAg
IDwvdHI+DQogICAgICAgICAgPHRyPg0KICAgICAgICAgICAgPHRkICB3aWR0aD0iNDciIGhlaWdo
dD0iMTUiIGFsaWduPSJsZWZ0IiB2YWxpZ249InRvcCI+PGltZyBzcmM9ImltYWdlcy9zdWJzY3Jp
YmVyLWh0bWxfMjYuanBnIiB3aWR0aD0iNDciIGhlaWdodD0iMTUiIGFsdD0iIj48L3RkPg0KICAg
ICAgICAgIDwvdHI+DQogICAgICAgIDwvdGFibGU+PC90ZD4NCiAgICAgICAgPHRkIHdpZHRoPSIy
NjYiIGhlaWdodD0iNjIiIGFsaWduPSJsZWZ0IiB2YWxpZ249InRvcCI+PGltZyBzcmM9ImltYWdl
cy9zdWJzY3JpYmVyLWh0bWxfMjQuanBnIiB3aWR0aD0iMjY2IiBoZWlnaHQ9IjYyIiBhbHQ9IiI+
PC90ZD4NCiAgICAgIDwvdHI+DQogICAgPC90YWJsZT48L3RkPg0KICA8L3RyPg0KPC90YWJsZT4N
CjwvYm9keT4NCjwvaHRtbD4NCg==


--==Multipart_Boundary_x368d72fe1ff44518e90537abdb4bf029x--

So, it's clearly showing me the encoded data.

So, what should modify to send the proper html page that should be visible in targeted email's inbox?


Why do you want to reinvent the wheel? There's lots of different implementations that allow you to send HTML mail with attachments, ie. Zend_Mail, PHPMailer, ... . Use one of these, and save yourself from headaches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜