How to Send Email With Attachment in PHP

In this article, you will learn how to send email with attachment in PHP. You need to ensure that you have configured your local installation…

The post How to Send Email With Attachment in PHP appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to send email with attachment in PHP. You need to ensure that you have configured your local installation for sending and receiving emails.

Send Email With Attachment

In order to send email with attachment, you can use the mail() method.

// Name of file attachment
$filename = "test.txt";

// Local path to file attachment
$path = "C:\Users\Codesource\Desktop";

// Full local path to file attachment
$file = $path.$filename;

// File contents
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);

// Sender's name
$from_name = "Codesource";

// Sender's email
$from_mail = "codesource@example.com";

// Recipient's email
$reply_to = "recipient@example.com";

// Recipient's email
$mail_to = "recipient@example.com";

// Email Subject
$subject ="Email with Attachment";

// Headers for email
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$reply_to."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// Messages and attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";

// If email with attachment has been sent
if (mail($mail_to, $subject, $nmessage, $header)) {

	// Display sent message
    echo "The email with attachment has been sent.";
    
// Else if email with attachment cannot be sent
} else {

	// Display failed to be sent message
    echo "The email with attachment failed to be sent";
}

Note: The mail() method functions by sending an email directly from a script.

The post How to Send Email With Attachment in PHP appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-03-02T12:29:39+00:00) How to Send Email With Attachment in PHP. Retrieved from https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/

MLA
" » How to Send Email With Attachment in PHP." Ariessa Norramli | Sciencx - Tuesday March 2, 2021, https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/
HARVARD
Ariessa Norramli | Sciencx Tuesday March 2, 2021 » How to Send Email With Attachment in PHP., viewed ,<https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Send Email With Attachment in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/
CHICAGO
" » How to Send Email With Attachment in PHP." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/
IEEE
" » How to Send Email With Attachment in PHP." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/. [Accessed: ]
rf:citation
» How to Send Email With Attachment in PHP | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/03/02/how-to-send-email-with-attachment-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.