Send File Attachment from Form Using phpMailer and PHP

后端 未结 8 1056
予麋鹿
予麋鹿 2020-11-22 14:09

I have a form on example.com/contact-us.php that looks like this (simplified):

8条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 15:05

    Use this code for sending attachment with upload file option using html form in phpmailer

     
    
    
                        
                        
                        
    
    
                        
                        
    
    
                    
       
    
    
        ';
                if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
                    echo "File is valid, and was successfully uploaded.\n";
                } else {
                    echo "Possible invalid file upload !\n";
                }
    
                echo 'Here is some more debugging info:';
                print_r($_FILES);
    
                print "
    "; ////// Email require_once("class.phpmailer.php"); require_once("class.smtp.php"); $mail_body = array($_POST['name'], $_POST['email'] , $_POST['msg']); $new_body = "Name: " . $mail_body[0] . ", Email " . $mail_body[1] . " Description: " . $mail_body[2]; $d=strtotime("today"); $subj = 'New enquiry '. date("Y-m-d h:i:sa", $d); $mail = new PHPMailer(); // create a new object //$mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only ,false = Disable $mail->Host = "mail.yourhost.com"; $mail->Port = '465'; $mail->SMTPAuth = true; // enable $mail->SMTPSecure = true; $mail->IsHTML(true); $mail->Username = "admin@domain.net"; //from@domainname.com $mail->Password = "password"; $mail->SetFrom("admin@domain.net", "Your Website Name"); $mail->Subject = $subj; $mail->Body = $new_body; $mail->AddAttachment($uploadfile); $mail->AltBody = 'Upload'; $mail->AddAddress("recipient@domain.com"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo '

    Success

    '; } } ?>

    Use this link for reference.

提交回复
热议问题