I am trying to integrate a ‘subscribe to newsletter’ feature on my wordpress blog
Users just need to enter their name and email address, hit subscribe. This is then supposed to send me an email and I add them to the mailing list.
I activated the WP SMTP Mail plugin and entered in the relevant email server information to enable WordPress to send emails, asked WordPress to send me a test message and this worked.
Now I want to receive an email whenever someone fills in their name & email and hit subscribe.
If I do this at the moment it just takes me to newsletter_signup.php and I get at error message saying…
‘NOT FOUND Apologies, but no results were found for the requested
post.’
Obviously this is not right and no email is being sent/received.
I have attached my code below, can anyone help me out here?
Thanks
Code for the signup form sidebar.php - This is not a plugin.
<form action="newsletter_signup.php" method="post">
<p><input class="full" type="text" name="name" placeholder="Your name*"></p>
<p><input class="full" type="email" name="Email" placeholder="Your email address*"></p>
<p><input type="submit" class="sub-btn" value="Subscribe"></p>
</form>
newsletter_signup.php
<?php
$name = $_POST['name'];
$Email = $_POST['Email'];
$to = 'me#myself.com';
wp_mail($to, $name, 'From: ' . $Email);
echo 'Your request Has Been Sent, Thank You. ';
?>
You need to include wp-load.php to use wp functions like wp-mail.Here is the full code:
<?php
require_once('wp-load.php');
$name = $_POST['name'];
$Email = $_POST['Email'];
$to = 'me#myself.com';
wp_mail($to, $name, 'From: ' . $Email);
echo 'Your request Has Been Sent, Thank You. ';
?>
Related
I am trying to get a simple two-field form to submit to an email address and then echo a "thanks for registering your interest" below the form (or instead of the form).
FYI, this is on a WordPress template file.
Here is the code, including the form:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="POST" autocomplete="on" id="register-form">
<input type="text" name="name" placeholder="Name"/>
<input type="email" name="email" placeholder="Email address"/>
<button type="submit" name="submit" class="button">Send
<img src="<?= get_image('icon-arrow-right-tiny.svg'); ?>"/></button>
</form>
<?php
if (isset($_POST['submit'])) {
// validate the email address first
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
// process the form only if the email is valid
if ($email) {
$to = 'info#example.com'; // where you want to send the mail
$from = 'info#mydomain.com';
$subject = 'Website Submission';
$message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email Address: ' . $_POST['email'] . "\r\n\r\n";
$headers = "From: $from\r\nReply-to: $email";
$sent = mail($to, $subject, $message, $headers);
} ?>
<p style='color: #fff; font-size: 14px;'>Thank you for registering your interest.</p>
<?php
}
?>
At the present time, the form does get sent, and the page does echo "Thank you for registering your interest" underneath the form, however it does not seem to be returning us to the correct page when you click the submit button.
Any ideas?
Thank you for all of your contributions. I have worked out the problem, and will share here for anybody else who comes here to find the answer.
WordPress has something important reserved for the "name" parameter, and thus you can't use it in PHP-based forms. Changing the parameter name from "name" to something else resolved the issue.
Additionally, WordPress also has the following names reserved and you cannot use them in forms - "day" "month" and "year".
I have check your code i think you have use color code #fff i.e. for the message.
Please try to make black or any other color rest of code are working.
:)
Thank you for registering your interest.
You have to put a php code below your Thank you message.
header("location:$_SERVER["PHP_SELF"]);exit;
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I have a registration form that requires Name, Phone, and and an optional comments section. After running multiple tests, the form seems to submit and (the bottom of the php code) the page redirects me to the "thank you page". But I don't get an email whatsoever. Anyone know what I'm doing wrong?
<form class="register-form" name="registerform" method="post" action="form-to-email.php">
<div class="form-column">
<p>Fields marked with * are required.</p>
<label for="first_name">First Name *</label>
<input type="text" id="first_name" name="first_name" placeholder="John">
<label for="last_name">Last Name *</label>
<input type="text" id="last_name" name="last_name" placeholder="Smith">
<label for="phone">Phone Number</label>
<input type="text" id="phone" name="phone" placeholder="503 999 9999">
<label for="comments">Comments</label>
<textarea id="comments" name="comments"></textarea>
</div><!--/ Form Column -->
<div class="submit-wrap"><input class="submit-form" type="submit" value="Register" /></div>
</form><!--/ Form -->
The PHP looks like this:
<?php
$myemail = 'veeeeeech#gmail.com';
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$visitor_phone = $_POST['phone'];
$comments = $_POST['comments'];
$to = $myemail;
$email_subject = "$first_name\n $last_name\n registered for Winter Camp 2015";
$email_body = "They said: \n $comments". "Their phone number is $visitor_phone.\n".
$headers = "From: $myemail\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: registered-thanks.html');
?>
Mail delivery is an inexact science from the programmer's POV. The system admin has a bit more control over it ... are you also the "box owner"?
You're not even checking to see if the mail() call failed:
$success = mail($to,$email_subject,$email_body,$headers);
if ($success) header('Location: registered-thanks.html');
else die("Could not send mail!!");
Of course that is way simplified ... better error handling would be a nice start.
Assuming that ($success) is returning true, it's then up to the system to do the right thing. Questions you'll need to ask include: is my PHP installation properly set up to do mail() ?
Does my web host allow it?
Is there a firewall blocking outbound SMTP connections?
What do the server logs say?
Did the mail get caught in a spam filter?
You can see why I say "an inexact science"....
PHP's mail function will return true when the message has been accepted for delivery. This is different from actually delivering or guaranteeing delivery, as noted by Kkinsey.
If you have shell access to the server where your web files are hosted you can test out whether or not you can even send mail out by trying this.
mail -s "Test Email" veeeeeech#gmail.com
Enter a line of text, then on a line all by itself enter a single "." and hit return. Look for the mail in your gmail account.
You might also try using sendmail directly as a quick test.
sendmail veeeeeech#gmail.com
have you checked the spam folder? some services will reject the email if it doesnt have a reply-to header.
$myemail = 'veeeeeech#gmail.com';
$to = $myemail;
$headers = "From: $myemail \r\n";
$headers .= "Reply-To: $myemail \r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$success = mail($to,$email_subject,$email_body,$headers);
if ($success) header('Location: registered-thanks.html');
else die("Foobar. Your mom.");
I have searched for this answer on Google and Youtube but have not found an answer.
I have created a very simple contact form on my website.
(http://www.torontoimc.com/contact)
I have attached a seperate PHP file to process the contact form elements.
When someone sends me an inquiry through the form on my site, I'm receiving all of the information except the person's email address input section info.
It's very important that I receive that email address otherwise I won't be able to reply to whom ever sent it to me.
I have tried setting the form to be sent to my gmail and outlook email but it just sends it as:
It just shows that the sender's email address as some random name "#hemi.websitewelcome.com"
I'm not sure if this is a server side issue or an issue with PHP file.
I have attached the code for both my Contact form and PHP file.
Here is the contact form code:
<form action="formprocess.php" method="post" name="contact">
<fieldset>
<label>First Name</label>
<input name="first_name" type="text"/>
<label>Last Name</label>
<input name="last_name" type="text"/>
<label>Email</label>
<input name="email_add" type="text"/>
<label>Please write your inquiry below.</label>
<textarea name="message" rows="15" cols="40">
</textarea>
<input type="submit" name="submit" id="submit" value="Submit"/>
<input type="reset" name="reset" id="reset" value="Reset"/>
</fieldset>
</form>
Here is the php code:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_add = $_POST['email_add'];
$message = $_POST['message'];
$to = "torontoimc#gmail.com";
$subject = "Inquiry for TorontoIMC";
mail ($to, $subject, $message, "From: " . $first_name . $last_name . $email_add);
header('Location: thanks.html');
exit();
?>
I apologize if this is a repeat question but I'm a newbie to php and have not really been able to find an answer, if someone can please help me out, I would really appreciate it.
Thanks,
Latish Vohra
Try this:
$headers = "From: $first_name $last_name <$email_add>\r\n";
mail ($to, $subject, $message, $headers );
I added "\r\n" to From
I understand what you are trying to do on the FROM line, but you should use the "real" FROM sender. Many servers will not allow you to use a FROM line from a domain that is not the host's (think about it, the truth is you are lying, the person that filled the form did not actually send that email).
Secondly, follow user4035's way of styling the email in the FROM, if you are going to use a name last name <email>, it may not only be the way to do it for normal practice, also in the way that you wrote it, joining the fields without spaces, you might end up with a FROM line such as this FrançoisMARTIN MELLIERfmartinm#gmail.comwhich may contain illegal characters and/or spaces that may cause some servers to choke on or discard. Instead 'FROM: '. $first_name . ' '. $last_name. ' <'. $email_add. '>' will produce FROM: François Martin Mellier <fmartinm#gmail.com>. But again, you should use a real account from the same domain you are sending (maybe something like 'FROM: New enquiry <info#torontoimc.com>' ?). As it's been pointed out, include the email_add field in the body of the message or the subject line in order to receive it; those are the proper places.
Using the -f parameter like mti2935 pointed out is always a good idea, but only if that email coount belongs to the same domain as the server's.
As a side note, try to take errors into account, a minimum:
if (mail ($to, $subject, $message, $headers )) {
header('Location: thanks.html');
} else {
header('Location: error.html');
}
might go a long way.
created a simple little php code to populate email with email address and info from a textbox on the form. it originally worked when I was calling to the script from a html form, but once I converted my site to PHP it stopped working. Eventually I would like to put this same info into a Database table but right now I would be content just getting the email to work.
When the form is submitted I get the email address from the customer, but I don't get the info from the textbox. here is my code.
<?php
$email = $_POST['email'];
$message = $_POST['message'];
mail( "sales#sixtoed-design.com", "Service Request", "From: $email",
$message );
header( "Location: http://www.sixtoed-design.com/thankyou.php" );
?>
Like I said it is a very simple code and worked fine before I converted my site completely to PHP.
Below is my code for the form if you need it.
<form method="POST" action="sendmail.php" enctype="multipart/form-data">
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input name="" type="submit" value="Send Email">
</form>
You're using the mail function wrong. See the docs.
The 3rd parameter should be the message and you send from as a header in the 4th parameter, so:
mail( "sales#sixtoed-design.com", "Service Request", $message, "From: $email" );
See example 2 in the docs
I'm sitting here wondering wheather this php contact form solution is too simple to actually work. Or, well it does work, but will it always work?
Also, atm when you recieve the email, it says FROM: zsf34o6.mywebsite.com#web09.b-one.com
which means that most mail clients will put it straight in the junkbox. How can I change it to the entered email address?
Thanks!
<form method="POST" action="mailer.php">
<input type="text" name="name" size="19">
<input type="text" name="phone" size="19">
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
$to = "you#you.com";
$subject = "From website";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$body = "From: $name_field\n E-Mail: $phone_field\n";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "Error!";
}
?>
It will work, but your recipient is going to get spammed heavily after it has been out for a while. You can cut back on that a lot by putting another field in the form that is hidden with CSS and then checking that it is still empty before sending your email. As for adjusting the return address, use the forth parameter to the PHP mail function. It will look something like this:
mail($to, $subject, $body, "From:$fromAddress\n");
Here is some quick and dirty code that I have used for a similar form for a website to relay a message to someone's smartphone in a way that makes it easy to give a call back while on the go:
http://rietta.com/stackoverflow/sample-form.txt