2

添付ファイル付きの電子メールを送信するようにスクリプトを変更しようとしています。現在は機能していますが、すべてのフィールドが必要であり、フィールドが不要になるようにするために何が欠けているのか疑問に思っていました。

<?php
if (($_POST)) {

$success = $error = false;

$post = new stdClass;

foreach ($_POST as $key => $val)
    $post->$key = trim(strip_tags($_POST[$key]));

    $dir = dirname(__FILE__);

    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();

    require_once($dir.'/swift/swift_required.php');

    $mailer = new Swift_Mailer(new Swift_MailTransport());

    $message = Swift_Message::newInstance()
                   ->setSubject('Imperial Order') // Message subject
                   ->setTo(array($post->email => $post->name, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
                   ->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])->setFilename($_FILES['attachment']['name']));

    // Send the email, and show user message
    if ($mailer->send($message))
        $success = true;
    else
        $error = true;      
}
?>

foreach と関係があると思いますが、それを取り出そうとすると、コード全体が壊れてしまいます。誰かがそれを助けることができれば、それは素晴らしいことです。検証を行う理由は、サーバー側ではなくクライアント側で検証したいからです。

4

1 に答える 1

2

for each を削除できますが、ポストされた各フィールドを個別にリクエストし、ループではなく一度に 1 つずつ処理/検証する必要があります。これは、ループがすべてのポスト変数をローカル変数にプルするためです。

また、ループの内側に迅速なメーラーのインクルードなどを必要とする必要はありません。ループの外に移動することができます $mailer = new transport start line はすべてループの外に出ることができます

例で編集

if (isset($_POST)) { //BAD BAD BAD way of checking there's better ways

$success = $error = false;

$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$input3 = $_POST['input3'];
$input4 = $_POST['input4'];

    $dir = dirname(__FILE__);
    // THIS BIT IS RETARDED BUT LEAVING IT IN FOR NOW
    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();
    // END RETARDED BIT
    require_once($dir.'/swift/swift_required.php');
    $mailer = new Swift_Mailer(new Swift_MailTransport());
    if (isset($_FILES) && strlen($_FILES['attachment']['name']) > 0) { //here we're checking that there is attachments if there are then we're going to do the attach sw code
        $message = Swift_Message::newInstance()
                   ->setSubject('Imperial Order') // Message subject
                   ->setTo(array($input1 => $input2, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
                   ->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])->setFilename($_FILES['attachment']['name']));
    } else {
        //non attach sw code
        $message = Swift_Message::newInstance()
                   ->setSubject('Imperial Order') // Message subject
                   ->setTo(array($input1 => $input2, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
                   ->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
    }
    //there's better ways of doing the above but as an example this will suffice.
    // Send the email, and show user message
    if ($mailer->send($message)) {
        $success = true;
    } else {
        $error = true;
    }
}
?>
于 2013-03-14T14:21:06.847 に答える