How to send file via mail using curl in php
You can send a file via email using curl
in PHP by creating a multipart email message and sending it using the curl
command. Here's an example:
<?php
// Set the email recipient and subject
$to = '[email protected]';
$subject = 'File attachment';
// Set the file path and name
$file_path = 'path/to/file.txt';
$file_name = 'file.txt';
// Set the email body
$body = 'This is the email body.';
// Create the multipart email message
$headers = array(
'From: [email protected]',
'To: '. $to,
'Subject: '. $subject,
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; boundary="===============1234567890=="'
);
$message = "--===============1234567890==\r\n";
$message.= 'Content-Type: text/plain; charset=UTF-8\r\n';
$message.= 'Content-Transfer-Encoding: 7bit\r\n';
$message.= $body. "\r\n";
$message.= "--===============1234567890==\r\n";
$message.= 'Content-Type: application/octet-stream; name="'. $file_name. '"\r\n';
$message.= 'Content-Disposition: attachment; filename="'. $file_name. '"\r\n';
$message.= 'Content-Transfer-Encoding: base64\r\n';
$message.= chunk_split(base64_encode(file_get_contents($file_path))). "\r\n";
$message.= "--===============1234567890==--\r\n";
// Send the email using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'smtp://smtp.example.com');
curl_setopt($ch, CURLOPT_PORT, 587);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
// Check the response
if (curl_errno($ch)) {
echo 'Error: '. curl_error($ch);
} else {
echo 'Email sent successfully!';
}
?>
In this example, we create a multipart email message with a text part and a file part. We use the chunk_split
function to split the file contents into chunks of 76 characters each, and the base64_encode
function to encode the file contents in base64. We then set the Content-Type
and Content-Disposition
headers for the file part, and add the file contents to the message.
We then use the curl
command to send the email to the specified recipient. We set the CURLOPT_URL
option to the SMTP server URL, the CURLOPT_PORT
option to the SMTP port, and the CURLOPT_RETURNTRANSFER
option to true
to return the response as a string. We set the CURLOPT_POST
option to true
to send the email as a POST request, and the CURLOPT_POSTFIELDS
option to the email message. We also set the CURLOPT_HEADER
option to true
to include the response headers in the output.
Finally, we check the response from the SMTP server using the curl_errno
and curl_error
functions, and display a success message if the email is sent successfully.
Note that this example assumes that you have a SMTP server set up and configured to send emails. You may need to modify the CURLOPT_URL
and CURLOPT_PORT
options to match your SMTP server's settings. Additionally, you should ensure that the file path and name are correct and that the file exists on the server.