Attach files in email - PHP

Attach files in email – PHP

0
(0)

In this tutorial, we will teach you how you can attach files in an email using PHP. We will be using PHPMailer library to send an email.

Attach files in email – PHP

We will be creating a simple form to enter email address of receiver, subject, message and ability to select a file. That file will be attached in a email and sent to the receiver. If you are testing via XAMPP/WAMP/MAMP you may need to download PHPMailer library from the link below:

Download PHPMailer

You can install the library from composer, all details are provided in the link above. We will be using 2 files, 1 to display a form for input and 2nd to send the mail.

Getting user input

Below is the code for index.php file

<form method="POST" enctype="multipart/form-data" action="send-mail.php">
    <p>
	Send to:
	<input type="text" name="receiver">
    </p>

    <p>
	Subject:
	<input type="text" name="subject">
    </p>

    <p>
	Message:
	<textarea name="message"></textarea>
    </p>

    <p>
	Select file:
	<input type="file" name="file">
    </p>

    <input type="submit">
</form>

Attaching file with email

Sending email via localhost requires an Gmail account and you need to enable less secure apps for your account. You can enable it from the link below:

Enable less secure apps

Now paste the following code in your send-mail.php file:

<?php

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 0;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp.gmail.com;';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'your_gmail_ID';                     // Your gmail address
    $mail->Password   = 'your_gmail_password';                               // Your gmail password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('your_gmail_ID', 'your_name');
    $mail->addAddress($_POST["receiver"]);

    $file_name = $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], $file_name);
    $mail->addAttachment($file_name);

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $_POST["subject"];
    $mail->Body    = $_POST["message"];

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

If you are using Mac, you may need to enable folder permission. You can enable folder permission by following steps below:

Folder permission for Mac users
Folder permission for Mac users
Folder permission for Mac users
Folder permission for Mac users

Now go ahead and try to submit the form and attaching any file to it. You will have attached file in your inbox (or in your spam if using localhost).

Instead of attaching the file in email, you can also upload the file on your server. And share its download link via an email. Learn here how you can do that.

How useful was this post

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post





2 Replies to “Attach files in email – PHP”

Leave a Reply

Please disable your adblocker or whitelist this site!