douqian1975 2014-02-11 23:00
浏览 58

想使用$ _POST ['email']在php邮件功能中设置接收者电子邮件

can anyone help me with this problem im having. what im trying to get to happen is when i submit a php form to my MySQL database that an email is sent at the same time but what i want to happen is that the the email is sent to the email address inputted on the form. sadly anything i have tried just isn't working here is what i have so far:

Here is the form:

<form action="index.php" method="post" id="add_player_form" name="form">
                    <input type="hidden" name="action" value="add_player"/>
                    <h3>Add New Player</h3>
                    <label>First Name:</label>
                    <input type="text" name="first_name" />
                    <br /><br />
                    <label>Last Name:</label>
                    <input type="text" name="last_name" />
                    <br /><br />
                    <label>Email:</label>
                    <input type="text" name="email" />
                    <br /><br />
                    <label>Position:</label>
                    <input type="text" name="position" />
                    <br /><br />
                    <label>Date Of Birth:</label>
                    <input type="text" name="dob" />
                    <br /><br />
                    <label>Country:</label>
                    <input type="text" name="country" />
                    <br /><br />
                    <label>City/Town:</label>
                    <input type="text" name="city_town" />
                    <br /><br />
                    <label></label><input type="submit" value="ADD" onClick="randomString();" />
                    <br /><br />
                    <input type="hidden" name="user_type_id" value="2" />
                    <input type="hidden" name="team_id" value="<?php echo $teamId ?>" />
                    <input type="hidden" name="password" value=""/>
                </form>

Here is the php code from index.php:

else if ($action == 'add_player') {
    $last_name = $_POST['last_name'];
    $first_name = $_POST['first_name'];
    $dob = $_POST['dob'];
    $position = $_POST['position'];
    $email = $_POST['email'];
    $country = $_POST['country'];
    $city_town = $_POST['city_town'];
    $password = $_POST['password'];
    $team_id = $_POST['team_id'];
    $user_type_id = $_POST['user_type_id'];
    add_player($last_name, $first_name, $dob, $position, $email, $country, $city_town, $password, $team_id, $user_type_id); //edit
    $team_manager = get_players();

    //$to = $_POST['email']; // this is the player's Email address
    $from = "teammanager0@outlook.com"; // this is the web app's Email address
    $subject = "Welcome to Team Manager";
    $message = "You have been added to a team on our web app TEAM MANAGER!" . "

" . "In order to login to your team please use 
        the following details: " . "

" . "Email: " . $email . "

" . "Password: " . $password;
    //$message = $first_name . " " . $last_name . " wrote the following:" . "

" . $_POST['message'];
    $headers = "From:" . $from;

    mail($email, $subject, $message, $headers);

    include('userPage.php');
}

and also my SMTP configuration:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.live.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = teammanager0@outlook.com

any help would be hugely appreciated and if you need more info please ask, cheers everyone !

  • 写回答

2条回答 默认 最新

  • dongma7796 2014-02-12 00:07
    关注

    So, nobody is answering and probably I will get some downvotes. But your problem is not going to go away by itself, so lets start debugging!

    First thing I noticed, was the the code is incomplete, so basically without it, we cannot confirm, that the problem isn't somewhere else. Since you said its not relevant, I deleted everything, that was commented out and / or we didn't have any reference at (like the mysql functions and etc.)

    Since some of the code is missing, I don't know if your problem is there or not. But else if ($action == 'add_player') { at the start and <input type="hidden" name="action" value="add_player"/>, would suggest, that your missing the _POST action value. If this is the case, then add $action = $_POST['action']; in the head of the code. Or change the $action to $_POST['action'].

    If the above idea doesn't work, then try my nerffed code:

    <?
    
    // Removed the first hidden input and merged it with submit button
    if ($_POST['action'] == 'Add Player') {
    
        print 'Submit action was trigged, lets hope also the mails report is going to be good.<br />';
    
        $last_name = $_POST['last_name'];
        $first_name = $_POST['first_name'];
        $dob = $_POST['dob'];
        $position = $_POST['position'];
        $email = $_POST['email'];
        $country = $_POST['country'];
        $city_town = $_POST['city_town'];
        $password = $_POST['password'];
        $team_id = $_POST['team_id'];
        $user_type_id = $_POST['user_type_id'];
    
        $from = "TeamManager <teammanager0@outlook.com>"; // Made format different, to support the name
        $subject = "Welcome to Team Manager";
        $message = "You have been added to a team on our web app TEAM MANAGER!" . "
    
    " . "In order to login to your team please use 
            the following details: " . "
    
    " . "Email: " . $email . "
    
    " . "Password: " . $password;
        $headers = "From: " . $from; // added whitespace
    
        if (mail($email, $subject, $message, $headers)) {
            print 'Email was sent by the PHP code, and the rest is up to the gods of the internet highway.<br />';
        }
    }
    
    print '<form action="" method="post">
    
        <h3>Add New Player</h3>
        <label>First Name:</label>
        <input type="text" name="first_name" />
    
        <br />
        <br />
    
        <label>Last Name:</label>
        <input type="text" name="last_name" />
    
        <br />
        <br />
    
        <label>Email:</label>
        <input type="text" name="email" />
    
        <br />
        <br />
    
        <label>Position:</label>
        <input type="text" name="position" />
    
        <br />
        <br />
    
        <label>Date Of Birth:</label>
        <input type="text" name="dob" />
    
        <br />
        <br />
    
        <label>Country:</label>
        <input type="text" name="country" />
    
        <br />
        <br />
    
        <label>City/Town:</label>
        <input type="text" name="city_town" />
    
        <br />
        <br />
    
        <label></label>
        <input type="submit" name="action" value="Add Player" onClick="randomString();" />
    
        <br />
        <br />
    
        <input type="hidden" name="user_type_id" value="2" />
        <input type="hidden" name="team_id" value="" />
        <input type="hidden" name="password" value=""/>
    
    </form>';
    

    This code as you can see is fairly debugged and also the hidden input has been replaced to submit-trigger. So try it out, and let us know what is the result, do you see the two notices?


    EDIT: So, the above code worked fine in my server. Meaning, that if the stuff, that I nerffed out from your code, isn't the problem (and we cannot verify that without seeing it) then your code isn't the problem. Possible other problems are:

    • Your ISP is blocking your outgoing emails (or your servers config. is wrong)
    • Your SMTP settings and wrong
    • Your testing e-mail service is considering your test-emails as spam. My ISP once official said, that too short and suspicious looking e-mails get treated as spam. So try testing it with different e-mail services and with more content-text
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝