-1

I'm trying to use the following code to try and send a PHP Email with a PDF attachment.

I get the message 'Mail Sent. Thank You'. However, No email is being sent.

Please could someone show me / explain to me where I am going wrong?

Thanks

<?php 
if(isset($_POST['submit'])){
    $to = "example@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $subject = "Subject Here";
    $message = $_POST['message'];
    $file = $_POST['upload'];
   
    
    $headers = 'From: someone@gmail.com ' . "\r\n" .
    'Reply-To: someone@gmail.com   ' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    
    $headers2 = "From:" . $to;
    mail($to, $subject, $message, $file, $email, $headers);
    echo "Mail Sent. Thank you.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="file" name="upload" accept="application/pdf,application/vnd.ms-excel" />
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – El_Vanja Apr 09 '21 at 09:29
  • Though, from a quick glance, `someone@someone@gmail.com` is not a valid e-mail. – El_Vanja Apr 09 '21 at 09:30
  • @El_Vanja not really i don't want to use PHP mailer function if i can help it. The code works fine if i take out the file attachment code – Mark O'Brien Apr 09 '21 at 09:33
  • That linked question has 21 (*twenty one*!) subtitles describing potential failures, 20 of which concern PHP and its `mail` function directly. How did you draw the conclusion that the solution is using PHPMailer? – El_Vanja Apr 09 '21 at 09:37

1 Answers1

0

There are lot of issues with your code, and it is recommended to first check the syntax and documentation on official site. For instance your are passing $file as parameter to mail function, but there is no FILE parameter in mail function (https://www.php.net/manual/en/function.mail.php)

Then you are not saving the uploaded PDF to any disk to send from. Uploaded files are captured through $_FILES variable not $_POST variable. You need to see that too.

Then sending any file is tricky job, and your job can be ease by using some library function such as PHPMailer so try to refer those.

Sumit Gupta
  • 2,152
  • 4
  • 29
  • 46