2

i studying on PHP now, and want to learn how to send email from diretory web to someone email with attachment,,

i try with this code, and it worked, but when i try to send email again to other email address, it didnt send again and need a few hour (or few moment, i didnt know exactly, just my guess) to worked again. i didnt know what the real problem is? if someone have same problem with me, please share here. thanks in advance. and sorry for my bad english.

this is my code,

    <?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>";
        }

    ?>
zuzu
  • 21
  • 1
  • Refer this link hope this will help: http://webcheatsheet.com/php/send_email_text_html_attachment.php – Insane Skull Aug 04 '15 at 12:58
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 04 '15 at 12:59
  • Zzzzx.......... – Syntax Killer Dec 18 '19 at 13:40

1 Answers1

0

If you use else if and else instead of just an if the script will only execute one of the 2 blocks of code. So try putting else if directly after the closing curly bracket of if($prodiMhs == "S1 Manajemen"){. Basicly change if($prodiMhs == "S1 Administrasi Niaga"){ to else if($prodiMhs == "S1 Administrasi Niaga"){ More info on elseif and else.

I hope this helps you.

Meneer Venus
  • 1,039
  • 2
  • 12
  • 28