can someone please help me figure out why this isn't sending? I'm new to PHP.
It's for a contact form on my website. I'm sending from my server (not localhost) and receiving the console message "Mail not sent"
<?php
include 'ChromePhp.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$to = 'me#myemail.com';
$subject = 'Email from Me';
$message = $_POST['email'];
if(mail($to, $subject, $message)){
ChromePHP::log( 'Mail Sent');
}else{
ChromePHP::log( 'Mail Not Sent');
}
}
?>
I realise this question has been asked before, but I can't find an answer that works. Thanks in advance!
You should include header to mail() function, example
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
then insert $headers parameter to mail() function:
mail($to, $subject, $message, $headers);
Without an error message, I can only guess. You probably need to set the SMPT server in your php.ini. You can do that at runtime using ini_set('SMTP', 'your_server_here').
More about the mail configuration here: http://php.net/manual/en/mail.configuration.php
If you don't have one, you can check if your ISP has an open server. ISPs sometimes have open SMTP servers (outgoing servers) and you can find them by looking for instructions on how to set up your mail program (Outlook).
Be careful though, and don't use this for anything but light testing. I can imagine they won't like a lot of spam going through their server.
EDIT: Also, read up on the headers required. I see you're missing a From header, which should result in a warning. See here: http://php.net/manual/en/function.mail.php
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.
You should check your mail services, because above PHP code quite true.
You can ask to your hosting provider to activate mail services because some hosting provider does not allow to use mail in default.
Related
I've got a form page on my website where the user has to enter his email ID. As soon as he does that an email should get triggered to him. I've used the mail() function but the email isn't going through. This is on a remote server. Am I missing out on something? Below is the PHP code that I'm using.
<?php
$to = 'xyz#gmail.com';
$subject = 'Demo mail';
$message = 'This is a demo mail. Please reply to make sure the mail communication is okay.';
$headers = 'From: abc#gmail.com' . "\r\n" .
'Reply-To: abc#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo("<p>Email successfully sent!</p>");
} else {
echo("<p>Email delivery failed</p>");
}
?>
The output is Email delivery failed!
What is it that I'm supposed to do apart from this? Thank you in advance!
mail() function relies on email configuration of the server the program is running on. You should take a look at your server email configuration, or use a library in order to hook up with an external SMTP service, like Gmail or one of your email servers. I recommend the Swift email library, which is well known, it's thoroughly tested and has a great community behind:
http://swiftmailer.org/
check php.ini config file for SMTP connection or add ini_set("SMTP", "IP_ADDRESS"); in your script.
you can also wrap your script with a error handler to debug the SMTP connection.
When I send mail via PHP's mail() it sends the wrong header information...
$to = 'mypersonal#gmail.com';
$subject = 'the subject';
$message = 'hello, hi :)';
$headers = 'From: Support <support#site.com>' . "\r\n" .
'Reply-To: From: support#site.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
in my gmail it shows
Support via mydedicatedserver.dediprovider.com
How can I configure PHP mail() to send my domain name?
mail() is already sending your domain name.
Gmail sometimes displays that message when Google is not familiar with your server and the hostname of your server does not match the domain name you're sending e-mail from. It's an anti-spam/anti-phishing measure.
Add proper SPF records to your domain. If the server is under your control, try changing its hostname to something that includes your domain name, like server1.site.com. Follow all other advice listed in the link below. Even then, there is no guarantee that Gmail will drop the message right away. In my experience, that message goes away after a while when Google becomes familiar with e-mails from your server and decides that none of them are spam. But Google seems reluctant to disclose exactly what is required, probably because they don't want spammers to get too clever.
See: https://support.google.com/mail/bin/answer.py?hl=en&answer=1311182
Also, the Reply-To: From: header is wrong.
I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = 'itsdfdsf#7sisters.in';
$email_from = "info#7sisters.in";
$full_name = 'XXXX';
$from_mail = $full_name.'<'.$email_from.'>';
$subject = "testing sender name";
$message = "";
$message .= '
<p><strong>This is only a test mail. Please do not reply.</strong><br />
';
$from = $from_mail;
//$headers = "" .
// "Reply-To:" . $from . "\r\n" .
// "X-Mailer: PHP/" . phpversion();
$headers = "From:" . $from_mail . "\r\n" .
"Reply-To:" . $from_mail . "\r\n" .
"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 'failed !!';
}
But although $to mail does no exist,it is not showing failed !!
The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.
I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog
check the return from of mail
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one...
You should run the $to through a validator to check its a valid address and then throw an error if its not, don't rely on mail() to filter out things which you already know are wrong, or can check against easily.
--UPDATE
Then check out #SeRPRo , but what your trying to do is hard work to test programatically - its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it's real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don't be surprised to find it difficult going if you avoid the verification e-mail route.
But although $to mail does no exist,it is not showing failed !!
actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. There's no easy way in PHP to check whether it's delivered.
You could CC yourself as a way of testing that it is leaving the outbox.
In my case it helped to set the return-path via the commandline parameter "-f", which can be passed in the $additional_parameters parameter of mail(). so i call
mail($to, $subject, $message, $headers, "-f address.where.i.want.the.bounces#xy.com");
... according to some comments on http://www.php.net/manual/en/function.mail.php hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the "From:" in the heade ... and so on)
The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory http://www.df.eu
Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce#mail.com');
This will send bounce email at bounce#mail.com if recipient mail id does not exist or recipient does not receive email by any other case.
I ran this PHP script in a dedicated server (OVH Kimsufi, Debian 6.0 stable).
<?php
$msg = "coucou les amis";
$sub = "test";
$head = 'From: webmaster#example.com' . "\r\n" . 'Reply-To: webmaster#example.com' . "\r\n";
echo mail("31415#yopmail.com", $msg, $msg, $head);
?>
Mail returned 1 but the email was never received... How to fix it ?
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
PHP has sent the message. Whether it reached its destination or not is beyond PHP's control.
It depends upon your server whether your server allow you to send mail using php or not
There is a lot that can wrong. Are you using Sendmail for example. Maybe you should inspect those logs, which probably are located at.
cat /var/log/mail.log
Or maybe your email does get send, but gets delivered in the spam folder?
I would instead advice you to outsource sending out emails using for example Sendgrid. Sendgrid is free when you sent less than 200 emails daily.
The problem was the exim ( = sendmail) default config. It do not allow direct SMTP sending (only local).
# dpkg-reconfigure exim4-config
You made a mistake and used $msg in place of $sub where subject was required as argument to the mail() function.
$msg = "coucou les amis";
$sub = "test";
$head = 'From: webmaster#example.com' . "\r\n" . 'Reply-To: webmaster#example.com' . "\r\n";
echo mail("31415#yopmail.com", $sub, $msg, $head);
After fixing the problem described in holodoc's post, you shall consider getting rid of mail(), which is unreliable.
I have an email form that isn't sending out an email to the recipient or a copy to the client. The form can be found at www.kelcos.co.uk/contact and the files associated with this are:
/index.php
/jquery.js
/sendemail.php
/submitform.php
/thanks.php
/verify.php
I have used this form on other websites http://www.bowlesgreen.co.uk/contact/ and http://www.arbortectreecare.co.uk/contact/ and it works fine - the only difference is that these other sites use my usual hosting provider and for the one that won't send I'm working through the clients hosting provider, which I can only presume is what is causing the problem.
I have contacted the hosting and so far we have eliminated a few things such as:
'The limitation to our systems is that the emails sent using scripts will be blocked if they are not going to or coming from an email address setup on the web hosting account. - so I am now sending the form to an a kelcos.co.uk address, but still no joy.
PHP/ASP was originally disabled, but now has been activated
the mail() script is enabled
I would really appreciated any advise any of you could offer.
Thanks
No, http://www.bowlesgreen.co.uk/contact/ doesn't work fine as you said. Firebug reports: POST http://www.bowlesgreen.co.uk/projects/wp/wp-content/themes/bowlesgreen/contactform/sendemail.php 404 Not Found
Your forms are submitting to the wrong URL.
Is Qmail available on the server? Are you setting the headers properly?
Try something like the following:
putenv ("QMAILUSER=myuser");
putenv ("QMAILNAME=My Name");
putenv ("QMAILHOST=mydomain.com");
$headers = 'From: My Name <myname#mydomain.com>' . "\r\n" .
'Reply-To: My Name <myname#mydomain.com>' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail_to = 'myemail#myemail.com';
$subject = "Testing email";
$body = "This mail is a test";
mail($mail_to, $subject, $body, $headers);
And see if you receive an email!
try these
http://forum.codecall.net/php-forum/28696-php-contact-form-isnt-sending-out-email.html
http://www.astahost.com/info.php/Php-Send-Email-Problem_t2259.html