Sending Email in php, headers problems - php

I trying to send email using mail() function...
here is my code:
<html>
<form action ="" method ="post">
<input type="submit" name= "email" value="email">
<form>
<?php
if (isset($_POST['email']))
{
mail("receiver#hotmail.com", "Subject: Hi", "hello" );
echo "Mail Sent";
}
?>
</html>
the code about works just fine, I can get the email but the only problem was when I check the email, the sender will be "webmaster#something.org"
I tried to change the code to:
mail("receiver#hotmail.com", "Subject: Hi","hello", "From: sender#yahoo.com" );
but it didn't work...
Could you please help me to include the name of the person who sent the email...
Thank a lot

Please try this.It is the simplest method to mail someone.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Check php mail manual.
You need to define 'From: xxx' in the header

check your form tag closed or not?
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Use [phpmailer to send the mail.
You can set the sender id and content type for the mail.

Related

How to send a simple email using PHP code

I would like to send an email to my gmail account using some simple PHP code. The code below works in terms of execution, however the problem is even thought is says "Message Sent" I am not receiving my email in my gmail account. Please advice
ini_set('SMTP',"smtp.gmail.com");
$to ="example#gmail.com"; // this will be replaced with my actual email
$from ="example#gmail.com"; // this will be replaced with senders email
$message = $_GET['Message'];
$subject = "This is a test";
if(mail($to,$subject,$message,$from))
{
echo "Message Sent";
}
else
{
echo "Message Not Sent";
}
Steps to send a simple email
Go to google
Search for "PHP Mail"
Click the first result
Read, read, keep reading, wait, read it over, read on
Enjoy!
But seriously:
(Examples are taken from PHP.net)
Example 1
Sending a simple email
Using mail() to send a simple email:
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('caffeinated#example.com', 'My Subject', $message);
?>
Example 2
Sending mail with extra headers.
The addition of basic headers, telling the MUA the From and Reply-To addresses:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Use his line of code:
$to ="example#gmail.com"; // this will be replaced with my actual email
$from ="example#gmail.com"; // this will be replaced with senders email
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
//$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = $_GET['Message'];
$subject = "This is a test";
mail($to, $subject, $message, $headers);

Send an email with PHP [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to send an email when the 'Send Email' button is pressed, but the following code won't send anything to my email:
<!DOCTYPE html>
<html>
<form action="email.php" method="post">
<input value="Send Email" name="email" type="submit">
</form>
<?php
if (isset($_POST['email'])) {
$msg = 'hello';
mail('example#gmail.com', 'Sample', $msg);
}
?>
</html>
Any idea on how to make it work?
Use the following PHP code to send the email
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Mail function will not work in Local server.You need to config SMTP on your local server. Take a look at this similar post,
php mail setup in xampp
try this
<?php
$to = "someemail.com"
$subject = "This is subject";
$message = "<b>This is message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#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...";
}
?>

not able to send email from PHP page

I want to send email from contact page of the my site. I google and it told to use PHP scripts as my domain is LINUX based so I cannot use ASP. I tried couple of them but not able to send. How ever I have used #ECHO to display message, which I got but not the email. Here the codes that I tried:
<?php
$userid='to.useri#example.com';
$subject='New Requirement';
$Name=$_POST['Name'];
$Email=$_POST['Email'];
$Phone=$_POST['Phone'];
$Message=$_POST['Message'];
$body= <<<EOD;
<br><hr><br>
Name: $Name <br>
Email: $Email <br>
Phone: $From <br>
Message: $Message
EOD;
$headers='From: $Email';
mail($userid,$subject,$body,$headers);
echo "Message send!!!";
?>
And I also tried :
<?php
$to = 'to.user#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from.user#example.com' . "\r\n" .
'Reply-To: from.user#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers )) {
ECHO 'Message send successfully';
}
else {
ECHO 'Please try again, Message could not be sent!';
}
?>
Can any one tell me what am I missing here.
Change your if Condition with the below mentioned code, hope it works:-
$mail=mail($to, "Subject: $subject",$message );
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
try this code
$senderName="John";
$senderEmail= "test#domain.com";
$recipient = "recipient#domain.com";
$subject ="testmail";
$message="test message";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail($recipient, $subject, $message, $headers );

MAIL NOT SHOWING UP

<?php
$to = 'something#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: example#test.com' . "\r\n" .
'Reply-To: example#test.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$send = mail($to, $subject, $message, $headers );
if ($send)
$mailReturns = "Mail sent successfully.";
else
$mailReturns = "Mail sent failed.";
?>
<?php echo $mailReturns; ?>
after run it shows mail sent successfully
but mail not showing up in any of the accounts. iv tried gmail, yahoo, rediff and ibibo why????
If you have qmail installed on your server, try using that. Alternatively, there is a nice PHP mailer class called swiftmail that is available here: http://swiftmailer.org/ you can try.
Your emails are probably being junked/marked as spam.
Try to see in spam folder.

php mail function

<?php
$sendto = "account#gmail.com";
$subject = "email confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
# send the email
mail($sendto, $subject, $message);
?>
this is the code that i wrote to test mail function on localhost.
i have ran the script in browser for several times and still dun receive any email in my mail box.
Do I need any additional configurations?
thx in advance!
Basically is hard to send a mail from localhost to any mail providers.
They have big restrictions on the incoming mails, and the simply mail() won't work.
You need to use an SMTP server.
and define that server in php configuration
smtp = localhost #(here should be your smtp server)
smtp_port = 25
if you don't have an SMTP server, try to pass all headers like in PHP examples:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
http://www.php.net/manual/en/function.mail.php
You need to make sure that you have your PHP installation set up to use a working SMTP Server. You might find what you're looking for in answers to this question. Failing that you'll likely need to test your script on your live web server.
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
?>
<?php
$email_from = 'yourname#yourwebsite.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message"
?>
<?php
$to = "inspiretechpark#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
<?php
$to = "name1#website-name.com, name2#website-name.com,name3#website-
name.com";
mail($to,$email_subject,$email_body,$headers);
?>
<?php
$to = "name1#website-name.com, name2#website-name.com,name3#website-
name.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$headers .= "Cc: someone#domain.com \r\n";
$headers .= "Bcc: someoneelse#domain.com \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
Try This Guys..This Is For Sending Mail
Try this:
<?php
$sender = 'email#example.com';
$recipient = 'email#example.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>
If you are working with localhost then, i hope it will never work. It will work only on a mail configured server. Please try on it.

Categories