simple way to add attachment to email
Moderator: General Moderators
simple way to add attachment to email
Hi, I am looking for an easy way to send an attachment to an email form in php. How do I specify the path and so forth. I tried AddAttachmnet ($path); but it didnt do the job and made the rest of the code unusable. Thanks
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
I built this function long time ago similar to PHPMailer class...It is better to use PHPMailer class to send mails because they know the best way to help reach your email to other email servers...anyway...here is it...I hope you would know to replace the variables I have used....
Code: Select all
//Build all attachments and add it to the message that has to be sent in the mail
function buildAttachments(){
foreach ($this->files as $file ){
$message .= "\r\nContent-Type: {$file['MIME']}; name=\"{$file['NAME']}\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"{$file['NAME']}\"\r\n\r\n";
$base64 = base64_encode($file['FILE']);
for ( $i=0; $i < strlen($base64) / 70; $i++ ){ //line size is set to 70
$message .= substr( $base64, $i*70, 70)."\r\n";
}
}
return $message;
}sorry, I am a little new here. Can you tell my exactly how to specify the path to a file on my server to include. For example, do I put quotations around the "http://mysite.com/images/sample.png" in the part where its says FILE. Thanks for quick response, I really appreciate it.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
<?
$sendTo = "webmaster@flashmailforms.com";
$subject = "Information From Flash Site";
$ip = $_SERVER['REMOTE_ADDR'];
$headers .= "From: " . $_POST['name'] . ">nl2br";
$headers .= "Reply-To: " . $_POST['email'] . ">nl2br";
$message = $_POST['message'] . "
" . "Sender's ip address is " .
nl2br($ip);
mail($sendTo, $subject, $message, $headers);
?>- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
I am not sure this would work. By the way, why you are using nl2br at a few places???
Code: Select all
<?
$sendTo = "webmaster@flashmailforms.com";
$subject = "Information From Flash Site";
$ip = $_SERVER['REMOTE_ADDR'];
$headers .= "From: " . $_POST['name'] . ">nl2br";
$headers .= "Reply-To: " . $_POST['email'] . ">nl2br";
$message = $_POST['message'] . "
" . "Sender's ip address is " .
nl2br($ip);
if (areAttachmentsAvailable === TRUE){// you have to set this variable if you want to attach files
$message .= "--".md5( uniqid("myboundary")."\n";
$message .= buildAttachments ('application/x-msword', "" , ""); //look out for available mimes
}
mail($sendTo, $subject, $message, $headers);
function buildAttachments($mime,$name, $filePath){
$message = "\r\nContent-Type: {$mime}; name=\"{$name}\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n\r\n";
$base64 = base64_encode($filePath);
for ( $i=0; $i < strlen($base64) / 70; $i++ ){ //line size is set to 70
$message .= substr( $base64, $i*70, 70)."\r\n";
}
return $message;
}
?>