Hi,
I am uploading a file(text,doc) in php.its working fine.
I want to send this uploaded file as attached file to mail function is working fine.but i don,t know how to attach that uploaded file.
need help.
Thanks
Hi,
I am uploading a file(text,doc) in php.its working fine.
I want to send this uploaded file as attached file to mail function is working fine.but i don,t know how to attach that uploaded file.
need help.
Thanks
Short answer: if your upload already works, hand the uploaded file (the tmp_name) to a mailer and let the library build the MIME parts. was right to recommend using a library; ’s raw mail() example shows how multipart MIME works but is easy to get wrong. For reliability and security use a maintained library (PHPMailer, Symfony Mailer, etc.) and send via SMTP rather than trying to craft boundaries yourself.
Quick checklist before attaching:
$_FILES['file']['error'] === UPLOAD_ERR_OK and is_uploaded_file()).finfo_file() (do not trust $_FILES['file']['type']).upload_max_filesize / post_max_size).$name = basename($_FILES['file']['name']); and strip odd chars).move_uploaded_file() if you want to keep a permanent copy; otherwise attach from the tmp_name immediately.Example (PHPMailer) — attach the uploaded file by path:
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'smtp-user';
$mail->Password = 'smtp-pass';
$mail->setFrom('webmaster@example.com');
$mail->addAddress('admin@my');
$mail->Subject = 'Uploaded file';
$mail->Body = 'See attached';
$mail->addAttachment($_FILES['file']['tmp_name'], basename($_FILES['file']['name']));
if (! $mail->send()) {
error_log('Mail error: ' . $mail->ErrorInfo);
} Troubleshooting and cautions: attachments may be rejected if too large or if your server’s MTA (sendmail/postfix/exim) blocks them; check mail logs and SMTP debug output. Always sanitize filenames and validate MIME types; consider virus-scanning or quarantining uploads before emailing them. For PHP upload details see the official docs: PHP file upload basics. For a modern mail library see PHPMailer.
Jump to Post— petr.pavel 19Hi Jacob, the easiest way would be to use some mailing library like XPertMailer http://www.xpertmailer.com/. It would take care of all the boring stuff related to sending attachments or html e-mails or validating …
Hi Jacob, the easiest way would be to use some mailing library like XPertMailer http://www.xpertmailer.com/. It would take care of all the boring stuff related to sending attachments or html e-mails or validating e-mail addresses, or even protecting yourself from e-mail injection attack.
If you however, insist on implementing it yourself, here's a good tutorial:
http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php
It's the #1 hit in Google when you search for "send attachment php".
Petr
Can anyone have code to send attached file in php.
Thanks
If you can't click the link provided and scroll down, here's the code it gives on emailing attachments(stripped of comments):
<?php
$to = 'youraddress@example.com';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--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="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?> We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.