User: Hp
Date: 06/06/2015
Time: 14:33
$to = 'example@email.com'; $subject = 'Testing PHP Mail function'; $body = 'This is a test email sent using PHP Mail function.'; $headers = 'From: sender@email.com'; if (mail($to, $subject, $body, $headers)) { echo 'Email sent successfully'; } else { echo 'Error: Email not sent'; }
$to = 'example@email.com'; $subject = 'Testing PHP Mail function with attachment'; $body = 'This is a test email sent using PHP Mail function with attachment.'; $headers = 'From: sender@email.com'; $file_attached = 'path/to/file.pdf'; $file_content = file_get_contents($file_attached); $file_encoded = base64_encode($file_content); $boundary = md5(time()); $headers .= "\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"$boundary\""; $body .= "\n\n--".$boundary."\n"; $body .= "Content-Type: application/pdf; name=\"file.pdf\"\n"; $body .= "Content-Transfer-Encoding: base64\n"; $body .= "Content-Disposition: attachment\n\n".$file_encoded."--".$boundary."\n"; if (mail($to, $subject, $body, $headers)) { echo 'Email sent successfully'; } else { echo 'Error: Email not sent'; }In this example, the PHP Mail function is used to send an email with a PDF attachment. The file content is first encoded using base64 and then included in the email body as a multipart/mixed content type. The parameters include the recipient's email address, email subject, body message, sender's email address, and the attachment file path. The PHP Mail function is a built-in function in PHP, and it does not require any additional package or library. However, there are many third-party libraries available that offer additional features and functionalities for sending emails in PHP, such as PHPMailer, SwiftMailer, and Zend Mail.