I am trying to get contact form details through mail. But failed to do so using mail() function. here's my code.
if(isset($_POST['rqsubmit'])) {
$name = htmlspecialchars($_POST['Field1']);
$email = trim($_POST['Field2']);
$phone = trim($_POST['Field3']);
$msg = strip_tags($_POST['Field4']);
//echo $name." ".$phone." ".$email." ".$msg;
$to = 'gowtham#gmail.com';
//$from = $email;
$subject = "Software Development";
$message = "Name:".$name."<br/>Phone".$phone."<br/>Message:".$msg;
//echo $message;
$semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//echo $message;
$headers = "From: xyz#gmail.com". "\r\n".
'Reply-To: xyz#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$ok = #mail($to, $subject, wordwrap($message, 70, "\r\n"), $headers);
if ($ok) {
echo "<p>Thank you for contacting us! !!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
} else {
echo "mail not sent";
}
I would like to know where i am going wrong. Any help would b highly appreciated. thank you!
Add:
'X-Mailer: PHP/' . phpversion();
to:
$headers
Perhaps it's being rejected due to the lack of a sender email address.
Put your own email address in the From: header, and add a Reply-To: header containing the sender's address.
Can I just point out that you need to check the form data thoroughly before putting it into an email. For example, I could quite easily send emails all over the place by setting Field2 to something like blah#example.com\nBcc: spam1#spam.com, spam2#spam.com, ....
EDIT:
Sorry, my mistake — you did add a From header. However, it's quite likely that your mail server is configured to reject emails where the sender address is not on its list of approved senders. That's why you need to put your own address in the From: header, and the sender's address in the Reply-To: header.
EDIT 2:
Also, what on earth are you doing with $mime_boundary? As far as I can tell, you're not sending a valid MIME message.
Related
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);
How can I send email notification to my users who gave registration details in my form using php?
I have code which runs perfectly to get emails to me, but now I also want to same details to my users.
I am trying to use "$from" into my array at "$to" but getting no email.
my mail.php
<?php
$subject = "Email Notification";
$message = "Thank you for your email";
$message = "Your Registering details are as follows:";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name</td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email</td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = $_POST['email'];
$to = array('my_address#example.com', 'my_address2#example.com', $from);
$lp = "notification#example.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$lp .'' . "\r\n" .
'Reply-To: '.$lp.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
It's pretty common that on shared hosting you need to send an email from an "existing email", at least match the domain. Maybe that's why the emails are not being sent?
So for example, if your domain is "www.my-awesome-domain.com", you can't send email headers
"from: example#example.com"
Instead, make sure to send emails from your domain, and ideally an existing email box, for example:
"from: office#my-awesome-domain.com"
Hope it helps! :)
I am having a problem sending emails when I add header information.
However when I just remove the header parameter it works. What is wrong? Is it the code? Or some setting I need to change on the web server admin panel to say "Allow-Headers" or something?
I am trying to send to hotmail in case this has any relavance in determining the problem.
Any help would be greatly appreciated. Thanks.
Below Doesn't Send Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message, $headers);
?>
Below Sends Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message);
?>
I use these headers in my php mailing function and it works well. Note: I also use a third party mail-routing service to avoid having my mails marked as coming from a spammy IP. You might want to look into that also.
$headers = 'From: '.$from.'#foo.net' . "\r\n" .
'Reply-To: '.$from.'#foo.net' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
I also use the optional fifth parameter to mail() to set the envelope address, e.g.:
$parameters = '-f '.$from.'#foo.net';
so the final call is:
mail($to, $subject, $message, $headers, $parameters);
You can just delete the "FROM:" from the headers list .. it prevents it in some hosts .But the real question then will be how ca I change the sent from email address to a specific email that I want
I'm using dreamhost for my mailing.
I'm having an issue with php mail function additional headers parameters.
This code works, and the email is sent:
$to = 'myemail#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $name <**webmaster#example.com**>\r\n" .
"Reply-To: $name <**webmaster#example.com**>\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
but when I replace webmaster#example.com to the variable $email
$headers = "From: $name <**$email**>\r\n" .
"Reply-To: $name <**$email**>\r\n" .
'X-Mailer: PHP/' . phpversion();
The email doesn't get sent. I did do a print_r($_POST), and the elements are there. I also did another test where I typed the email: webmaster#example.com into the form, to see if it would send, and it did. So my question is, how do I remedy this issue, if a user types their email address into the form with another mailing extension, that mail will not get sent, but if the extension is #example.com, then the mail will get sent.
$name variable should be the full emailaddress:
"$name = " 'a name' email#whatever.com> "
$headers = "From: $from \r\n";
works on my servers. Add the leading < to the email addtress. This system won't display the string at all if I include it. If your form has different variables for the responders nam and email address you'll have to concatenate them to get $name in the right formate.
The title explains itself. It is a website for in-house employees to buy and sell from each other. Its based solely around Microsoft Outlook emailing addresses. All the emails are supposed to be sent from the seller's email as they post items. Except when I enter <php phpinfo(); ?> on the action php page it tells me that the sendmail_from attribute thing is sending from a bogus email on the server. It seems to be the automatic email for the php script to send from. This may be why the emails are getting sent to spam, because the email is not valid. Also, I read online about having full and valid headers but most headers seem optional and i cant find anywhere that explains optimal headers. My mailing code:
//send approval email to the approver
$from = isset($_POST['from'])? $_POST['from']:1;
$message = isset($_POST['message'])? $_POST['message']:1;
$message = $message . '<a href="http://dev-corkboard/newapproval.php?id='
.$result[0][0].'"> Click here to approve website post.</a>';
// In case any of our lines are larger than 70 characters, we should use
// wordwrap()
$message = wordwrap($message, 70);
$to = 'clehane#eatonvance.com';
$replyto = isset($_POST['replyto'])? $_POST['replyto']:1;
$subject = isset($_POST['subject'])? $_POST['subject']:1;
$headers = "MIME-Version: 1.0" . "\r\n" . 'From: "'.$from.'"' . "\r\n" .
'Reply-To: "'.$replyto.'"' . "\r\n" .
'Content-Type:text/html;charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
//test message for email
}
header ("location: newindex.php"); `
Any ideas?
And bam! Solved it, needed to put email addresses as such:
$from = 'MyName <myemail#mycompany.com>';
And I also included these headers:
"X-Priority: 0\r\n".
"X-MSMail-Priority: Normal\r\n".
"X-Mailer: mycompany.com