web 2.0

How to use PHP & Send Email Attachment File

How to use PHP & Send Email Attachment File This the tutorial/example a scripts how to use PHP and send mail/email and  Attachment File from php Scirpt.

ShotDev Focus:
- PHP & Send Mail (Attachment File)

Example 1

php_sendmail_attachment1.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$strTo = "Mr.Member ShotDev.Com<member@shotdev.com>";
$strSubject = "Test sending mail.";
$strMessage = "My Body & <b>My Description</b>";

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";
$strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>\n";
$strHeader .= "Bcc: webmaster@shotdev.com";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 //
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Files 1 ***//
$strFilesName1 = "my1.txt";
$strContent1 = chunk_split(base64_encode(file_get_contents($strFilesName1)));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName1."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName1."\"\n\n";
$strHeader .= $strContent1."\n\n";

//*** Files 2 ***//
$strFilesName2 = "my2.txt";
$strContent2 = chunk_split(base64_encode(file_get_contents($strFilesName2)));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName2."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName2."\"\n\n";
$strHeader .= $strContent2."\n\n";

$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //
if($flgSend)
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
?>
</body>
</html>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_sendmail_attachment1.php

Screenshot

PHP Send Mail (Attachment File)

PHP Send Mail (Attachment File)

.

Example 2 (Multiple Attachment With Array)

php_sendmail_attachment2.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$strTo = "Mr.Member ShotDev.Com<member@shotdev.com>";
$strSubject = "Test sending mail.";
$strMessage = "My Body & <b>My Description</b>";

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";
$strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>\n";
$strHeader .= "Bcc: webmaster@shotdev.com";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 //
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Attachment Files ***//
$arrFiles[] = "my1.txt";
$arrFiles[] = "my2.txt";
$arrFiles[] = "my3.txt";
$arrFiles[] = "my4.txt";
$arrFiles[] = "my5.txt";

for($i=0;$i<count($arrFiles);$i++)
{
if(trim($arrFiles[$i]) != "")
{
$strFilesName = $arrFiles[$i];
$strContent = chunk_split(base64_encode(file_get_contents($strFilesName)));

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No show error //
if($flgSend)
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
?>
</body>
</html>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_sendmail_attachment2.php

Screenshot

PHP Send Mail (Attachment File)

PHP Send Mail (Attachment File)
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

One Response to “How to use PHP & Send Email Attachment File”

  1. 1extension…

Leave a Reply

You must be logged in to post a comment.