Can someone find where's wrong in this code PHP email - php

I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>

$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.

i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>

You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.

Related

php and html email forms

I am creating a website for my church choir in HTML. I have a "contact us" page written in HTML. It has a form for the user to send the choir director an email from the website. I am aware that I have to write the email in php in order for the email to send. Do I need to duplicate the html file and rewrite it in PHP?
<form name="Send-mail" action="mailto:email#email.com" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
I need the server to send the email from the website to the email of our choir director.
You would need to create the PHP file where there is an email sending logic,eg: sendEmail.php. The form tag would change to:
<form name="Send-mail" action="sendEmail.php" method="post">
Below is how you can send the mail in php from form inputs.
<form action="mail.php" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
You will have to name your php file mail.php like in case below
mail.php
<?php
$to = $_POST['email'];
$subject = $_POST['subject'];
$sender = $_POST['name'];
$message = $_POST['message'];
$message .= "<h1>This is Message from $sender.</h1>";
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
You will need to ensure that email for this line of code is correctly set.
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
In otherword, if your site email is support#hey.com
Try Replacing it snderemail#somedomain and it will work. You can also try the replacement with some other emails like good working gmail and let me know what happen. Thanks

Don't know what to write in my message variable?

I am a PHP noob, so i don't know what to write in my message variable. I need my data from the form to be sent to the email. I got a textarea, 2 inputs (name, email). Want the text from input to be sent to my email. Here code i have:
<?
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'rayetzkiillya#gmail.com';
$subject = 'Обратный звонок';
$message;
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Отправитель <from#example.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>
<form action="send.php" class="postcard" method="post">
<span>Your message:</span>
<textarea type="text" value="" required></textarea>
<div id="stripe1"></div>
<div id="stripe2"></div>
<img src="./images/Seal_of_the_United_States_Department_of_the_Post_Office.svg. png" alt="Oops" id="seal" />
<img src="./images/stamp.jpg" alt="Stamp" id="stamp" />
<div class="inputs">
<div class="inputs" id="input1"><label for="to" type="text" id="to">to: </label> <input type="text" value=" Me" readonly><div id="stripe3"></div></div>
<div class="inputs" id="input2"><label for="from" type="text" id="from">from: </label> <input type="text" id="input2"><div id="stripe3"></div></div>
<div class="inputs" id="input3"><label for="email" type="text" id="email">email: </label> <input type="text" id="input3"><div id="stripe3"></div></div>
<button type="button" class="btn btn-primary" id="send_button">Send</button>
</form>
You can make your message an HTML string, or just plain text.
For plain text, you can do something like
$message .= "name: ".$_POST['name']."\r\n";
$message .= "Message: ".$_POST["theMessage"];
However you need to name your inputs in the HTML as well,
so
<input type="text" id="input2" name="name">
<textarea type="text" value="" required name="theMessage"></textarea>
Or similar.
For the from address, you have to use change your header line
change
$headers .= "From: Отправитель <from#example.com>\r\n";
to
$headers .= "From: ".$_POST["name"]." <".$_POST["from"].">\r\n";
And obviously name your input accordingly:
<input type="text" id="input3" name="from">
Also, as suggested by others, you should be sanitizing/validating these values before using them.

Send HTML Form data Via PHP

can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>

PHP/HTML form data to send as an email from my GMAIL account

I am trying to send an email to myself when someone fills in the form on my web page. My form looks like this:
<form id="contactForm" action="#" method="post">
<fieldset>
<p>
<label for="name" >Name</label>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your full name" />
</p>
<p>
<label for="email" >Email</label>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
</p>
<p>
<label for="web">Website</label>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your website" />
</p>
<p>
<label for="comments">Message</label>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</p>
<!-- send mail configuration -->
<input type="hidden" value=EMAIL" name="to" id="to" />
<input type="hidden" value="Email received from Website" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
and the PHP i have looks like this:
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'EMAIL',
'password' => 'PASSWORD'
));
$mail = $smtp->send($to, $headers, $msg);
?>
Where EMAIL and PASSWORD are replaced with the values....
Can anyone advise on what to do?
Update host to smtp.gmail.com and port to 587 and update us with the results.
Edit (all in one)
PHP - and HTML together (and replace the mail() function with your own SMTP)
I.e.:
$mail = $smtp->send($to, $subject, $msg, $headers);
New code:
<?php
//vars
if(isset($_POST['submit'])){
$subject = $_POST['subject'];
$to = "email#example.com";
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
// insert with your SMTP
mail ($to, $subject,$msg, $headers);
echo "sent";
}
?>
<form id="contactForm" action="" method="post">
<fieldset>
<p>
<label for="name" >Name</label>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your full name" />
</p>
<p>
<label for="email" >Email</label>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
</p>
<p>
<label for="web">Website</label>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your website" />
</p>
<p>
<label for="comments">Message</label>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</p>
<!-- send mail configuration -->
<input type="hidden" value="Email received from Website" name="subject" id="subject" />
<!-- ENDS send mail configuration -->
<p><input type="submit" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
Original answer
There were quite a few issues with your code.
Your parameters are off ($to, $headers, $msg) headers goes last and the $subject was not included; it must be in there.
The syntax is ($to, $subject,$msg, $headers)
it's TO, then Subject, then Message, then Headers. As per the mail() manual Even though you are using SMTP, mail is mail.
And I don't know why you're doing:
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
Just set it in your form's action=
Here is what worked for me: and you can replace:
mail ($to, $subject, $msg, $headers);
with (and if it works for you) It seems there were other answers given in regards to this.
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'EMAIL',
'password' => 'PASSWORD'
));
$mail = $smtp->send($to, $headers, $msg);
PHP - tested (send-mail.php)
<?php
//vars
if(isset($_POST['submit'])){
$subject = $_POST['subject'];
$to = "email#example.com";
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
mail ($to, $subject,$msg, $headers);
echo "sent";
}
?>
HTML form
<form id="contactForm" action="send-mail.php" method="post">
<fieldset>
<p>
<label for="name" >Name</label>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your full name" />
</p>
<p>
<label for="email" >Email</label>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
</p>
<p>
<label for="web">Website</label>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your website" />
</p>
<p>
<label for="comments">Message</label>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</p>
<!-- send mail configuration -->
<input type="hidden" value="Email received from Website" name="subject" id="subject" />
<!-- ENDS send mail configuration -->
<p><input type="submit" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
Footnotes:
Missing quote in value=EMAIL" but that has been replaced in my PHP with:
$to = "email#example.com";
You have to check first the value of variable $to is a correct mail direction. Based on the code you posted probably will wrong.
On the php code you call the class Mail but you don't include/require, add this:
// Pear Mail Library
require_once "Mail.php";
After send mail you cant show the result with this:
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
First, check your spam in your mailbox. If gmail has received your mail, it must be there. If not, check your mail server configuration and if it is authorized to send mail for your domain. You can also check mail server logs for potential problems.
In any case, I would recommend you to use solutions like Mailjet or MailChimp. You can sign up for basic package and get your email. The advantage with the solution like this is that you dont need to be maintaining your mail server and you can check the status of every mail being processed.
Here's a cheap and easy way to do it.
In your php.ini replace the relevant parts of the mail function with
[mail function]
; For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 587
; For Win32 only.
sendmail_from = EMAILADDRESSHERE
Replace sendmail.ini with
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
auth_username=EMAILADDRESSHERE
auth_password=PASSWORDHERE
force_sender=EMAILADDRESSHERE
Then in your code, send an email much the same except write it as
mail($to, $subject, $msg, $headers)
or modify to suit.

static page send email WordPress

I'm new to WordPress. I create a static page contact.php. In contact page, I have a contact form, so users would get in touch with me. When the user would click the submit button it should go to index.php and prompting the success of email delivery otherwise display not successful..
this is the code of contact.php
<form class="form" method="post" action="/send-email.php">
<p class="name">
<input type="text" name="name" id="name" placeholder="Enter your name" size="40" />
<label for="name">Name</label>
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" size="40" />
<label for="email">Email</label>
</p>
<p class="web">
<input type="text" name="telephone" id="telephone" placeholder="000-000-000" size="40" />
<label for="telephone">Telephone</label>
</p>
<p class="text">
<label for="message">Message</label>
<textarea name="message" placeholder="Write something to us" cols="40" rows="5" /></textarea>
</p>
<p class="submit">
<input type="submit" value="Send" />
<input type="reset" value="Cancel" />
</p>
</form>
send-email.php
<?php
$ToEmail = 'sample#sample.com';
$EmailSubject = 'Contact Form';
$mailheader = "From: " . $_POST["email"] . "\r\n";
$mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
$MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
$MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
$MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");
/* echo "<pre>";
print_r($MESSAGE_BODY);
echo "<pre>"; */
header("Location: /index");
But it's not sending. And the URL looks just like www.site.com/send-email.php
What am I missing in here? Any ideas? I would really appreciate your help. Thanks.
Sounds like die("Failure") might be kicking in and logging the "Failure" message instead of printing it on screen. Or the mail module isn't installed, so it is running into an error where it doesn't know what the mail() function even is—admittedly unlikely. Not too sure.
My guess is that your server is not even set up to send mail properly yet. That can be a complicated setup if you are running your own server. If you are on a shared server, it really should be set up already... unless your host is not very good.
In any case, if you are running in WordPress I'd recommend installing the Jetpack plugin by the folks who made WordPress itself. It includes a feature that adds a contact form to a page with a few clicks of your cursor.
If that form won't send an email, there is something wrong that is beyond the scope of the information you provided in your question.
<form class="form" method="post" action="index.php">
<p class="name">
<label for="name">Name</label>
<input type="text" name="name" id="name" placeholder="Enter your name" size="40" />
</p>
<p class="email">
<label for="email">Email</label>
<input type="text" name="email" id="email" placeholder="mail#example.com" size="40" />
</p>
<p class="web">
<label for="telephone">Telephone</label>
<input type="text" name="telephone" id="telephone" placeholder="000-000-000" size="40" />
</p>
<p class="text">
<label for="message">Message</label>
<textarea name="message" placeholder="Write something to us" cols="40" rows="5" /></textarea>
</p>
<p class="submit">
<input type="submit" name="submit" value="Send" />
<input type="reset" value="Cancel" />
</p>
</form>
AND YOUR index.php
<?php
if(isset($_POST['submit'] == 'Send')) {
$ToEmail = 'sample#sample.com';
$EmailSubject = 'Contact Form';
$mailheader = "From: " . $_POST["email"] . "\r\n";
$mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
$MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
$MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
$MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
$status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");
}
if($status){
echo "your success message";
}
?>
try this
ONE MORE THING
Since you are using this on wordpress you should better use wp_mail funciton of wordpress

Categories