Log in

View Full Version : mail form sender ID and error



chechu
11-20-2019, 02:36 PM
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!


<?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>

molendijk
11-21-2019, 10:34 PM
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:
<?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.

chechu
11-24-2019, 07:32 PM
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?

molendijk
11-24-2019, 08:04 PM
Try this:
<?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>

chechu
11-24-2019, 08:37 PM
Works like a charm, Arie.
Thanks.