I'm having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it works there.
I think it has to do with the web-hosting company itself. Do any of you know what I need to do in order to make it work? Is there something I need to tell them to install? I think they're running on Apache.
Thanks,
Amit
For clarification purposes, here is the mail-script.
<?php
$to = 'my#email.com';
$subject = 'Contact from your website';
$message =
'Below are details from the Contact Us Form ' . "\n\n" .
'Name: ' . $_REQUEST['name'] . "\n\n" .
'Telephone Number: ' . $_REQUEST['phone'] . "\n\n" .
'E-mail address: ' . $_REQUEST['email'] . "\n\n" .
'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
//SPAM CHECK
$str = $_REQUEST['spam'];
$strE = $_REQUEST['email'];
if( $str != "10" || $strE == "")
{
echo "<div align='center' style='color:red'>One or more of the form fields were incorrect, you will be redirected to the contact page within 3 seconds.</div>";
?><meta http-equiv="refresh" content="3;URL=http://engineercreativity.com/samples/biz/contact"><!-- EDIT THIS -->
<?php
} else {
mail ($to, $subject, $message, $headers);
?>
<meta http-equiv="refresh" content="0;URL=http://engineercreativity.com/thankyou.html"> <!-- EDIT THIS AS WELL -->
<!--
<div class="text" align="center" style="text-align: center; color: green;">
<br/>
Thank you for contacting us!
<br/>
The message was succesfully sent!
</div>
-->
<?php
}
?>
If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)
I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)
Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.
Posting full headers also can help legitimate messages pass spam tests.
$headers = "Return-path: <sendingemail#test.com>\n";
$headers .= "Reply-to: <sendingemail#test.com>"."\n";
$headers .= "Content-Type: text/html; charset=windows-1252\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: <sendingemail#test.com>\n";
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Organization: My Organization\r\n";
$headers .= "\n\n";
Write a really simple script, like
<?php
mail('your_mail#example.com', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';
Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25 is sufficient).
If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?
Whatever the solution, check your mail()'s output.
Most common solution
Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).
Alternative
The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).
mail() function of php, will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user#serverhostname.tld and not someone#example.com like you had told the mail function to do.
If you use SMTP and view the original the email is actually sent from someone#example.com
You can download SMTP class from:
https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html
Related
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 6 years ago.
please refer the following code.in there am sending username and password.email has sent to the gmail.but it is in spam folder.it happens only in gmail.
Here is my code
$to = $email;
$subject = ' Web Site| login Details'; // Give the email a subject
$message = '
Thanks for signing up!
Your account has been created, you can login with the following credentials.
------------------------
Username: '.$uname.'
Password: '.$upass.'
Web Builder login: '.$ulink.'
------------------------ ';
$headers .= 'From:noreply#ggg.eee.net' . "\r\n";
$headers .= 'Bcc: kae#ggg.eee.net' ."\r\n";
$headers .= 'Bcc: thi#ggg.eee.net' ."\r\n";
mail($to, $subject, $message, $headers);
There are many reasons why email providers mark your messages as spam. Sometimes it has to do with the way your server and DNS are configured or other times it may be the content of your message. I would recommend using this tool to help pinpoint the weak spots of your email messages to help you get through most spam filters
Mail-Tester.com
Use this code may be it work properly
$to = Email;
$subject = ' Web Site| login Details'; // Give the email a subject
$message = '
Thanks for signing up!
Your account has been created, you can login with the following credentials.
------------------------
Username: '.$uname.'
Password: '.$upass.'
Web Builder login: '.$ulink.'
------------------------ ';
$headers = 'From: noreply#ggg.eee.net' . "\r\n" ;
$headers .='Reply-To: '. $to . "\r\n" ;
$headers .= 'Bcc: kae#ggg.eee.net' ."\r\n";
$headers .= 'Bcc: thi#ggg.eee.net' ."\r\n";
$headers .='X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
if(mail($to, $subject, $message ,$headers)) {
echo('<br>'."Email Sent ;D ".'</br>');
}
else
{
echo("<p>Email Message delivery failed...</p>");
}
I have found the soution of this problem:
Solution-1:
Use double quotes to headers.
Solution-2:
The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.
Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are gone.
https://github.com/PHPMailer/PHPMailer
Source of solution
Mail function now not working my server. i cannot find out the solution for that. i am getting bellow this error when i submitting the contact form.
Warning: mail() [function.mail]: SMTP server response: 550 Sender is not allowed. in E:\HostingSpace\abayamtranslations.com\httpdocs\contact-form\classes\contact.php on line 137
ERROR! Please ensure PHP Mail() is correctly configured on this server.
$address = "info#abayamtranslations.com";
ini_set("SMTP","mail.abayamtranslations.com");
ini_set("smtp_port","25");
ini_set('sendmail_from','info#abayamtranslations.com');
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
Try with the following
$emailFrom = "info#abayamtranslations.com"; // match this to the domain you are sending email from
$email = "example#example.com";
$subject = "Subject of email";
$headers = 'From:' . $emailFrom . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Return-path: " . $email;
$message = "Message of email";
mail($email, $subject, $message, $headers);
"550 Sender is not allowed" means that the SMTP Blocked Senders list rejected the sender. Search your SMTP log for 550 Sender is not allowed and figure out what address is getting blocked and then alter your list to let that through.
That is the meaning of the error you are getting. This is from the hMailServer Documentation.
Can you try if the following will work?
<?php
mail('info#abayamtranslations.com','Test Email','This is a test email.',"From: info#abayamtranslations.com");
?>
If it doesn't work, then it's probably due to a misconfiguration in your hMailServer and you would need to check your hMailServer Logs.
You are on the windows server, hence forth you have to change SMTP of windows server.
Better way is to use PHPMailer Library. https://github.com/PHPMailer/PHPMailer
Some important information is missing under your SMTP configuration....
username and password is needed to authenticate with the SMTP mail server.....
it easy ti use smtp mailing in php with:
PHPMailer (https://github.com/Synchro/PHPMailerhttps://github.com/Synchro/PHPMailer)
have a look into it....
Thankyou....
I am working on a WordPress plugin in which I send email using the PHP mail function, but there is a problem with the mail function. It sends emails to my non-Gmail account, but it's doesn't send emails to my Gmail account. I am using following code:
function send_mail()
{
global $wpdb;
$to = 'mymail#gmail.com';
$subject = 'Hello';
$name='my name';
$from="name#mydomain.com";
$message = "
<html>
<head>
<title>my title</title>
</head>
<body>
<div>
<tt> ".Hii How Are you."</tt>
</div>
</body>
</html>";
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1\r\n";
$header .= "From: ".$name."<".$from.">\r\n";
mail($to, $subject, $message, $header);
}
Is there something wrong with my code, or is there some issue with the mail function? If any alternate method is available to send email, please give me the link.
Check if adding fifth variable works for you... here is my code for sending emails.
if( mail( $recipient, $subject, $message, $headers, "-f noreply#mydomain.com"))
return "success";
Check the spam folder, It might be there. Its a server issue, it hapened to me also many times. Gmail blocks mails or sends to spam from some servers due to some reasons. Ask your server provider to check why mails are not going to gmail inbox.
use the code sample as below, email address in <> and that last paramter worked for me.
$headers = 'From: <test#test.com>' . "\r\n" .
'Reply-To: <test#test.com>';
mail('<myEmail#gmail.com>', 'the subject', 'the message', $headers,
'-fwebmaster#example.com');
?>
It is the answer provided in PHP mail() function will not send to gmail but will send to my non-gmail account by ARH3, which i have tried and tested
I've researched this intensely. Here, at Stack Overflow, I've figured out that one needs to use an -f parameter with the php mail() function, if one wants undeliverable mail to bounce back. Following is my script (as it stands now):
//Send Confirmation email. Following are the variables for the email
// mail function best practices: http://collaborate.extension.org/wiki/Best_Practices_Using_the_PHP_mail_Function
$sendto = $email; // this is the email address collected from the foreach routine.
$e_subject = stripslashes($subject); // Subject
//$message = "<html>" . stripslashes($body) . "</html>";
$message = "
<html>
<body>
<p>Hello " . stripslashes($fName) . ":</p>
<div>" . stripslashes($body) . "</div>
</body>
</html>
";
// Always set content-type when sending HTML email
$header = "MIME-Version: 1.0" . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
// extract user domain so you can set up X-Mailer
$u_domain=substr(strrchr($user_email, '#'), 1);
$dom_array = explode(".",$u_domain);
$user_domain = $dom_array[0];
$header .= "X-Mailer: ". $user_domain ."\r\n";
$header .= "X-Sender-IP: {$_SERVER['REMOTE_ADDR']}\r\n";
$header .= "X-Originating-IP: [".getenv("REMOTE_ADDR")."]\r\n";
$header .= "From: " . $user_email . "\r\n";
$header .= "Sender: ". $user_email . "\r\n";
// The "envelope sender" is the address listed in the "Return-Path:" header - and controls where the email is sent to in the event that a recipient address bounces. http://collaborate.extension.org/wiki/Best_Practices_Using_the_PHP_mail_Function
$header .= "Return-Path:" . $user_email . "\r\n";
$header .= "Reply-To:" . $user_email . "\r\n";
$bounceTo = "-f". $user_email;
// Collect variables from above and insert into the mail() function.
mail($sendto, $e_subject, $message, $header,$bounceTo);
You'll notice a lot of commenting - I'm just trying to figure this out. My mail() sends wonderfully. The mail is coming into my inbox with formatting as it should be. But... the $bounceTo variable ("-f" . $user_email) is not working. I've intentionally mailed to 3 known inactive addresses, and I'm not getting any bounce backs.
All the header settings in the above code are in place because I've learned that these may affect bounce backs. I'm totally willing to get rid of un-necessary headers and add what is necessary. But... at this point the script seems to be a mess -which is not producing bounce backs.
Any suggestions are welcome.
Thanks Much:
Pavilion
Have you looked at the comment posted at http://www.php.net/manual/en/function.mail.php#107321. That might lead you in the right direction.
This thread explaining bounced mail in php might be of benefit to you. I personally have never had to use the -f parameter to handle bounced emails.
Overriding the bounce address with -f is not allowed on all servers, especially if you are on a shared hosting server this is often not possible. In this case, it's often better not to use the very limited mail() function but use a smtp library like phpmailer or swiftmailer instead.
Btw: you don't have to send mails to an inactive address to check your bounce address. Send them to an active account, in the message source look for the "Return-Path" header, this is the bounce address.
I'm using the following to send registration e-mails:
$subject = 'subject is here';
$message_raw = 'e-mail text';
$message = base64_encode($message_raw);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64' . "\r\n";
$headers .= 'From: papa.sk <register#papa.sk>' . "\r\n";
$sendit = mail($to, $subject, $message, $headers);
For some people the e-mails are put into the spam folder (in gmail too).
In /etc/postfix/main.cf I have this:
myorigin = /etc/mailname
smtpd_banner = papa1.vps.websupport.sk ESMTP
Not sure whether I should change the above.
you may need a reverse dns record for your server.
many mail servers considers that mails sent from a host are spam if the hostname cannot be looked up. that is nslookup papa.sk should return an ip address, and nslookup <ip address> should return papa.sk.
The php mail() function has nothing to-do with your emails being marked as spam.
That an email is being marked as spam happens on the other end. You can not influence the process much with mail() - as it's the other end.
There can be thousand of reasons why an email is being marked as spam, and as long as you don't know the concrete reason why your email is being marked as spam, you can do nothing against that.
There is a whole industry which makes a living of that btw.