Results 1 to 5 of 5

Thread: mail form sender ID and error

  1. #1
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    917
    Thanks
    120
    Thanked 2 Times in 2 Posts

    Default mail form sender ID and error

    Dear all,
    I have this lovely newsletter script. Basic, simple, but I'd like to add the following:
    - how to have the email adres of the subscriber as sender (now sender = recipient)
    - how to have an error message when there is nothing filled in but just clicked on send
    Thanks!

    Code:
                                <?php
                                    if(isset($_POST['Email'])) {
                                    $recipient = "contact@mysite.com";
                                    $subject = "Newsletter subscription";
                                    $sender = $recipient;
                                    $body .= "Add this email adress to the newsletter: \n";
                                    $body .= " ".$_REQUEST['Email']." \n";
                                    mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
                                    echo "<div>thanks for subscribing</div>";
                                }
                                ?>
                                <form method="POST" class="subscriber-form" action="#newsletter">
                                    <input type="text" class="form-control input-lg" name="Email" placeholder="your@mailadress.com">
                                    <button type="submit" class="btn btn-maincolor" id="send" value="Submit" name="Submit">subscribe</button>
                                </form>

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 263 Times in 255 Posts
    Blog Entries
    56

    Default

    Your code is correct. The sender is NOT the recipient because of $body .= " ".$_REQUEST['Email']." \n";, where Email is the address entered by the sender.
    As for forcing an error message when nothing is filled in, this can be done by putting required in the input field. Code:
    Code:
    <?php
    if(isset($_POST['Email'])) {
    $recipient = "chechu@gmail.com";
    $subject = "Newsletter subscription";
    $sender = $recipient;
    $body .= "Add this email address to the newsletter: \n";
    $body .= " ".$_REQUEST['Email']." \n";
    mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
    echo "<div>Thanks for subscribing</div>";
    }
    ?>
    <form method="POST" class="subscriber-form" action="#newsletter">
    <input type="text" class="form-control input-lg" name="Email" placeholder="your@mailadress.com" required>
    <button type="submit" class="btn btn-maincolor" id="send" value="Submit" name="Submit">subscribe</button>
    </form>
    Note that mails sent through PHP mail() often go to spam or are treared as spoof messages.

  3. The Following User Says Thank You to molendijk For This Useful Post:

    chechu (11-24-2019)

  4. #3
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    917
    Thanks
    120
    Thanked 2 Times in 2 Posts

    Default

    Thanks for your reply, Arie.
    The "required" works very well.
    The thing is that I would like to see the submitted mail adres as the sender. Now I see contact@mysite.com as the sender, whilst I'd like to see your@mailadress.com as sender.
    So both sender as the mail adress in the email should be the same.
    Is that possible, please?

  5. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 263 Times in 255 Posts
    Blog Entries
    56

    Default

    Try this:
    Code:
    <?php
    if(isset($_POST['Email'])) {
    $recipient = "chechu@gmail.com";
    $subject = "Newsletter subscription";
    $sender = $_REQUEST['Email'];
    $body .= "Add this email address to the newsletter: \n";
    $body .= " ".$_REQUEST['Email']." \n";
    mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
    echo "<div>Thanks for subscribing</div>";
    }
    ?>
    <form method="POST" class="subscriber-form" action="#newsletter">
    <input type="text" class="form-control input-lg" name="Email" placeholder="your@mailadress.com" required>
    <button type="submit" class="btn btn-maincolor" id="send" value="Submit" name="Submit">subscribe</button>
    </form>

  6. The Following User Says Thank You to molendijk For This Useful Post:

    chechu (11-24-2019)

  7. #5
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    917
    Thanks
    120
    Thanked 2 Times in 2 Posts

Similar Threads

  1. Replies: 4
    Last Post: 02-02-2018, 07:47 AM
  2. Replies: 1
    Last Post: 06-26-2015, 12:16 PM
  3. Replies: 1
    Last Post: 12-03-2013, 07:05 PM
  4. Mail Script No Sender
    By yannickjeh in forum PHP
    Replies: 5
    Last Post: 08-31-2010, 08:53 PM
  5. Replies: 1
    Last Post: 09-17-2009, 10:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •