send email with php in a local host - php

I want to send email with php using xampp.
<html>
<head>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$to = $_POST['email_add'];
//define the subject of the email
$subject = $_POST['sbjct'];
//define the message to be sent. Each line should be separated with \n
$message = $_POST['msg'];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$mail_sent = #mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
<form action="send_email.php" method="post">
Email Add<input type="text" name="email_add" />
Subject<input type="text" name="sbjct" /> <br>
<textarea name="msg" rows="9" cols="20"></textarea><br>
<input type="submit" value="Send"/>
</form>
</body>
</html>
It's not working. Is there something wrong with my code?
I found some tutorials on the cloud, but it mentions SMTP and I do not understand it.

PHP doesn't actually send the email... in Linux it'll forward it to sendmail or similar daemon, in Windows it'll forward it to an SMTP server. Depending on your OS you have to setup these other processes and explain to PHP how/where to contact them (in php.ini in the [mail function] section).

PHP need a SMTP server to send emails.
You can specify it in your php config file (and set it to your ISP one for example), or use "Test Mail Server Tool" ( http://www.toolheap.com/test-mail-server-tool/ ) that catches the smtp local calls and saves them in a directory of your hard disk, so that you can debug everything.
Obviously, this solution works only if you need to debug it.
If your need is to send the mail, please refer to php documentation and use your ISP SMTP server.

If you are using the mail() function on Windows from a local development environment, you may need to specify an SMTP server that can relay the mail for you. Linux has this functionality baked in, Windows may not. Your ISP may provide you with this or a webmail provider may allow you to relay mail through them.
Look inside your php.ini file and locate the [mail] section and fill in:
SMTP = smtp.yourprovider.com
SMTP_PORT = 25
From the PHP mail manpage
"The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine)."

I think you're looking for http://www.php.net/manual/en/function.mail.php ?

<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Email Add<input type="text" name="email_add" />
Subject<input type="text" name="sbjct" /> <br>
<textarea name="msg" rows="9" cols="20"></textarea><br>
<input type="submit" name="submit" value="Send"/>
</form>
</body>
</html>
<?php
If(isset('submit'))
{
error_reporting(E_ALL);
$to = $_POST['email_add'];
//define the subject of the email
$subject = $_POST['sbjct'];
//define the message to be sent. Each line should be separated with \n
$message = $_POST['msg'];
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$mail_sent = mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
}
?>

Here is a script I re-use in a lot of my projects, pretty straight forward just copy in and change the obvious values.
$email_to = "myemail#gmail.com,anotheremail#gmail.com";
$email_subject = "This is the subject line";
$break = 'echo "/n"';
$form_email = "no-reply#myemail.com";
//Function to convert /n to line breaks in HTML
function nl2br2($email_message) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $email_message;
}
//Unformatted email body
$email_message = "This is the main blody of the email";
//Format string against function
nl2br2($email_message);
// create email headers
$headers = 'From: '.$form_email."\r\n".
'Reply-To: '.$form_email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);

Related

How I can send data to the form and submit all to an email? [duplicate]

I want to send an email with PHP when a user has finished filling in an HTML form and then emailing information from the form. I want to do it from the same script that displays the web page that has the form.
I found this code, but the mail does not send.
<?php
if (isset($_POST['submit'])) {
$to = $_POST['email'];
$subject = $_POST['name'];
$message = getRequestURI();
$from = "zenphoto#example.com";
$headers = "From:" . $from;
if (mail($to, $subject, $message, $headers)) {
echo "Mail Sent.";
}
else {
echo "failed";
}
}
?>
What is the code to send an email in PHP?
EDIT (#1)
If I understand correctly, you wish to have everything in one page and execute it from the same page.
You can use the following code to send mail from a single page, for example index.php or contact.php
The only difference between this one and my original answer is the <form action="" method="post"> where the action has been left blank.
It is better to use header('Location: thank_you.php'); instead of echo in the PHP handler to redirect the user to another page afterwards.
Copy the entire code below into one file.
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$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 " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Original answer
I wasn't quite sure as to what the question was, but am under the impression that a copy of the message is to be sent to the person who filled in the form.
Here is a tested/working copy of an HTML form and PHP handler. This uses the PHP mail() function.
The PHP handler will also send a copy of the message to the person who filled in the form.
You can use two forward slashes // in front of a line of code if you're not going to use it.
For example: // $subject2 = "Copy of your form submission"; will not execute.
HTML FORM:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP handler (mail_handler.php)
(Uses info from HTML form and sends the Email)
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$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 " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
To send as HTML:
If you wish to send mail as HTML and for both instances, then you will need to create two separate sets of HTML headers with different variable names.
Read the manual on mail() to learn how to send emails as HTML:
http://php.net/manual/en/function.mail.php
Footnotes:
In regards to HTML5
You have to specify the URL of the service that will handle the submitted data, using the action attribute.
As outlined at https://www.w3.org/TR/html5/forms.html under 4.10.1.3 Configuring a form to communicate with a server. For complete information, consult the page.
Therefore, action="" will not work in HTML5.
The proper syntax would be:
action="handler.xxx" or
action="http://www.example.com/handler.xxx".
Note that xxx will be the extension of the type of file used to handle the process. This could be a .php, .cgi, .pl, .jsp file extension etc.
Consult the following Q&A on Stack if sending mail fails:
PHP mail form doesn't complete sending e-mail
PHP script to connect to a SMTP server and send email on Windows 7
Sending an email from PHP in Windows is a bit of a minefield with gotchas and head scratching. I'll try to walk you through one instance where I got it to work on Windows 7 and PHP 5.2.3 under (IIS) Internet Information Services webserver.
I'm assuming you don't want to use any pre-built framework like CodeIgniter or Symfony which contains email sending capability. We'll be sending an email from a standalone PHP file. I acquired this code from under the codeigniter hood (under system/libraries) and modified it so you can just drop in this Email.php file and it should just work.
This should work with newer versions of PHP. But you never know.
Step 1, You need a username/password with an SMTP server:
I'm using the smtp server from smtp.ihostexchange.net which is already created and setup for me. If you don't have this you can't proceed. You should be able to use an email client like thunderbird, evolution, Microsoft Outlook, to specify your smtp server and then be able to send emails through there.
Step 2, Create your Hello World Email file:
I'm assuming you are using IIS. So create a file called index.php under C:\inetpub\wwwroot and put this code in there:
<?php
include("Email.php");
$c = new CI_Email();
$c->from("FromUserName#foobar.com");
$c->to("user_to_receive_email#gmail.com");
$c->subject("Celestial Temple");
$c->message("Dominion reinforcements on the way.");
$c->send();
echo "done";
?>
You should be able to visit this index.php by navigating to localhost/index.php in a browser, it will spew errors because Email.php is missing. But make sure you can at least run it from the browser.
Step 3, Create a file called Email.php:
Create a new file called Email.php under C:\inetpub\wwwroot.
Copy/paste this PHP code into Email.php:
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
Since there are many kinds of smtp servers, you will have to manually fiddle with the settings at the top of Email.php. I've set it up so it automatically works with smtp.ihostexchange.net, but your smtp server might be different.
For example:
Set the smtp_port setting to the port of your smtp server.
Set the smtp_crypto setting to what your smtp server needs.
Set the $newline and $crlf so it's compatible with what your smtp server uses. If you pick wrong, the smtp server may ignore your request without error. I use \r\n, for you maybe \n is required.
The linked code is too long to paste as a stackoverflow answer, If you want to edit it, leave a comment in here or through github and I'll change it.
Step 4, make sure your php.ini has ssl extension enabled:
Find your PHP.ini file and uncomment the
;extension=php_openssl.dll
So it looks like:
extension=php_openssl.dll
Step 5, Run the index.php file you just made in a browser:
You should get the following output:
220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at
Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0
<534edd7c92761#summitbroadband.com> Queued mail for delivery
lang:email_sent
done
Step 6, check your email, and spam folder:
Visit the email account for user_to_receive_email#gmail.com and you should have received an email. It should arrive within 5 or 10 seconds. If you does not, inspect the errors returned on the page. If that doesn't work, try mashing your face on the keyboard on google while chanting: "working at the grocery store isn't so bad."
If you haven't already, look at your php.ini and make sure the parameters under the [mail function] setting are set correctly to activate the email service. After you can use PHPMailer library and follow the instructions.
You can also use mandrill app to send the mail in php. You will get the API from https://mandrillapp.com/api/docs/index.php.html where you can find the complete details about emails sended and other details.
You need to add an action into your form like:
<form name='form1' method='post' action='<?php echo($_SERVER['PHP_SELF']);'>
<!-- All your input for the form here -->
</form>
Then put your snippet at the top of the document en send the mail. What echo($_SERVER['PHP_SELF']); does is that it sends your information to the top of your script so you could use it.
You need a SMPT Server in order for
... mail($to,$subject,$message,$headers);
to work.
You could try light weight SMTP servers like xmailer
Here are the PHP mail settings I use:
//Mail sending function
$subject = $_POST['name'];
$to = $_POST['email'];
$from = "zenphoto#example.com";
//data
$msg = "Your MSG <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 "Mail Sent.";
I think one error in the original code might have been that it had:
$message = echo getRequestURI();
instead of:
$message = getRequestURI();
(The code has since been edited though.)

How can I have a form submit 2 actions? [duplicate]

I want to send an email with PHP when a user has finished filling in an HTML form and then emailing information from the form. I want to do it from the same script that displays the web page that has the form.
I found this code, but the mail does not send.
<?php
if (isset($_POST['submit'])) {
$to = $_POST['email'];
$subject = $_POST['name'];
$message = getRequestURI();
$from = "zenphoto#example.com";
$headers = "From:" . $from;
if (mail($to, $subject, $message, $headers)) {
echo "Mail Sent.";
}
else {
echo "failed";
}
}
?>
What is the code to send an email in PHP?
EDIT (#1)
If I understand correctly, you wish to have everything in one page and execute it from the same page.
You can use the following code to send mail from a single page, for example index.php or contact.php
The only difference between this one and my original answer is the <form action="" method="post"> where the action has been left blank.
It is better to use header('Location: thank_you.php'); instead of echo in the PHP handler to redirect the user to another page afterwards.
Copy the entire code below into one file.
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$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 " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Original answer
I wasn't quite sure as to what the question was, but am under the impression that a copy of the message is to be sent to the person who filled in the form.
Here is a tested/working copy of an HTML form and PHP handler. This uses the PHP mail() function.
The PHP handler will also send a copy of the message to the person who filled in the form.
You can use two forward slashes // in front of a line of code if you're not going to use it.
For example: // $subject2 = "Copy of your form submission"; will not execute.
HTML FORM:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP handler (mail_handler.php)
(Uses info from HTML form and sends the Email)
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$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 " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
To send as HTML:
If you wish to send mail as HTML and for both instances, then you will need to create two separate sets of HTML headers with different variable names.
Read the manual on mail() to learn how to send emails as HTML:
http://php.net/manual/en/function.mail.php
Footnotes:
In regards to HTML5
You have to specify the URL of the service that will handle the submitted data, using the action attribute.
As outlined at https://www.w3.org/TR/html5/forms.html under 4.10.1.3 Configuring a form to communicate with a server. For complete information, consult the page.
Therefore, action="" will not work in HTML5.
The proper syntax would be:
action="handler.xxx" or
action="http://www.example.com/handler.xxx".
Note that xxx will be the extension of the type of file used to handle the process. This could be a .php, .cgi, .pl, .jsp file extension etc.
Consult the following Q&A on Stack if sending mail fails:
PHP mail form doesn't complete sending e-mail
PHP script to connect to a SMTP server and send email on Windows 7
Sending an email from PHP in Windows is a bit of a minefield with gotchas and head scratching. I'll try to walk you through one instance where I got it to work on Windows 7 and PHP 5.2.3 under (IIS) Internet Information Services webserver.
I'm assuming you don't want to use any pre-built framework like CodeIgniter or Symfony which contains email sending capability. We'll be sending an email from a standalone PHP file. I acquired this code from under the codeigniter hood (under system/libraries) and modified it so you can just drop in this Email.php file and it should just work.
This should work with newer versions of PHP. But you never know.
Step 1, You need a username/password with an SMTP server:
I'm using the smtp server from smtp.ihostexchange.net which is already created and setup for me. If you don't have this you can't proceed. You should be able to use an email client like thunderbird, evolution, Microsoft Outlook, to specify your smtp server and then be able to send emails through there.
Step 2, Create your Hello World Email file:
I'm assuming you are using IIS. So create a file called index.php under C:\inetpub\wwwroot and put this code in there:
<?php
include("Email.php");
$c = new CI_Email();
$c->from("FromUserName#foobar.com");
$c->to("user_to_receive_email#gmail.com");
$c->subject("Celestial Temple");
$c->message("Dominion reinforcements on the way.");
$c->send();
echo "done";
?>
You should be able to visit this index.php by navigating to localhost/index.php in a browser, it will spew errors because Email.php is missing. But make sure you can at least run it from the browser.
Step 3, Create a file called Email.php:
Create a new file called Email.php under C:\inetpub\wwwroot.
Copy/paste this PHP code into Email.php:
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
Since there are many kinds of smtp servers, you will have to manually fiddle with the settings at the top of Email.php. I've set it up so it automatically works with smtp.ihostexchange.net, but your smtp server might be different.
For example:
Set the smtp_port setting to the port of your smtp server.
Set the smtp_crypto setting to what your smtp server needs.
Set the $newline and $crlf so it's compatible with what your smtp server uses. If you pick wrong, the smtp server may ignore your request without error. I use \r\n, for you maybe \n is required.
The linked code is too long to paste as a stackoverflow answer, If you want to edit it, leave a comment in here or through github and I'll change it.
Step 4, make sure your php.ini has ssl extension enabled:
Find your PHP.ini file and uncomment the
;extension=php_openssl.dll
So it looks like:
extension=php_openssl.dll
Step 5, Run the index.php file you just made in a browser:
You should get the following output:
220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at
Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0
<534edd7c92761#summitbroadband.com> Queued mail for delivery
lang:email_sent
done
Step 6, check your email, and spam folder:
Visit the email account for user_to_receive_email#gmail.com and you should have received an email. It should arrive within 5 or 10 seconds. If you does not, inspect the errors returned on the page. If that doesn't work, try mashing your face on the keyboard on google while chanting: "working at the grocery store isn't so bad."
If you haven't already, look at your php.ini and make sure the parameters under the [mail function] setting are set correctly to activate the email service. After you can use PHPMailer library and follow the instructions.
You can also use mandrill app to send the mail in php. You will get the API from https://mandrillapp.com/api/docs/index.php.html where you can find the complete details about emails sended and other details.
You need to add an action into your form like:
<form name='form1' method='post' action='<?php echo($_SERVER['PHP_SELF']);'>
<!-- All your input for the form here -->
</form>
Then put your snippet at the top of the document en send the mail. What echo($_SERVER['PHP_SELF']); does is that it sends your information to the top of your script so you could use it.
You need a SMPT Server in order for
... mail($to,$subject,$message,$headers);
to work.
You could try light weight SMTP servers like xmailer
Here are the PHP mail settings I use:
//Mail sending function
$subject = $_POST['name'];
$to = $_POST['email'];
$from = "zenphoto#example.com";
//data
$msg = "Your MSG <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 "Mail Sent.";
I think one error in the original code might have been that it had:
$message = echo getRequestURI();
instead of:
$message = getRequestURI();
(The code has since been edited though.)

I can't send mail using PHP mail function... I am getting an error

I am unable to send mail using the PHP mail() function. I am getting an error message. What's wrong in it? Please, help me...
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in E:\xampp\htdocs\SimpleEmail.php on line 14
Email could not be sent.
<html>
<head>
<title>Simple Send Mail Form</title>
</head>
<body>
<h1>Mail Form</h1>
<form name="form1" method="post" action="SimpleEmail.php">
<table>
<tr><td><b>To</b></td><td><input type="text" name="mailto" size="35"></td></tr>
<tr><td><b>Subject</b></td>
<td><input type="text" name="mailsubject" size="35"></td></tr>
<tr><td><b>Message</b></td>
<td><textarea name="mailbody" cols="50" rows="7"></textarea></td>
</tr>
<tr><td colspan="2">
<input type="submit" name="Submit" value="Send">
</td>
</tr>
</table>
</form>
</body>
</html>
//SimpleEmail.php//
<?php
if (empty ($mailto) ) {
die ( "Recipient is blank! ") ;
}
if (empty ($mailsubject) ){
$mailsubject=" " ;
}
if (empty ($mailbody) ) {
$mailbody=" " ;
}
$result = mail ($mailto, $mailsubject, $mailbody) ;
if ($result) {
echo "Email sent successfully!" ;
}else{
echo "Email could not be sent." ;
}
?>
You aren't running your own mail server and aren't configured to use anyone else's. So where would the mail go? Talk to the system administrator or hosting provider.
the error message mean you need to install SMTP server to let the mail function work normally .
since you using xampp you may configure Mercury Server which comes with XAMPP ..
once you install it and configure it the mail function will work normally ..
Dont worry...in our local servers many of them doesnot run based on SMTP (Simple Mail Transfor Protocol) so that we con't send the mails but some servers may run on that and if you configured into SMTP protocol port then you can also send them
You can try to test your PHP mail script in your localhost by follow given steps in this tutorial.
http://blogs.bigfish.tv/adam/2009/12/03/setup-a-testing-mail-server-using-php-on-mac-os-x/
May it will help you.
Use following code to send an email
ini_set("SMTP","aspmx.l.google.com");
$to = "yourmailid#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "toemail#gmail.com";
$headers = 'From: '.$from. "\r\n" .
'Reply-To: '.$from. "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to,$subject,$message,$headers)) echo "Mail Sent.";

PHP mail function not working

I have a php script that sends an email. It looks like this:
<?php
// subject
$subject = "$first_name $last_name has sent you a message on The Red-line";
// message
$message = "<html>
<head>
<title>
The Red-line
</title>
</head>
<body>
<p>
Hi $war_first,
</p> <br />
<p>
$first_name $last_name has sent you a message on the Red-line. To view your message, please login to <a href='www.thered-line.com'>the Red-line.</a>
</p> <br />
<p>
Sincerely,
</p> <br />
<p>
The Red-line Operator
</p>
</body>
</html>";
// 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 .= "From: The Red-line messages#theredline.com \r\n";
$headers .= "To: $war_first $last_war <$war_mail>\r\n";
// Mail it
mail($war_mail, $subject, $message, $headers);
?>
When I was testing this out on the remote server, I used this script to send an email to my inbox at turbocapitalist1987#yahoo.com. I got the email but the "from" part doesn't work . Yahoo says that my email was sent from "the#yahoo.com"
Does anybody have a clue as to why this isn't working?
Thanks
For your From header, enclose the actual address with < >:
$headers .= "From: The Red-line <messages#theredline.com> \r\n";
This is the correct format to use when you have both a display name and an email address. I'm guessing Yahoo was interpreting the first word "The" as the entire email address, and provided a default (yahoo.com) for the domain.
The <> seems to be your problem, it should be:
$headers .= "From: The Red-line <messages#theredline.com> \r\n";
Secondly, on Unix/Linux, you must have a working MTA (i.e.: sendmail, postfix, etc.) configured on your server, either to relay mail directly, or through a Smart Host. If you're on Windows, you must have a SMTP server configured in php.ini.
I would recommend using pre-built PHP email library such as: http://swiftmailer.org/

php simple contact form not sending mail even after confirmation

I have been trying to get a php contact form working on my portfolio site (currently on a free megabyet.net account), but on testing it(on the uploaded site) even though i get the thankyou/confirmation message, I still don't receive any message on my mail account (specified in the code), I can't seem to understand the problem here....help needed!
can it be something related to SMTP??
Here's the code :
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Mail has been sent, thankyou!";
mail($to, $subject, $body, $headers);
} else {
echo "blarg!";
}
?>
HTML Code:
<form id="contact_frm" action="mail.php" method="POST">
<h4>Name :</h4>
<input type="text" id="f_name" name="name"/><br/><br/>
<h4>E-Mail Address :</h4>
<input type="text" id="f_email" name="email"/><br/><br/>
<h4>Message :</h4>
<textarea id="f_msg" name="message" cols="22" rows="5"/></textarea><br/><br/>
<input id="send_btn" type="submit" value="Send >>" name="submit" /><br/>
</form>
Firstly you should be checking if mail() returns true or not to determine if mail has been sent successfully:
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if ($success) {
echo "Mail has been sent, thankyou!";
// redirect to thank you page here
}
else {
echo "message failed";
}
} else {
echo "blarg!";
}
?>
Try that and let us know if that works.
Also, have you tried sending to a different email address? It may be that Yahoo is blocking that web host for spam. Being a free host it is a very likely scenario.
If you are looking for something related to sending email via SMTP. I would recommend you use Code Igniters mailer class.
http://codeigniter.com/user_guide/libraries/email.html
This also allows for debugging and handling SMTP errors gracefully.
can it be something related to SMTP??
Probably. Why don't you check your mailq and the log files from your MTA?
#John .. checked with that if condition with the code below and i get a failed output =/ ...so my mail() function is returning false =( ...and yea i've tried gmail but with the mail function not running fine on the first place.... it doesn't work...
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if($success) {
echo "Mail has been sent, thankyou!";
} else {
echo "message sending failed!";
}
} else {
echo "blarg!";
}
?>
output- message sending failed!
so, do I need to define some extra params here?...also i saw that my host has given the path to sendmail as -- /usr/sbin/sendmail does it has anything to do with my mail function acting bad?...i mean do I need to define the sendmail param in it?
#unknown- hmm codeigniter may help, but i've never used it before...let's see...
#symcbean- sorry i don't know how to do that :P...probabaly cuz i'm not very well versed with SMTP yet?.... still a learner/beginner...
If the E-Mail goes out correctly, but never arrives, it could be that it gets caught by a spam filter. A few bullet points I wrote in reply to an similar question a few months ago:
Does the sender address ("From") belong to a domain on your server? If not, make it so.
Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
If you have access to log files, check those, of course, as suggested above.
Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.

Categories