PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
//Hi I am trying to send this email form with attachments it can send mail with attachement but cannot attach from browser
//define the receiver of the email
$to = 'calvinebarongo@gmail.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: calvine_barongo@yahoo.com\r\n Reply-To: recruitment@oldmutualkenya.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('file.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="dda2.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
$message = "Hey Check this attachments";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
To attach the file file.pdf it needs to found on the server itself. So if you're trying to create a send attachment where the user select the file that's on his pc to send it via email as attachment you need an upload script, that will upload it in a temp file then delete it once the email has been sent.
@Rodgers thanks so much I wanted to include this html code but i am having issues
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<table width="341" border="0">
<tr>
<td width="81">Email to </td>
<td width="250">
<label>
<input type="text" name="textfield" />
</label> </td>
</tr>
<tr>
<td>File</td>
<td><label>
<input type="file" name="attach" />
</label></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
How do I upload or create the upload script do you have a working code you email me on calvinebarongo@gmail.com
//target where you uploaded file will be uploaded.
$target = "tmp/";
//checking if folder tmp exists
if(!file_exists($target))
{
mkdir("tmp/") or die ("Unable to create Directory tmp/");
}
//You might want to restrict people from upload all kind of file.
//The extension list below is every file that normal user (i.e. Not Hackers) will try to upload
$allowedExtensions = array("txt","csv","htm","html","xml",
"css","doc","xls","rtf","ppt","pdf","swf","flv","avi",
"wmv","mov","jpg","jpeg","gif","png");
//Checking if file is in the allowedExtensions lists
if (!in_array(end(explode(".",strtolower($_FILES['attach']['name']))),$allowedExtensions)) {
die($file['name'].' is an invalid file type!<br/><a href="javascript:history.go(-1);"> << Go Back</a>');
}
//Starting the upload reading the filename being uploaded and uploading the file to tmp/
$target = $target.basename( $_FILES['attach']['name']);
if(move_uploaded_file($_FILES['attach']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['attach']['name']). " has been uploaded";
}else{
echo "Sorry, there was a problem uploading your file.";
}
<?php
//target where you uploaded file will be uploaded.
$target = "tmp/";
//checking if folder tmp exists
if(!file_exists($target))
{
mkdir("tmp/") or die ("Unable to create Directory tmp/");
}
//You might want to restrict people from upload all kind of file.
//The extension list below is every file that normal user (i.e. Not Hackers) will try to upload
$allowedExtensions = array("txt","csv","htm","html","xml",
"css","doc","xls","rtf","ppt","pdf","swf","flv","avi",
"wmv","mov","jpg","jpeg","gif","png");
//Checking if file is in the allowedExtensions lists
if (!in_array(end(explode(".",strtolower($_FILES['attach']['name']))),$allowedExtensions)) {
die($file['name'].' is an invalid file type!<br/><a href="javascript:history.go(-1);"> << Go Back</a>');
}
//Starting the upload reading the filename being uploaded and uploading the file to tmp/
$target = $target.basename( $_FILES['attach']['name']);
if(move_uploaded_file($_FILES['attach']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['attach']['name']). " has been uploaded";
}else{
echo "Sorry, there was a problem uploading your file.";
}
//Hi I am trying to send this email form with attachments it can send mail with attachement but cannot attach from browser
//define the receiver of the email
$to = 'calvinebarongo@gmail.com';
//define the subject of the email
$subject = 'Test email with attachment';
$from='calvine_barongo@yahoo.com';//Change this to email you're using.
$reply_address="recruitment@oldmutualkenya.com";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$semi_rand = md5(time());
$headers = "From: ".$from ;
$parameters = "-r ".$reply_address;
//define the headers we want passed. Note that they are separated with \r\n
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//$email_txt .= $msg_txt;
//define the body of the message.
ob_start(); //Turn on output buffering
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
$message = "Hey Check this attachments";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$fileatt=$target;
$fileatt_type = "application/octet-stream"; // File Type
$start= strrpos($fileatt, '/');
// == -0 ? strrpos($attachment, '//') : strrpos($attachment, '/');
$fileatt_name= substr($fileatt, $start+1, strlen($fileatt)); // Filename that will be used for the file as the attachment
//echo "<br>$fileatt is ".$fileatt_name."Starting at $start<br>";
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message.= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
"Content-Disposition: attachment;\n" .
//" name=\"{$fileatt_name}\"\n" .
" filename=\"$fileatt_name\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
$email_message.="--{$mime_boundary}--\n";
//send the email
$mail_sent = @mail($to, $subject, $email_message, $headers,$parameters);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>