Send Email attachment via PHP | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Send Email attachment via PHP

This code works perfectly for sending the email body headers including the NAME of the user uploaded image file. But, it will not send the image file as an attachment so I can acccess it. How can I achieve this with the current code? <?php $errors = ''; $myemail = 'ththinka@gmail.com';//email if(empty($_POST['fname']) || empty($_POST['email']) || empty($_POST['file']) || empty($_POST['request'])) { $errors .= "\n All fields are required"; } $fname = $_POST['fname']; $email_address = $_POST['email']; $file = $_POST['file']; $request = $_POST['request']; if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Invalid email address"; } if( empty($errors)) { $to = $myemail; $email_subject = "Submission from: $fname"; $email_body = "View submission: \n " . " \n Name: $fname \n Email: $email_address \n Character Picture: $file \n Request: \n $request"; $headers = "From: $myemail\n"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); //redirect to the 'thank you' page header('Location: thankyou.html'); } ?> <!DOCTYPE HTML> <html lang="en"> <head> <!--SITE INFO--> <link rel="icon" href="images/favicon.ico" type="image/ico"> <title>Submit</title> </head> <body> <!-- This page is displayed only if there is some error --> <?php echo nl2br($errors); ?> </body> </html>

26th Apr 2019, 1:16 PM
ThThinka
ThThinka - avatar
1 Answer
0
Use PHPMailer. There's a lot of video tutorials about it. PHPMailer allows you to send message along with attachments
29th Jan 2020, 7:22 PM
Luzzitto