I'm trying to send emails with attachment from a HTML form using PHP mail().
I have found a script online that works - but with one problem:
If a file is attached to the email, the script works fine. However, if there is no attachment, the email is sent but doesn't contain any text content (blank email body).
I want the ability to add an attachment to be optional.
My question is, how can an email be sent successfully, with or without an attachment?
Thanks!
The PHP script I'm using is as follows:
<?php
$email = trim($_POST['email']);
$email_san = filter_var($email, FILTER_SANITIZE_EMAIL);
$fname = $_POST['first_name'];
$fname_san = filter_var($fname,FILTER_SANITIZE_STRING);
$lname = $_POST['last_name'];
$lname_san = filter_var($lname,FILTER_SANITIZE_STRING);
$org = trim($_POST['organisation']);
$org_san = filter_var($org,FILTER_SANITIZE_STRING);
$user_phone = $_POST['phone'];
$trim_phone = trim($user_phone);
$replace_phone = preg_replace('/[^0-9+-]/', '', $trim_phone);
$phone_san = filter_var($replace_phone,FILTER_SANITIZE_NUMBER_INT);
$message = $_POST['message'];
$fromemail = $email_san;
$subject="Inquiry";
$email_message = '<p><b>First Name:</b> '.$fname_san.'</p>
<p><b>Organisation:</b> '.$org_san.'</p>
<p><b>Email:</b> '.$email_san.'</p>
<p><b>Phone:</b> '.$phone_san.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
$semi_rand = md5(uniqid(time()));
$headers = "From: ".$fromemail;
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
if($_FILES["file"]["name"]!= ""){
$strFilesName = $_FILES["file"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: application/octet-stream;\n" .
" name=\"{$strFilesName}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$strContent .= "\n\n" .
"--{$mime_boundary}--\n";
}
$toemail="email@somedomain.com";
if(mail($toemail, $subject, $email_message, $headers)){
echo "Email sent.";
}else{
echo "Email NOT sent.";
}
?>
/*** UPDATE ***/
OK, taking advice that PHPMailer is a better method of sending emails, I have made another attempt to setup PHPMailer.
I don't have Composer so I downloaded and installed PHPMailer manually. I don't have Composer so I downloaded and installed PHPMailer manually. My server folder hierarchy is shown in this image.
I found a PHPMailer tutorial online which provided a simple script which I saved as 'mailer-test.php', uploaded and linked to my PHPMailer install:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "php-mailer/src/Exception.php";
//PHPMailer Object
$mail = new PHPMailer(true); //Argument 'true' in enables exceptions
//From email address and name
$mail->From = "me@mydomain.com";
$mail->FromName = "Full Name";
//To address and name
//$mail->addAddress("recipient@somedomain.com", "Recepient Name");
$mail->addAddress("recipient@somedomain.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("me@mydomain.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
try {
$mail->send();
echo "Message has been sent successfully";
} catch (Exception $e) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
However, when I view 'mailer-test.php' in a browser, I get the follwing server error:
This page isn't working. yourdomain.com is currently unable to handle this request. HTTP ERROR
What is causing this?