成功发送电子邮件后POST到CSV

时间:2017-09-26 14:26:18

标签: php csv email

我有一个潜在客户表单,在填写完成后发送电子邮件通知。如果电子邮件成功,我怎样才能将这些数据写入服务器上的csv?

这是我的代码,现在它只是什么都没做,当填写表单时,电子邮件结束,但没有任何内容写入CSV。

逻辑是(可能是错误的)如果它到达succ消息,打开file.csv并将$ email_f写入它并关闭csv。

我做错了什么?

<?php 
if($_POST['email']){
    $email_f = $_POST['email'];
    $to = "myemail@example.com";
    $subject  = "subject";

    $message = "<html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    </head>
    <body>
    <strong>Title</strong><hr />
       <table align='center' width='99%' border='0' cellpadding='0' cellspacing='0'>
        <tr bgcolor='#cccccc'><td width='30%' style='padding:7px 5px; font-weight:bold;'>Email:</td><td style='padding:7px 5px;'>".$email_f."</td></tr>    
       </table>
    </body>
    </html>";

    //echo $message;
    $headers = "From: <from@example.com>\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
    $headers .= 'Bcc:' . "\r\n";
    $mail = mail($to, $subject, $message, $headers);
    if($mail)
    {
        echo "succ";
      $file = 'file.csv';
    $file = fopen($file, 'a');
    fputcsv($file, $email_f);
    fclose($file);
}
}






 ?>

我也试过这个:

$list = array ($mail());

    $fp = fopen('/file.csv', 'w');
    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);

好的越来越近了,我添加了这个打开文件的snippit,但没有写任何内容,任何想法?:

$list = array(

    mail($to, $subject, $message, $headers)
    );

    $fp = fopen('file.csv', 'w');

    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);

1 个答案:

答案 0 :(得分:0)

此代码适用于曾经想知道的人!

 public  static class Browser
{
    private static IWebDriver driver;
    private  static string baseURL = "someURL";

    public static ISearchContext Driver { get { return driver; } }

    internal static bool WaitUntilElementIsDisplayed(By element, int timeout)
    {
        for (int i = 0; i < timeout; i++)
        {
            if (ElementIsDisplayed(element))
            {
                return true;
            }
            Thread.Sleep(1000);
        }
        return false;
    }

    internal static IWebElement FindElement(By by)
    {

        return driver.FindElement(by);
    }

    public static bool ElementIsDisplayed(By element)
    {
        var present = false;
        driver.Manage().Timeouts().ImplicitlyWait(System.TimeSpan.FromSeconds(0));
        try
        {
            present = driver.FindElement(element).Displayed;
        }
        catch (NoSuchElementException)
        { }
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        return present;

    }



    public static void Initialize()
    {
        var options = new InternetExplorerOptions();
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        options.EnsureCleanSession = true;
        options.IgnoreZoomLevel = true;

        driver =
            new InternetExplorerDriver(

                @"C:Myfilepath",
                options, TimeSpan.FromMinutes(10));

        Goto("");
    }

    public static void CleanUp()
    {
        driver.Close();
        driver.Quit();
    }

    public static void Goto(string URL, bool userBaseURL = true)
    {
        if (userBaseURL)
            driver.Navigate().GoToUrl(string.Format("{0}/{1}", baseURL, URL));
        else
            driver.Navigate().GoToUrl(URL);
    }


}