After researching several questions and try multiple options, I couldn't make it work still!
I have this contact form that won't be sent.
<form id="contact" method="post" action="process.php">
<fieldset>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Your name" title="Your name" class="required">
<label for="email">E-mail:</label>
<input type="email" name="email" placeholder="yourmail#domain.com" title="Your e-mail address" class="required email">
<label for="phone">Phone number:</label>
<input type="tel" name="phone" placeholder="+34 111 22 33 44" title="Your phone number">
<p> </p>
<input type="radio" name="rsvp" value="si">
<span class="destacar-contacto">ACCEPT</span> the invitation<br>
<input type="radio" name="rsvp" value="no">
<span class="destacar-contacto">REJECT</span> the invitation<br>
<p> </p>
<p class="negrita-contacto">If you're coming: what would you like better?</p>
<input type="radio" name="menu" value="carne"> Calf sirloin with foie<br>
<input type="radio" name="menu" value="pescado"> Marinade salmon with dill<br>
<input type="radio" name="menu" value="vegetariano"> Fungus risotto<br>
<label for="mas">Is someone coming with you? Let us know their name and their prefered menu here:</label>
<textarea name="mas"></textarea>
<label for="message">Aditional message:</label>
<textarea name="message"></textarea>
<input type="submit" name="submit" class="boton" id="submit" value="Enviar" />
</fieldset>
</form>
And this is the PHP function:
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$rsvp = strip_tags($_POST['rsvp']);
$menu = strip_tags($_POST['menu']);
$mas = strip_tags($_POST['mas']);
$message = strip_tags($_POST['message']);
mail( "formulario#ourdreamjourney.com", "rsvp",
"Name: $name\nEmail: $email\nPhone: $phone\nRsvp: $rsvp\nMenu: $menu\nMas: $mas\nMessage: $message\n",
"From: Our Dream Journey <mail#hotmail.com>" );
I tried randomly to send just "Name: $name" and I got an e-mail! But then put all the other options back and nothing again...
Someone can help me please?
Thank you so much in advance! :)
You can use phpMailer class. Download this library from here. Many example with to send email.
http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list
Edit:
Try this code
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From:Our Dream Journey <mail#hotmail.com>' . "\r\n";
if(mail( "formulario#ourdreamjourney.com", "rsvp",nl2br("Name: $name\nEmail: $email\nPhone: $phone\nRsvp: $rsvp\nMenu: $menu\nMas: $mas\nMessage: $message\n"),
$headers )) echo "Sent"; die;
Try to add all the information to the header element:
$headers = "From: myplace#here.com\r\n";
$headers .= "Reply-To: myplace2#here.com\r\n";
$headers .= "Return-Path: myplace#here.com\r\n";
$headers .= "CC: sombodyelse#noplace.com\r\n";
$headers .= "BCC: hidden#special.com\r\n";
Maybe there also other options that I don't remember like MIME
Maybe another problem is that some of the variables break the string(close the string). Try to print it somewhere,a file with file_put_contents or just echo it
Related
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.
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.
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
I'm writing to create a contact form on my website and then get the information sent to my inbox, however it is not working whatsoever. Please take a look at my code below (PHP is not my thing) and let me know where i've gone wrong.
Here's the PHP script:
<?php
$to = 'example#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];
$project = $_POST['project'];
$range1 = $_POST['range1'];
$range2 = $_POST['range2'];
$body = <<<EMAIL
Hi, my name is $name.
I'd like to discuss the possibility of working together on a $project.
My budget for the project is £$range1 and I would like to complete the project within $range2 Month(s).
Additional Information:
$message
Regards, $name
<hr>
$name
$email
$tel
EMAIL;
$header = "From: $email";
if($_POST['send']){
mail($to, $subject, $body, $header);
$feedback = 'Thank you for your message, we will get back to you shortly.';
}
?>
And here's the HTML form:
<form id="form_id" name="form_name" action="" method="post">
<div>
<input type="text" name="name" id="name" placeholder="Name" required/>
</div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
</div>
<div>
<input type="tel" name="tel" id="tel" placeholder="Phone" required/>
</div>
<div>
<select required id="project">
<option selected>Select type of project…</option>
<option value="Responsive Website">Responsive Web Design</option>
<option value="Graphic Design">Graphic Design</option>
<option value="Motion Graphics">Motion Graphics</option>
</select>
</div>
<div>
<label for="range1">Budget: </label>
<input type="range" name="range1" id="range1" min="400" max="2000" step="50" value="6" required onchange="rangevalue.value=value"><output id="rangevalue">400</output>
</div>
<div>
<label for="range2">Timeframe: </label>
<input type="range" name="range2" id="range2" min="1" max="12" step=".5" value="1" required onchange="rangevalue1.value=value"><output id="rangevalue1">1</output>
</div>
<div>
<label for="message">Additional Information: </label><br/>
<p>(Please use this space to tell us about your company, the main objectives of the proposed website and anything else you think might be useful)</p>
<textarea name="message" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
Thanks for the help. FYI this can be achieved easily with WordPress through Contact Form 7 (or a similar plugin).
<input type="submit" name="submit" value="submit" />
You have to change this line to:
<input type="submit" name="send" value="submit" />
if($_POST['send']){
actually checking if submit button is clicked...
And, yes - if html and php are on different pages, you have to set proper form action link...
I think you should take a look at this or this, be aware that despite W3Schools may serve as a basics tutorial because of the friendly-user examples, always complement with other resources look here why.
Then you can take a look at this answers, this is just because you need some more basis to understand everything you are doing.
I can't see your $_POST['send'] name in the form, or it's just too late and I'm tired:
Don't know, but maybe, you want this in your form before the submit:
<input type="hidden" name="send" >
Then:
I already posted this answer HERE, but here it is:
First i think your missing some headers look at those
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
2nd check if its true or false when calling the mail function
if( mail($email_to, $email_subject, $email_message, $headers)!==true)
{
die('Fail to send');
}
die('Success');
}
You should take a look at the isset() function to make sure all your variables are set from the form.
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.