I am trying to figure out how to retrieve the dropdown selected and send it to the mail using PHPmailer but I am unable to achieve it it just needs some tweaks !! Also to note that I am using Bulma as a part of my project.
<form action="bug.php" method="POST" class="" enctype="multipart/form-data">
<div class="section">
<div class="container">
<div class="columns">
<div class="column"></div>
<div class="column is-5">
<div class="field">
<label class="label">Name</label>
<div class="control">
<input
class="input"
type="text"
id="name"
name="vname"
placeholder="e.g Alex Smith"
/>
</div>
</div>
<div class="field">
<label class="label">Email</label>
<div class="control">
<input
class="input"
type="email"
name="vemail"
placeholder="e.g. [email protected]"
/>
</div>
</div>
<div class="control">
<div class="select">
<select id="selection" onchange="showradio(this)">
<option value="0">Select dropdown</option>
<option value="1">Best</option>
<option value="2">Bestest</option>
<option value="3">Exceptional</option>
</select>
</div>
</div>
<div class="buttons" >
<button name="btnsubmit" id="btnsend" class="button is-primary is-light">Submit</button>
</div>
</div>
<div class="column"></div>
</div>
</div>
</div>
</form>
Now the PHPMailer that I have configured.
<?php
$name = $_POST['vname'];
$email = $_POST['vemail'];
$msg = $_POST['vmsg'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'PHPmailer/vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('[email protected]', 'Tools');
$mail->addAddress('[email protected]', 'To'); // Add a recipient
$mail->AddAddress("$_POST['emailreceiver']");
// // Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'New Bug Submission !';
$mail->Body = $msg;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Simply the code only sends the message, subject, and name but I need to add the text which is selected in the dropdown.
As suggested, you are missing "name" attribute for select element so, you need to put name="my_dropdown". Also, you will need change value of options from 0,1,2 to the actual text. If you don't want to change the options value then you can create a simple helper function something like this:
function get_dropwown_text() {
return ["Select dropdown", "Best", "Bestest", "Exceptional"];
}
In your view(HTML), you can prepare dropdown using the helper function by:
<select id="selection" onchange="showradio(this)">
<?php
foreach(get_dropwown_text() as $k => $v) {
echo sprintf('<option value="%d">%s</option>', $k, $v);
}
?>
</select>
Now, in your php script you can get the dropdown text by:
$dropdown_value = $_POST['my_dropdown']; //will give 1 or 2 or 3
$dropdown_text = get_dropwown_text()[$dropdown_value]; //will give you actual text
//Now you can use the $dropdown_text to send in mail body via PHPmailer
$msg = $dropdown_text;
The effect of these lines is to form a spam gateway:
$msg = $_POST['vmsg'];
$mail->AddAddress("$_POST['emailreceiver']");
$mail->Body = $msg;
Anyone can write a script that sends whatever content they choose to whoever they like, using your server to do so. That's a very dangerous thing to allow, and such holes are frequently discovered and massively exploited.
To address this you need to change either the use of the email address, or the message content. The former is easier, and the way to do it is to not use a submitted address to send to, but a proxy for it.
In your form you might have options like this:
<select name="emailreceiver">
<option value="sales" selected>Sales</option>
<option value="support">Technical support</option>
<option value="accounts">Accounts</option>
</select>
Then in your script, map these values to fixed addresses:
$addresses = [
'sales' => '[email protected]',
'support' => '[email protected]',
'accounts' => '[email protected]',
];
//Validate address selection before trying to use it
if (array_key_exists('emailreceiver', $_POST) && array_key_exists($_POST['emailreceiver'], $addresses)) {
$mail->addAddress($addresses[$_POST['emailreceiver']]);
} else {
//Fall back to fixed address if selection invalid
$mail->addAddress('[email protected]');
}
//Validate any email address that was submitted, and use it as a reply-to
if (!$mail->addReplyTo($_POST['vemail'], $_POST['vname'])) {
die('Invalid email address provided');
}
This way there is no external control over who the message is sent to. Take a look at the contact form example provided with PHPMailer for guidance on how to build a contact form safely.
Quite separately, this is suspicious, suggesting you might not get how composer works:
require 'PHPmailer/vendor/autoload.php';
Thecomposer.json
file should be at the top level of your project as a whole, not something specific to PHPMailer. Have a search for tutorials on how to use composer.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Bulma is another popular CSS framework and its main feature is the fact that its components rely heavily on flexbox, which makes it truly modern. You may think that Bulma is somewhat similar to a hybrid of Bootstrap and Semantic UI, but without any complexity. It uses some of the same principles as Semantic User Interface, with class names, includes many popular components, but manages to keep things simple - for example, form elements are barely styled to maintain a cross-browser look.
https://bulma.io/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.