$to_email = "example@example.com"; $subject = "Test email"; $message = "This is a test email sent using PHP Mail To."; // Send email $success = mail($to_email, $subject, $message); if ($success) { echo "Email sent successfully."; } else { echo "Email sending failed."; }
$to_email = "example@example.com"; $subject = "Test email with attachment"; $message = "This is a test email with attachment sent using PHP Mail To."; $attachment_path = "path/to/attachment.pdf"; // Email headers $headers = "From: sender@example.com\r\n"; $headers .= "Reply-To: sender@example.com\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n"; // Email body $email_body = "--boundary\r\n"; $email_body .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; $email_body .= "Content-Transfer-Encoding: 7bit\r\n"; $email_body .= "\r\n"; $email_body .= $message . "\r\n"; $email_body .= "--boundary\r\n"; // Attachments $file_contents = file_get_contents($attachment_path); $file_encoded = chunk_split(base64_encode($file_contents)); $email_body .= "Content-Type: application/octet-stream; name=\"" . basename($attachment_path) . "\"\r\n"; $email_body .= "Content-Transfer-Encoding: base64\r\n"; $email_body .= "Content-Disposition: attachment; filename=\"" . basename($attachment_path) . "\"\r\n"; $email_body .= "\r\n"; $email_body .= $file_encoded . "\r\n"; $email_body .= "--boundary--"; // Send email $success = mail($to_email, $subject, $email_body, $headers); if ($success) { echo "Email sent successfully."; } else { echo "Email sending failed."; }In this example, the email headers and body are formatted in a way that supports attaching files. The attachment is added to the email body as a base64-encoded string. The `$headers`, `$email_body`, and `mail()` function are used to send the email with the attachment. Overall, PHP Mail To is a simple and effective way to send emails from PHP scripts without needing to install any external libraries or packages. However, it does not offer advanced features such as email templates, email tracking, or HTML email support. For those features and more, developers may choose to use external libraries such as PHPMailer or SwiftMailer.