attach a file to email php

    $filename = 'myfile';
    $path = 'your path goes here';
    $file = $path . "/" . $filename;

    $mailto = '[email protected]';
    $subject = 'Subject';
    $message = 'My message';

    $content = file_get_contents($file);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (RFC)
    $eol = "\r\n";

    // main header (multipart mandatory)
    $headers = "From: name <[email protected]>" . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--" . $separator . $eol;
    $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol;
    $body .= $content . $eol;
    $body .= "--" . $separator . "--";

    //SEND Mail
    if (mail($mailto, $subject, $body, $headers)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
        print_r( error_get_last() );
    }

3
1

                                    Attach a file to email

3 (1 Votes)
0
3.71
7
T. Spikes 105 points

                                    use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email-&gt;SetFrom('[email protected]', 'Your Name'); //Name is optional
$email-&gt;Subject   = 'Message Subject';
$email-&gt;Body      = $bodytext;
$email-&gt;AddAddress( '[email protected]' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email-&gt;AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email-&gt;Send();

3.71 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
upload file as email attachment php send file to email php php form with attachment to email php mail attachment syntax mail function with attachment in php how to send attachment php php send email attachments how to send attachement with mail() php How to send email with phpmailer with attachment php send mail to file service php send mail to file send php mail from file how to send attachment in mail using phpmailer send php file as email body how to attach file in mail using mail function in php how to attach file in mail using php how to send a document via email using php attach file to mail in php send email php attached file send email with attachment phpmailer send email as attachment php send attachment smtp php php mail function send attachment mail function php attach file email header add attachment php how to send attachment in phpmailer mailjet php add attachment php send file email @mail how to send attachment in mail using php send file using mail php how to send a file in email php php mail function with html and attachment example php mail function with attachment example attachment in php mail function PHP form send email with attachment send email with attachment sendgrid php send mail attachment using mail php phpmailer send email class with attachment funtion to add attachment to mail in php how to get attachment in php send files in email in php send attachment with php mail function php send email smtp with attachment send format email with attachment php example attach files to email in php ? php send email with attachment phpmailer send file in email php how to send email attachment doc file using php send file attachments in email php how to send email with document file in php php send attachment to email after submission php file to send mail file send from php to mail sending attachments in mail php php mail send files send attachments over email using phpmailer send attachments over email using php how to add attachments in php mailer php 7 send email with attachment send file with php mailer mail in php with attachment send attachment using phpmailer email with attachment php send file with mail php php mail send attachment file php mail function attachment php mail() with attachment file how to use php mailer with attachment mail attachment in php mail attachment n php send attachment in php mail send file from $_Files['file'] to email php send attachment in mail phpmailer read attachment files email php send mail attachment with php access email attachments on server php send mail with attachement php send email attachment php send attachment in phpmailer how to add attachment in phpmailer send email with attachment in php example php sending email with attachment mail php not sending attachments send email with attached file php phpmailer send attachment from content phpmailer send attachment from form send form attachment in mail using php send attachment in mail using php php code to send attachment in email phpmailer with attachment attachment in phpmailer example php send attached file email how to use an include file when sending a email in php php to send email with attachment php email add attachment send email with attachment php sendgridemail attachment example php php to upload and email file phpmailer php to upload and email file php sendmail with attachment attach file to mail using php mailder send php file file email phpmailer mail function display html and attachment in content in php mail function in php with attachment how to put attachment in body of email php outlook php send mail with attachments php mailer add attachment from a url php mailer send attachments php mailer send attachment add attachment phpmailer mail attachment php send file in php mail php add mail string attachment php add mail attachment how to add attachment ni php to email phpmailer email attachment attachment in phpmailer attach a file in php email send emails with attachement in php send attachment in mail +php phpMailer send attachment php send email attachment email-&gt;send(); php send email attachment quick php script to send email to user send file by email php php send email with file attachment php file to send email attach file mail function php php save attachment from email how to send attachment in email using php mailer php script to send mail with attachment code php script to send mail with attachment php code to send email with attachment how to send email with attached file in php send email with attachment using php Send mail with attachment using SMTP in PHP smtp mail with attachment in php send attachment php mail send attachment php generate file and attach it to email php send mail php with attachment file attachment email in php hwo to send attachment in php email by mail method how to send attachment in mail in php sent email with attachment in php Send attachments and html with PHP Mail mail with attachment in php phpmailer send with attachment send mail with attachment in phpmailer php mail() example with attachments php mail() with attachment php mail attachment form option send email with attachment image in php How to send attachment in email in PHP php mail attachment options php send email with attachment from form sending email with attachment in php php add file to mail function php mail function with attachment send attachment in mail php mailer php email attachment script php mail attachment script How to attach files and send mail in PHP using mail function in php send attachment send attachment php mail form php send attachment php mail form attach document in email using php read email attachment php insert a file to a mail php attach a file to a mail php php script to send email with attachment from form php get email attachment add attachment in mail using php php send mail with attachment example how to send attached file email in php Send Email with Attachment in PHP using PHPMailer Send Email with Attachment in PHP using PHPMailer] php mail attachments how to send attachment file in mail in php mail php send attachment attachemnt php php send mail with excel attachment smtp mail function in php for file upload php mailer attach file upload file send to email php how to send email using SendGrid from PHP send email using php how to send mail with attachment in php send mail including from php send a file using mail() php html code to send email with attachment php email with attachment php mail attechment php file attechment email php mail file attachment php email send with attachment php form email send php send to email php contact form send email php send email from server function to send email in php phpmailer send email with attachment how to send email with attachment php email php send email with attached file php send email with attachment example add attachment in php how to send file with mail in php how to send an email with php php form to send email how to sent attachment send email in php send mail using php mail file in php unable to send email using php mail() attach file in phpmailer add attachment in phpmailer send mail php php send emails from an outlook account php send email smtp send email in php javatpoint sending html form data to an email address without php how to send email from a website using php phprad send mail send html email in php php add attachment to email php send mail yahoo how to send mail in php how to send an email using php php change sender fo mail)( fundtion send mail from contact form php send email php smtp gmail php get email from server php send mail php mail send can you send a email via php without having email plugin php create pdf and attach to email php email send html view php send emails php send file to email php sending email php sending an email php send email gmail phpmailer send image attachment php send email example php send and receive email php send data to email imap search php get attachments adding html upload attachment to php mail adding an upload attachment to mail php php mailer send email php email says there is an attachment php html email template with attachment adding attachments from html to phpmail sending an email with a file with php php send email send a attachment through php imap php get attachments file attachment in php mail function send email and attache file in php ad atatchmen mail php php send email text file php email attach() phpmailer add attachment php auto send email attachment Attach something to an email PHP javascript sending mail with attachment php php mail send with file phpsendmail with attachment add file to mail php php mail attachment file php how to attache file in mail attach document php mail change attachment in sent email in php send mail with attachment in php send file mail php read email attachment php imap get attachement imap email example file php how to send email with attachment using php php add file to email php mail send file php mail attach file send attachments with php mail() insert file as email body php send attachment file and body text with email in php send file with attachment email in php mail attachment in php with link mail function attached file php attachment mail add attachment in to custom email in php send attachment using mail function in php php code for email with attachment php send mail with attachment php attachment Send Attachment in php mail code php send attachment with email as link php email attachment attach a file to email php mail-&gt;send with attachment in php demo how to send video throught mail in php how to add attachment to email from html send attachment in mail php 7 + php send mail attachment send php file to email send content php file to email how to get attachments to emails php php mail add attachment php mail with attachment email attachment php php mail send attachment mail php attachment php mail attach file from server $mail- addattachment php mail_attachment function php phpmailer mail_attachment function php phpmail php mail attachment php how to send file to email php mail() attachment send email with file attachment php send a file mail php send files to email php how to send attachment file in mail php send attachment in mail php sending file attachment php mail laravel mail send attachment send mail with view laravel send javascript in nodemailer how to send email through python html form send email c# code to send email using smtp php mail upload attachment sending attachments php php send attachment with email how to send file attachment in php php attach file to email php send email with attachment html file send fil to mail with php send email with attachment in php send file email php php send file email php send email with attachment
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source