简体   繁体   English

我可以使用PHP从表单发送数据并选择下拉列表发送电子邮件吗?

[英]Can i send data from a form and select drop down to email using PHP?

I'm currently building a form in which a visitor inputs their information and submits it, in then sends an email across to the department that they've selected via the dropdown menu, just wanted to see if this is actually possible and if so how do i continue the IF statement, also is there anything wrong with my code, i'm no pro when it comes to html/php. 我目前正在构建一个表单,访客可以在其中输入信息并提交信息,然后通过下拉菜单向选择的部门发送电子邮件,只是想知道这是否可行以及如何实现我要继续执行IF语句吗,我的代码也有问题吗,关于html / php我不是专业人士。

    <input type="text" name="full_name" placeholder="*Enter Name">

    <input type="email" name="email" placeholder="*Enter Email">

    <input type="text" name="company"  placeholder="*Enter Company">
    <br>

    <select id="Visiting">
     <option>Select Department to email</option>
      <option name="Visiting1" value="event1">Operations</option>
      <option name="Visiting2" value="event2">Security</option>
      <option name="Visiting3" value="event3">Shunters</option>  
      <option name="Visiting4" value="event2">Stock & Systems</option>
      <option name="Visiting5" value="event3">Goods In</option>  
      <option name="Visiting6" value="event3">Facilities</option>
      <option name="Visiting7" value="event3">IT</option>
      <option name="Visiting8" value="event3">Despacth</option>
      <option name="Visiting9" value="event3">Transport</option>
    </select>

    <br>
    <br>
    <br>

    <button type="submit">Sign In</button>

<?php
$name = $_POST['full_name'];
$email = $_POST['email'];
$company = $_POST['company'];
$visitor = $_POST['visitor'];

if($visitor == "security"){
    $mail_to = 'your security email';    
}elseif
  ($visitor == "it"){
    $mail_to = 'your it email';    
}else
  ($visitor == "Transport"){
$mail_to = 'your transport email';




$body_message = 'From: '.$name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$company;
$body_message .= 'Message: '.$visitor;

$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
    alert('Thank you for notifying us, we will be with you shortly.');
    window.location = 'http://nameofwebsite.co.uk';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
    alert('Message failed. Please, try again or select another department');
    window.location = <'http://nameofwebsite.co.uk.>/';
</script>
<?php
}
?> 

Your HTML is wrong, you can only add a name-attribute to a <select> -tag it doesn't work on <option> -tags. 您的HTML是错误的,您只能将名称属性添加到<select> -tag,而不能在<option> -tags上使用。

Your html code should look like this: 您的html代码应如下所示:

<select id="Visiting" name="Visiting">
    <option disabled selected>Select Department to email</option>
    <option value="Visiting1">Operations</option>
    <option value="Visiting2">Security</option>
    <option value="Visiting3">Shunters</option>  
    <option value="Visiting4">Stock & Systems</option>
    <option value="Visiting5">Goods In</option>  
    <option value="Visiting6">Facilities</option>
    <option value="Visiting7">IT</option>
    <option value="Visiting8">Despacth</option>
    <option value="Visiting9">Transport</option>
</select>

PHP: PHP:

$visitor = $_POST['Visiting'];
    switch ($visitor) {
        case 'Visiting1':
        $mail_to = 'your Visiting1email';
        break;
    case 'Visiting2':
        $mail_to = 'your Visiting2 email';
        break;
    case 'Visiting3':
        $mail_to = 'your Visiting3 email';
        break;
    ...
}

I hope this helps 我希望这有帮助

First of all, the PHP function mail() is probably the issue here if you are getting errors. 首先,如果遇到错误,PHP函数mail()可能是这里的问题。 You must have PHP set up to actually send emails, it doesn't typically work out-of-the-box without some configuration. 您必须将PHP设置为实际发送电子邮件,如果没有进行一些配置,它通常无法立即使用。

SMTP for sending emails is a very old protocol, and in it's original form it didn't require a username/password (authentication) to send an email. 用于发送电子邮件的SMTP是一个非常古老的协议,它的原始格式不需要发送用户名/密码(身份验证)即可发送电子邮件。 It was on the honor system that you weren't spoofing someone else in the "From:" field. 在荣誉系统上,您没有在“发件人:”字段中欺骗其他人。

Obviously things have changed, and servers that receive email verify that the origin server matches the "From:" field on the email, and the origin server itself asks for a username/password to be able to send an email in the first place (and restricts the authenticated user's ability to customize the "From:" field as well). 显然情况已经发生了变化,接收电子邮件的服务器会验证原始服务器是否与电子邮件上的“发件人:”字段相匹配,并且原始服务器本身会要求用户名/密码才能首先发送电子邮件(并且也限制了通过身份验证的用户自定义“发件人:”字段的功能)。

This being said, you can run your own SMTP server and you can do so as insecurely as you like, but other email servers online won't take you very seriously because you'll look like just another spammer out there. 话虽这么说,您可以运行自己的SMTP服务器,并且可以根据自己的意愿安全地运行它,但是其他在线电子邮件服务器并不会非常重视您,因为您看起来就像是另一个垃圾邮件发送者。 Instead it's much more reliable to use an externally "properly" set up SMTP server through a web hosting company, or from a popular web mail service such as Gmail or Yahoo. 相反,通过网络托管公司或通过流行的Web邮件服务(例如Gmail或Yahoo)使用外部“正确”设置的SMTP服务器要可靠得多。

Under the hood, PHP's mail() function uses the Linux command line utility called sendmail . 在后台,PHP的mail()函数使用名为sendmail的Linux命令行实用程序。 You can also use other libraries out there like PEAR's mail package. 您还可以使用其他库,例如PEAR的邮件包。

A lot of people have sendmail using a default local SMTP server, and set up the that server as a relay to Gmail (or whatever) external server. 很多人有sendmail使用默认的本地SMTP服务器,并设置该服务器作为中继到Gmail(或其他)外部服务器。 This way scripts can send emails without a username/password (the relay takes care of that). 这样,脚本可以发送不带用户名/密码的电子邮件(由中继负责)。 Obviously this has some security implications, but it can be a simple way to do it. 显然,这有一些安全隐患,但这可以是一种简单的方法。

If you let us know more about what email server you want to send mail from, and what type of server you website is running on, I could point you in the right direction for configuring a mail sending utility. 如果您让我们进一步了解您要从哪个电子邮件服务器发送邮件,以及您的网站正在运行哪种服务器,我可以为您指明配置邮件发送实用程序的正确方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM