can't send mail in php using mail function [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I was trying to send mail by php, the function mail returns 1, but i didn't get
the mail in my inbox
if (isset($_POST["firstName"]) == false)
return;
$to = "mymail#mail.com";
$subject = $_POST["subject"];
$message = $_POST["content"];
$headers = 'From: ' . $_POST["email"] . "\r\n" .
'Reply-To: ' . $_POST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$from = "From: " . $_POST["firstName"] . " " . $_POST["lastName"] . " <" . $_POST["email"] . ">";
echo mail($to, $subject, $message, $headers);
echo '<script type="text/javascript">alert("sent message succesfully");</script>';

Using PHP's mail() function it's possible. Remember mail function will not work in Local server.
<?php
$to = 'abc#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: admin#example.com' . "\r\n" .
"CC: somebodyelse#example.com";
mail($to, $subject, $message, $headers);
?>
Reference:
http://php.net/manual/en/function.mail.php

Related

What is wrong with my PHP Send Mail Function below?

What is wrong with my PHP Send Mail function? I am hosting this script on a live server.
$to = 'myemail#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: exampleemail#gmail.com' . "\r\n" .
'Reply-To: exampleemail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){}
else{
echo "Email sending failed";
}
I keep receiving the "Email sending Failed" message.

php mail and yahoo.com not vibing

Ok so Ive recently developed a php contact form for party inquiries.
The form sends one request to the website and one confirmation to the person who fills out the form.
In gmail it seems that both emails are being sent properly. However, if you fill out the form with a yahoo email it will send only the confirmation. Not the request. This is obviously problematic. So I am trying to figure out why.
My php is as follows:
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$guests = $_POST['guests'];
$type = $_POST['type'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$message = $_POST['message'];
$formcontent="msg";
$comfirmcontent="msg";
$fromname="mydomain.com";
$fromemail="info#mydomain.com";
$confirmheader=
'From: "' . $fromname . '" <' . $fromemail . '>' . "\r\n" .
'Reply-To: "' . $fromname . '" <' . $fromemail . '>' . "\r\n" .
'X-Mailer: PHP v' . phpversion();
$recipient = "info#mydomain.com";
$subject = "Contact From Website";
$mailheader = "From: $email";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, $confirmsubject, $comfirmcontent, $confirmheader) or die("Error!");
header('Location: party-form-thank-you.html')
So this email isnt sending from yahoo:
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
But this one is:
mail($email, $confirmsubject, $comfirmcontent, $confirmheader) or die("Error!");
I thought it may be because the
'X-Mailer: PHP v' . phpversion();
was defined in the confirmation email so I tried writing this into the $mailheader
$mailheader = "From: $email" . "\r\n" .
'X-Mailer: PHP v' . phpversion();
...Didnt work.
So I am asking you all my coding friends for help as to why.
TIA
#kaiqing
You were correct, since the variables were defined as the same with different name I think yahoo was having a problem,
I rewrote it as this:
$name = $_POST['name'];
$email = $_POST['email'];
$fromname="BOULETTESLARDER.COM";
$recipient = "info#bouletteslarder.com";
$subject = "Contact From Website";
$mailheader = 'From: "' . $name . '" <' . $email . '>' . "\r\n" .
'X-Mailer: PHP v' . phpversion();
$confirmheader=
'From: "' . $fromname . '" <' . $recipient . '>' . "\r\n" .
'Reply-To: "' . $fromname . '" <' . $recipient . '>' . "\r\n" .
'X-Mailer: PHP v' . phpversion();
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, $confirmsubject, $comfirmcontent, $confirmheader) or die("Error!");
Now it will submit and send both emails — to the website & to the user — if a user filling out the form has a yahoo address.

mail() function doesn't not send infomation to my email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Here is my code:
<?php
header('Content-type: text/plain; charset=utf-8');
$to = 'example#gmail.com';
$subject = 'Message to the site';
$realname = htmlspecialchars($_POST['realname']);
$email = htmlspecialchars($_POST['email']);
$msg = htmlspecialchars($_POST['msg']);
$headers = 'From: example#gmail.com' . "\r\n" .
'Reply-To: example#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $msg, $headers) or die("Error!");
echo 'Thank you! :)';
?>
Could somebody explaing me - why this code does not work properly?
I meant that, when I click button submit, it send the email, but I could not include $msg in it and I don't know why.
Try to add :
$headers .= 'Content-type: text/html';
Add this line under $headers and than check i hope this will work for you...

HTML Form/PHP and MySQL email notification

complete beginner at PHP and was wanting a little direction for a website I am creating. I want the admin of the website to receive an email with all of the form information aswell as it being stored in the database. The database is storing the information fine, just need an email notification. How is this achieved. My PHP code is:
<?php
session_start();
include('connection.php');
$product = $_POST['product'];
$productcomments = $_POST['productcomments'];
$name = $_POST['name'];
$address = $_POST['address'];
$age = $_POST['age'];
$delivery = $_POST['delivery'];
mysql_query("INSERT INTO orderform(product, productcomments, name, address, age, delivery)VALUES('$product', '$productcomments', '$name','$address', '$age', '$delivery')");
header("location: google.com");
$to = 'j_bussey#live.co.uk';
$subject = 'Order';
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mysql_close($con);
?>
this is the very basic example of mail function. read more about mail() manual here.
$email = $_POST['email'];
$subject = "Email Subject";
$message = "Email Message Body";
mail($email, $subject, $message, "from: admin#yourdomain.com");
PHP has an awesome mail() function. I'm taking this from the documentation page here: http://us2.php.net/manual/en/function.mail.php
<?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);
?>
So since you're already grabbing the variables, you would just edit the $message variable in the code above to look something like this:
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
etc, etc. If you don't want to assume that they have html emails enabled, you would use \n instead of <br />
Edit:
You also need to change your header('Location: google.com'); to header('Location: http://www.google.com'); or wherever you want to redirect after the email has been sent off.

issue in sending email with custom headers

I'm having some issues in understanding how to send custom headers with the php mail() function.
What's wrong with this code?
<?php
function send_email($name, $lastname, $from, $subject, $message){
$to = 'to_adress#gmail.com';
$headers = 'To: ' . $to .
'\r\nFrom: ' . $from .
'\r\nSubject:' . $subject;
mail($to, $subject, $message, $headers);
print 'Email sent';
}
send_email('jhon', 'doe', 'from_adress#gmail.com', 'subject', 'message');
?>
It gives me no php error, and the email does not arrive.
'\r\n' is not the same as "\r\n". Try something like this instead:
$headers = 'To: ' . $to . "\r\n"
. 'From: ' . $from . "\r\n"
. 'Subject: ' . $subject;
However you don't need to repeat the To: and Subject: headers, so just using this should do:
$headers = 'From: ' . $from;

Categories