I have the following code in a PHP file.
$to = $row['EmailID'];
$subject = "XYZ";
$message = "yes!!!!";
$from = "pallav123goyal#gmail.com";
if(!mail($to,$subject,$message,"From:" . $from))
{
echo "Confirmation E-mail couldn't be sent to " . $row['EmailID'] . "<br>";
}
else
{
echo "Confirmation E-mail sent to " . $row['EmailID'] . "<br>";
}
}
On running the code, the else part of the above code is executed as evident from its output. But no e-mail gets sent to $row['EmailID'] (which is a valid email ID) What could be the error?
Try this syntax
<?php
$to =$row['EmailID'];
$subject ="XYZ";
$txt ="yes!!!!";
$headers = "From: pallav123goyal#gmail.com";
mail($to,$subject,$txt,$headers);
?>
It is most likely a problem with your sendmail (assuming you are running the code on a linux system) configuration and not with the code per se. Try to use PEAR mail and specify an external SMTP server (e.g. gmail).
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have a problem with sending the form to my e-mail.
Currently, when i send the form i get message from "emailtest#test.com" with $message2 but i don't get $message from 'inputEmail' to my e-mail "emailtest#test.com".
I would add that I am not PHP programmer and this is my first script in this language.
I would be very grateful for your help
<?php
$to = 'emailtest#test.com'; // this is your Email address
$from = $_POST['inputEmail']; // this is the sender's Email address
$first_name = $_POST['inputName'];
$inputCompany = $_POST['inputCompany'];
$inputPosition = $_POST['inputPosition'];
$inputProjects = $_POST['inputProjects'];
$inputOfficeProjects = $_POST['inputOfficeProjects'];
$inputPresentation = $_POST['inputPresentation'];
$inputMessage = $_POST['inputMessage'];
$number = $_POST['number'];
$subject = "Test";
$subject2 = "Test1";
$message = $first_name . " example " . $inputPosition . " 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.";
header('Location: dziekujemy.html');
?>
Since your code works in a local environment, with mailtodisk, I suspect a "problem" with the mail server. You are setting the From: Header of the first email send attempt to the email address of the person that filled out the form. Most mail servers might reject that because it is not a valid address that you own. I am not an expert to this so someone might have a better, more detailed explanation.
Remove the headers from the first email or set the From: Header to a address which can actually sent over that mailserver. If this works you can use a Reply-to header which enables most clients to directly answer to the set email address.
Example below:
...
$headers = 'From: ' . $to . "\r\n" .
'Reply-To: '.$from;
...
I might suggest - even it it looks complicated at first - to use PHPMailer. It makes sending emails with PHP much easier.
I don't know why but I don't manage to get any email after html form is submitted.
I can see the data in the DB but I'm not receiving any Emails.
I put some code after the sending that work just fine. Only the mails are not sending.
Please help me.
The PHP file:
<?php
$Name = $_POST['Name'];
$Phone= $_POST['Phone'];
$Email= $_POST['Email'];
$Subject= $_POST['Subject'];
$Message= $_POST['message'];
$conn = mysql_connect("MyServerName", "userName", "password");
mysql_select_db("Mydbname",$conn);
$sql = "INSERT INTO FC(Name, Phone, Email, Subject, Message)
VALUES ('$Name', '$Phone', '$Email', '$Subject', '$Message')";
if (mysql_query($sql, $conn)) {
$to = "myEmail#test.com"; // this is your Email address
$from = "myEmail#test.com"; // this is the sender's Email address
$headers = "From: " . $Email;
$subject = "Form submission - " . $Subject;
$message = $Name . " wrote the following:" . "\n\n" . $Message . "\n\n" . "His Phone number is: " . $Phone;
mail($to,$subject,$message,$headers);
echo '<html>
<head>
<meta http-equiv="refresh" content="0.1;url=http://www.google.com" />
</head>
<body>';
echo '<script language="javascript">';
echo 'alert("message successfully sent! we will contact you shortly.")';
echo '</script>';
echo '</body></html>';
} else {
echo "something went wrong";
}
?>
Start by checking the return value of mail to see if the message is being accepted by your SMTP server.
Please make sure that mail() is not blocked in your php.ini file.
If your mail function returns FALSE then you would KNOW there's a problem with your mail configuration. If it returns TRUE then there could still be something wrong with your mail configuration. You would need to check the mail log in either case to see if you are getting any errors.
The kicker is that php may be handing off the mail, your server may be sending off the mail, and it may get spam filtered along the way.
You could try explicitly setting the From address using the -f parameter, as explained in example #3 in the php.net manual page for mail:
<?php
mail('nobody#example.com', 'the subject', 'the message', null,
'-fwebmaster#example.com');
?>
So for you, your From address could be invalid based on your hosting specifications but if its not:
$from = "-fmyEmail#test.com";
mail($to,$subject,$message,$headers,$from);
I am trying to send an email through php. But the mail() function keeps returning FALSE even though I am sure everything is correct.
After browsing through stackoverflow for similar problems, one answer stood out by saying that it could also be because of the server.
NOTE: The mail server part of the nginx server isn't configured yet. I have no idea if PHP needs that specific module in order to work.
# $email and $randomPassword already defined.
$subject = "New Password";
$message = "Your new password is ".$randomPassword;
$headers = 'From: contact#mail.com' . "\r\n" .
'Reply-To: contact#email.com';
$checkMail = mail($email, $subject, $message, $headers);
if ($checkMail) {
echo "Mail send";
} else {
echo "Mail not send";
}
How do I send an email to the user with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form.
Here is my current php code. It currently just shows them a message that says there name and that the message has been sent and then sends me an email to my email address.
<?php
if(isset($_POST['submit'])){
$to = "benlevygraphics#gmail.com";
$headers = "From: " . $_POST['email'];
$subject = "Ben, you have been contacted...";
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
echo("<p class=contactformsent>".$_POST['name'].", your message has been sent!</p>");
}
else{
echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
}
}
?>
I am new to php and I have read stuff online and I still don't understand so if you could be clear in your examples or help I would greatly appreciate it very much. Thanks!
Assuming your current code is already working fine, you can do this to send yourself an email together with the recipient:
Set $to to $_POST['email']
Set $headers to "From: {$_POST['email']}\r\nBcc: benlevygraphics#gmail.com"
Adjust $body and $subject to your needs.
Btw, I can't say this often enough; make sure that your page has some form of CSRF protection.
How to properly add CSRF token using PHP
The above is just one way, there are others, just search for it :)
Look into your php.ini beacuse you have to enter a SMTP Server.
At my file it begins in line 1087 with "[mail function]"
I'm creating a 'forgot password' page where the user enters their email address and the script finds the password associated to that email, and sends it to the stored email address.
I believe the problem has to do with my SMTP Mailserver. I am using WAMP which doesn't have one so I downloaded one that was free.
This is the php script I'm using:
$id = checkEmail($email);
$record = readMemberRecord($id);
$password = #mysql_result($record,0,'cred');
if($password){
$email_subject = "Email Request";
$email_body = "Your password is ".$password.".";
$header = "NinjaMan";
mail($email, $email_subject, $email_body,$header);
$msg = "Your password has been sent to ".$email.".";
}else{
$msg = "Sorry, the email ".$email." wasn't found.";
}
The $msg outputs properly so I know the code is passing the mail function.
Try sending in a proper "From" in $header.
$emailFrom = "admin#yourdomain.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Email Request";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Your password is ".$password.".";
mail($email, $subject, $message, $headers);
See details of the mail() function.
If this doesn't work, try using PHPMailer. You configure it in code, no need to edit php.ini.
I've used it in some projects (v 2.0.4, I see the latest is 5.1) and had no problems.
Try using Google's server to send mails, you can see how to do that here
Try using this
//Email information
$to = "garggarima#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "support#sltechsoft.com";
$headers = "From:" . $from;
$mail=mail($to,$subject,$message,$headers);
if($mail) {
echo "Thanks for mail";
} else {
echo "Mail not Sent";
}
//Email response
echo "Thank you for contacting us!";