This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
This is my php code to send email. I got the massage 'Message was sent, you can send another one' But email is not sending.
<h4>Please fill out the following form and we will be in touch with you soon.</h4>
<form action="mytest.php" method="post" id="contactform">
<ol>
<li>
<label for="name">Your Name <span class="red">*</span></label>
<input id="name" name="name" class="text" />
</li>
<li>
<label for="email">Your email <span class="red">*</span></label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" class="text" />
</li>
<li>
<label for="message">Message <span class="red">*</span></label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons">
<input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
<div class="clr"></div>
</li>
</ol>
</form>
<?php
if(!$_POST) exit;
$email = $_POST['email'];
$errors=0;
if($errors==1) echo $error;
else{
$email_from = $_POST['email'];
$email_from = "from#gmail.com";
$headers = "From: " . strip_tags( $_POST['name'] ) . "\r\n";
$mail_to_send_to = "to#gmail.com";
$your_feedbackmail = "from#gmail.com";
$sendflag = 'send';
if ( $sendflag == "send" )
{
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $your_feedbackmail" . "\r\n" . "Reply-To: $email" . "\r\n" ;
$a = mail( $mail_to_send_to, "Feedback Form Results", $message, $headers );
if ($a)
{
print("Message was sent, you can send another one");
} else {
print("Message wasn't sent, please check that you have changed emails in the bottom");
}
}
}
?>
I'm Using Cpanel to host my web site. Is there any special configurations to do this? I'm new to php. Please help me.
mail function doesn't provide authentication functionality. Your have to use Mail class from Mail Pear package. See here for an example:
example
I am assuming you are on shared hosting based on the fact that you are using Cpanel, some shared hosting solutions don't play nicely with php's mail() function, I have found that using phpmailer https://github.com/PHPMailer/PHPMailer works better and provides more functionality
to use phpmailer :
download from the link, place the files in your a folder accessible to your web application.
code :
$email_from = $_POST['email'];
$email_from = "from#gmail.com"; // this overwrites the $_POST['email'] value, check this
$email_from_name = "Nishanthi";
$gmailUsername = "from#gmail.com";
$gmailPassword = "mysecretpassword";
$mail_to_send_to = "to#gmail.com";
$your_feedbackmail = "from#gmail.com";
$emailSubject = "Place Subject Here";
$emailContent = "This message can contain <b>HTML</b>";
require '[path_to_your_phpmailer_files]/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2; //Enable SMTP debugging
$mail->Debugoutput = 'html'; //Ask for HTML-friendly debug output
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $gmailUsername; // SMTP username
$mail->Password = $gmailPassword; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom($email_from, $email_from_name);
$mail->addAddress($mail_to_send_to);
$mail->addReplyTo($your_feedbackmail);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $emailSubject;
$mail->Body = $emailContent;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message was sent, you can send another one';
}
?>
Related
<div class="contact_form clearfix" id="Contact">
<h2>Hello... You can send me message to my universe here.</h2>
<img src="img/planeta1.png" alt="">
<form class="clearfix spaceForm" action="contactform.php" metod="post" >
<label for="name">Your name:</label>
<input type="text" name="name" id="name" placeholder="Jon Doe" required>
<label for="email">Your email:</label>
<input type="text" name="email" id="email" placeholder="something#mama.com" required>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required>
<label for="message">Your message:</label>
<textarea name="message" id="message" required></textarea>
<button type="submit" name="submit">Send mail</button>
</div>
and php code here...
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "pisitenam#sammostalnisindikatstark.org.rs";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html");
}
?>
My contact form instead to send message download on computer as php file. I uploaded my site to netfly but stil doesnt work.
Can anybody help and give me a hint where is problem?
On XAMPP im getting blank page and mail is not sent. When I uploaded site on netfly site works fine but contact from when click submit start download php file where is code writen for controling contact form.5 day im trying to find solution for this problem and im geting tired :D So if anybody can help...
you are having some spell mistake in your form tag, first of all correct the method spell in your code as it is not correct so it can't redirect and post your data to contact form.
mail library contains various function.
for example:
<?php require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'your_smtp_domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('john#example.net', 'John doe'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
this can works for you if you use library.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I've been working on this for some time and am quite new to php. I'm having trouble getting this to send. A second set of eyes on this code would be most helpful:
<?php
if(isset($_POST['submit'])){
$to = "myEmail"; // this is your Email address
$from = $_POST['emailAddress']; // this is the sender's Email address
$fullName = $_POST['fullName'];
$subject = "Form submission";
$message = $fullName . " wrote the following:" . "\n\n" . $_POST['comment'];
$message2 = "Here is a copy of your message " . $fullName . "\n\n" . $_POST['comment'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $fullName . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<form method="post" action="contact.php">
<div class="form-group">
<label for="fullName">Name</label>
<input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe">
<label for="emailAddress">Email</label>
<input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="jane.doe#example.com">
<label for="comment">Comment</label>
<textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea>
<button name="submit" type="submit" class="btn">Submit</button>
</div>
</form>
Many thanks!
I would suggest to use PHPMailer. It is an open-source project available on GitHub : PHPMailer - GitHub
This class permits you to exploit the SMTP services of the most famous mailing platform to get a fast system to send mail using PHP.
It's really simple to set up a code with this class, starting from an HTML form :
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="email" name="from" placeholder="From">
<input type="email" name="to" placeholder="To">
<input type="text" name="subject" placeholder="Subject">
<textearea name="content"></textarea>
</form>
</body>
</html>
<?php
require_once('PHPMailer_5.2.4\class.phpmailer.php');
if(isset($_POST["submit"])){
$username = 'YOUR_GMAIL_ACCOUNT';
$password = 'YOUR_ACCOUNT_PASSWORD';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//$mail->addAttachment($_FILES["image"]["name"]);
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["from"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
$mail->AddAddress($to);
if(!$mail->Send())
{
echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}
}
?>
As you can see in the PHP code, I'm using Gmail SMTP service to send this mail. Note that if you want to use other services you have to change the SMTP server.Furthermore, you need to login into your email service to get an access to the SMTP service, and you need really often to enable the possibility of accessing your mailing account by third part applications too.
In some cases, the SMTP server won't accept TLS or SSL encryption.
Is it possible to add attachment adding enctype="multipart/data" attribute to your form tag and getting the uploaded file through the $_FILES array.
I hope this is going to help you!
In your code parameters of second mail function are not completed you didn't define value of subject2 I think your first message send in the right way but the second will not
I created a simple form on my page and now I tried to add php script to sending email. Unfortunately it does not work. After clicking on the button, I want the user to remain on my side without redirection.
mail_sender.php
<?php
if(isset($_POST['submit'])){
$to = "someone#gmail.com";
$from = $_POST['email'];
$message = " You received the fallowing message:" . "\n\n" . $_POST['message'];
mail($to,$message,$from);
echo "Mail Sent. Thank you, we will contact you shortly.";
}
?>
HTML
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" id="submit" value="Send">
</form>
First of all name attribute is missing in your submit button. And php mail function is wrong.
It should be:
$subject = "Your subject";
$headers = "From: $from ";
mail($to,$subject,$message,$headers);
instead of:
mail($to,$message,$from);
PHP's default mail() function doesn't work most of the times, especially with GMail. This is because your e-mail needs to be formatted in a special way to be accpeted by Google's mail server. You'll be better off using a mail library like PHPMailer.
Here's how to send an e-mail using PHPMailer from a GMail Account.
$mail = new PHPMailer();
// ---------- adjust these lines ---------------------------------------
$mail->Username = "xxx#gmail.com"; // your GMail user name
$mail->Password = "passwd"; // your GMail Password
$mail->AddAddress("yyy#gmail.com"); // recipients email
$mail->FromName = "Your Name"; // readable name
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
//----------------------------------------------------------------------
if(!$mail->Send())
{
echo "mail sent";
}
I tried everything and now i received message SMTP ERROR: Failed to connect to server and SMTP connect() failed
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" name="submit" id="submit" value="Send">
</form>
PHP
<?php
require "PHPMailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer();
$to = "someone#gmail.com"; // required
$from = $_POST['email'];
$comment =
'Email: '.$from.' <br> />
Message: '.$_POST['message'].' <br> />'
;
$mail->Username = "someone#gmail.com"; // your GMail user name
$mail->Password = "Password"; // your GMail Password
$mail->AddAddress("someone#gmail.com"); // recipients email
$mail->setFrom($_POST['email']);
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Subject = 'Here is the subject';
//----------------------------------------------------------------------
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
i have this form:
<form action="/sendemail.php" id="main-contact-form" method="post" name="contact-form" role="form">
<div class="form-group">
<input class="form-control" id="name" name="name" placeholder="put your name" required="" type="text" />
</div>
<div class="form-group">
<input class="form-control" id="email" name="email" placeholder="Email" required="" type="email" />
</div>
<div class="form-group">
<input class="form-control" id="subject" name="subject" placeholder="Subject..." required="" type="text" />
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Mensaje" required="" rows="8"></textarea>
</div>
<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Enviar" />
and this is sendemail.php:
<?php
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message\n $pos";
mail ($to, $subject, $body, $from)
?>
the email is arrive empty, i try a lot of stuffs but always comes in the same way, is like the form canĀ“t pass the data from inputs to php variables. im new in php so, any help is welcome.
thanks.
The use of mail() function is so insecure and is a vector attack to send massive spam, don't use anymore, for this reason is better to use PHP Mailer libray.
Create a EMAIL account (example: no-reply#domain.com) in your hosting and after that install this library PHP Mailer, now you can do this:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
UPDATE
After some research I've found that:
You need to ask godaddy technical support to enable "sendmail".
PHP mail() not working on GoDaddy
----------
I've tested your code and it works. I've received the email with ALL the $_POST contents without any problems.
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
NOTE:
1 - $pos = $_POST; is an array and it will be displayed as Array.
2 - Emails sent via mail() will most likely end-up on the spam folder (read the comments below).
I have been created simple mail sending from localhost using php,
Her is the code:
HTML:
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
EMAIL.PHP:
<?php
require_once('class.phpmailer.php');
$mail->MsgHTML($body);
$body ='A sample email';
$mailer->IsSMTP();
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mailer->Host = 'smtp.gmail.com';
$mailer->Username = 'YYYYYYYYYY#gmail.com';
$mailer->Password = 'XXXXXXXXX';
?>
when run this code,
following error displayed,
Fatal error: Call to a member function MsgHTML() on a non-object online 6
note: where is got phpmailer.php source code from this link
I m new to php, but i want to know, particular this section.
Can anyone help me to fix this,
Thanks in advance,
Perhaps $mail is not instantiated, and spelled wrong (did you mean $mailer?). Also, you should set $body before MsgHTML($body). From your link, you may want to add this
$mailer = new PHPMailer;
and make changes like so:
$mailer = new PHPMailer;
$body ='A sample email';
//$mail->MsgHTML($body);
$mailer->MsgHTML($body);
$mailer->IsSMTP();
...
Here is an example of an HTML-form and PHPMailer:
HTML
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
mail.php
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require_once('class.phpmailer.php');
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer#bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_PHPMailer#bradm.inmotiontesting.com"; // SMTP username
$mail->Password = "password"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>