只通过PHP发送一次电子邮件

时间:2015-08-04 12:47:32

标签: php email

我现在正在学习PHP,并希望学习如何从diretory web发送电子邮件给某人带附件的电子邮件,

我尝试使用此代码,但它确实有效,但是当我尝试再次发送电子邮件到其他电子邮件地址时,它又没有发送,需要几个小时(或者很少一刻,我完全不知道,只是我的猜测)再次工作。我不知道真正的问题是什么? 如果有人和我有同样的问题,请在这里分享。提前致谢。抱歉我的英语不好。

这是我的代码,

    <?php
    session_start();
        if (isset($_SESSION['xx']) &&  $_SESSION['xx'] == session_id() ) {
    //panggil file config.php untuk menghubung ke server
    include('config.php');

    //catch data from the form
    $nmMhs = $_POST['name'];
    $sexMhs = (isset($_POST['sex']))?$_POST['sex']:"";
    $almMhs = $_POST['address'];
    $tlpMhs = $_POST['telp'];

    $date=$_POST['tgl']; $month=$_POST['bln']; $year=$_POST['thn'];
    $tgllhrMhs = $date.'-'.$month.'-'.$year;
    $emlMhs = $_POST['email'];
    $prodiMhs = (isset($_POST['prodi']))?$_POST['prodi']:"";
    $statMhs = (isset($_POST['stat']))?$_POST['stat']:"";
    $klsMhs = (isset($_POST['kelas']))?$_POST['kelas']:"";
    $orgMhs = $_POST['org'];
    $sakitMhs = $_POST['sick'];
    $almetMhs = (isset($_POST['almet']))?$_POST['almet']:"";
    $kausMhs = (isset($_POST['kaos']))?$_POST['kaos']:"";

    //send email
    $email_to = $emlMhs; // The email you are sending to (example)
    $email_from = "someone@gmail.com"; // The email you are sending from (example)
    $email_subject = "subject of the messages"; // The Subject of the email
    $email_txt =    "hello everybody, this is a message"; // Message that the email has in it

//email attachment
    $fileatt = "path/file"; // Path to the file (example)
    $fileatt_type = "application/msword"; // File Type
    $fileatt_name = "file.docx"; // Filename that will be used for the file as the attachment
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    $headers="From: $email_from"; // Who the email is from (example)
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
    $email_message .= "\n\n";
    $data = chunk_split(base64_encode($data));
    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    //save data to database
    if($prodiMhs == "S1 Manajemen"){    
        $query = mysql_query("insert into mhs_manajemen values(null, '$nmMhs', '$sexMhs', '$almMhs', '$tlpMhs', '$tgllhrMhs', '$emlMhs', '$prodiMhs', '$statMhs', '$klsMhs', '$orgMhs', '$sakitMhs', '$almetMhs', '$kausMhs' )") or die(mysql_error());
        if ($query) {
            mail($email_to,$email_subject,$email_message,$headers);
            echo "<script type='text/javascript'>
                          window.alert('Data has been inputed')
                          window.location.href='isi_data.php';
                  </script>";
        } 
    }

    if($prodiMhs == "S1 Administrasi Niaga"){
        $query = mysql_query("insert into mhs_administrasi values(null,'$nmMhs', '$sexMhs', '$almMhs', '$tlpMhs', '$tgllhrMhs', '$emlMhs', '$prodiMhs', '$statMhs', '$klsMhs', '$orgMhs', '$sakitMhs', '$almetMhs', '$kausMhs' )") or die(mysql_error());
        if ($query) {
            mail($email_to,$email_subject,$email_message,$headers);
            echo "<script type='text/javascript'>
                          window.alert('Data has been inputed')
                          window.location.href='isi_data.php';
                  </script>";
        } 
    }

    } else {
            session_destroy();
            echo "<script type='text/javascript'>
                          window.alert('<b>Access denied! Please login first!<b>')
                          window.location.href='index.php';
                  </script>";
        }

    ?>

1 个答案:

答案 0 :(得分:0)

如果您使用else ifelse而不只是if,则脚本只会执行2个代码块中的一个。因此,如果直接在if($prodiMhs == "S1 Manajemen"){的结束花括号之后尝试放置else。基本上将if($prodiMhs == "S1 Administrasi Niaga"){更改为else if($prodiMhs == "S1 Administrasi Niaga"){ More info on elseif and else.

我希望这会对你有所帮助。