Отправить электронное письмо нескольким получателям на основе выбранной опции в php?

Добрый вечер всем,

В настоящее время я пытаюсь отправить электронное письмо соответствующему получателю на основе выбранного варианта, который они выбрали в форме электронной почты перед отправкой. Например, вы открываете форму, вводите свое имя и т. Д. И вы видите вариант «Я клиент, я пресса и т. Д.», И я выбираю «Я пресса» .. Это теперь следует отправить на адрес press@domain.com, и это зависит от их выбора.

Я не самый лучший с PHP и пробовал это некоторое время, и я чувствую, что я довольно близко, но могу закрыть последние детали. Я также посмотрел на некоторые предыдущие ответы, и я стараюсь сделать как можно меньше переделок, поскольку я знаю, что это не слишком сложно. Кто-нибудь подумает, скажите мне, что я делаю неправильно, чтобы он не отправлял? Я чувствовал, что я определил правильные вещи, но не уверен, что происходит.

Вот мой PHP ..

<?php // We use the name2 field as bait for spambots. It's display is off, // so humans wouldn't know to enter anything into it. Robots would, // so we ignore submissions with info in name2. $mail_sent = false; if(sizeof($_POST) && $_POST["name2"] == "") // receiving a submission { define("SUBJECT", "Visitor Message from Website"); // production recipient: define("RECIPIENT", ".$department."); // prep our data from the form info $senderName = $_POST['name']; $senderEmail = $_POST['email']; $department = $_POST['department']; $subject = SUBJECT; $messageBody = $senderName . ' ('.$senderEmail.') wrote in '.$department.': ' . $_POST['message']; if($department == 'customer') { //if customer was selected $to = 'customer@gmail.com'; } else if($department == 'distribution') { //if distribution was selected $to = 'distribution@email.com'; } else if($department == 'press') { //if press was selected $to = 'press@email.com'; } else if($department == 'career') { //if career was selected $to = 'career@email.com'; } else if($department == 'other') { //if other was selected $to = 'other@email.com'; } // From $header = "from: $senderName <$senderEmail>"; // To $to = RECIPIENT; // Send it! $send_contact = mail($to, $subject, $messageBody, $header); // Check success of send attempt if($send_contact){ // show thankyou screen $mail_sent = true; } else { // send failed. echo "ERROR"; } } ?> 

А вот сама форма!

  <form action="contact-form.php" id="contactForm" method="post" accept-charset="utf-8"> <input id="name" type="text" name="name" minlength="2" placeholder="Your Name&hellip;" required> <input id="email" type="email" name="email" placeholder="Your Email&hellip;" required> <input id="name2" type="text" name="name2" placeholder="Your Last Name&hellip;" required> <select id="department" type="text" name="department"> <option value="customer" name="customer">I am a customer</option> <option value="distribution" name="distribution">department in distribution</option> <option value="press" name="press">I am with the press</option> <option value="career" name="career">department in a career position</option> <option value="other" name="other">Other</option> </select> <textarea name="message" placeholder="Your Message&hellip;" required></textarea> <input type="submit" value="Send away!"> </form> 

Любую помощь любезно оценивают. У вас отличная неделя!

Related of "Отправить электронное письмо нескольким получателям на основе выбранной опции в php?"

Следующая работа, однако эта строка, if(sizeof($_POST) && $_POST["name2"] == "") не работает в моих тестах.

Я предлагаю вам изменить его на if(!empty($_POST) && $_POST["name2"] != "") (Что в моем ответе).

То, что это в основном говорит, «если не пусто, а имя2 ничего не равно,

 <?php // We use the name2 field as bait for spambots. It's display is off, // so humans wouldn't know to enter anything into it. Robots would, // so we ignore submissions with info in name2. $mail_sent = false; if(!empty($_POST) && $_POST["name2"] != ""){ // receiving a submission $to = $_POST['department']; // prep our data from the form info $senderName = $_POST['name']; $senderEmail = $_POST['email']; $department = $_POST['department']; $subject = "Visitor Message from Website"; $messageBody = $senderName . ' ('.$senderEmail.') wrote in '.$department.': ' . $_POST['message']; if($department == 'customer') { //if customer was selected $to = 'customer@gmail.com'; } else if($department == 'distribution') { //if distribution was selected $to = 'distribution@email.com'; } else if($department == 'press') { //if press was selected $to = 'press@email.com'; } else if($department == 'career') { //if career was selected $to = 'career@email.com'; } else if($department == 'other') { //if other was selected $to = 'other@email.com'; } // From $header = "From: $senderName <$senderEmail>"; // echo $to; // my testing purpose // Send it! $send_contact = mail($to, $subject, $messageBody, $header); if($send_contact){ $mail_sent = true; } else { echo "ERROR"; } // end brace for if(!empty($_POST) && $_POST["name2"] != "") } ?>