簡體   English   中英

PHP將表單創建為CSV並作為附件發送到電子郵件

[英]PHP create Form to CSV and Send to email as attachment

我正在創建一個具有表單的網頁,在該表單中,我需要將其另存為csv,並且需要將其作為附件文件發送到電子郵件中,我該怎么做?

首先,我如何將所有輸入輸出到csv,然后將其保存為csv文件並自動將其附加到電子郵件。

以下是我嘗試過的代碼:

的PHP

<?php

    // define variables and set to empty values
$nameErr = $emailErr = $countryErr = $confirmErr = "";

function clean_text($string)
{
 $string = trim($string);
 $string = stripslashes($string);
 $string = htmlspecialchars($string);
 return $string;
}

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "sample@email";
    $email_subject = "Form Submission";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        exit();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['country']) ||
        !isset($_POST['confirm_email'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $country = $_POST['country']; // not required
    $confirm_email = $_POST['confirm_email']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  } 

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }

      if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Subsciber Details Information.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($name)."\n";
    $email_message .= "Country: ".clean_string($country)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Confirmed Email: ".clean_string($confirm_email)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

    /*Autoreply Sender Name*/
 $headerss = "FROM: APSR2020 <2020@coac.co.jp>\r\n";

    /* Prepare autoresponder subject */
$respond_subject = "Sign-up for APSR 2020 Mailing List Completed";

/* Prepare autoresponder message */
$respond_message = "Thank you for your interest in sign-up for our mailing list.
We will keep you updated on APSR 2020.

In the case that this email is unexpected, it will be troubling, but please inquire through the given address.

Congress Secretariat of APSR 2020
subs-apsr 2020@coac.co.jp

Please keep get in touch with: https://apsr2020.jp
";

/* Send the response message using mail() function */

mail($email_from, $respond_subject, $respond_message, $headerss);
//redirect to the 'thank you' page
header('Location: thank-you-page.html');

?>
<!-- include your own success html here -->
<?php
}
?>

HTML表格

<form name="contactform" method="post">
                <div class="keep__me__posted">

                    <p>Please sign up NOW.</p>
                    <p class="margin__p">We will keep you updated on APSR 2020.</p>

                    <div class="input__form">
                        <label for="name">Name</label>
                        <input type="text" id="name" name="name" maxlength="50" size="30" value="" required>
                    </div>
                    <div class="input__form">
                        <label for="country">Country</label>
                        <input type="text" id="country" name="country" maxlength="30" size="30" value="" required>
                    </div>
                    <div class="input__form">
                        <label for="email">Email Address</label>
                        <input type="email" id="email" name="email" maxlength="80" size="30" value="" required>
                    </div>
                    <div class="input__form">
                        <label for="confirm_email">Confirm Email</label>
                        <input type="email" id="confirm_email" name="confirm_email" maxlength="30" size="30" required>
                    </div>
                    <div class="input__form submit">
                        <tr>
                            <td colspan="2" style="text-align:center">
                                <input type="submit" value="Subscribe" class="submit" onclick="checkEmail()">
                            </td>
                        </tr>
                    </div>
                </div>
            </form>

要寫入您的csv文件,您可以查看以下函數

$file = fopen('yourFile.csv', 'w');
fputcsv($file, array('this','is some', 'csv "stuff", you know.'));
fclose($file);

要將此文件與郵件一起發送,我認為更簡單的選擇是使用PHPMailer。 真的不知道如何使用它,但是您可以在這里輕松找到它。 首先,您需要下載並安裝在gitHub上找到的軟件包,然后像這樣使用它:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM