Why can’t I send email via PHP? - php

I wrote this code for sending email using PHP and upload it to a server, but it doesn't work:
<?php
$to = "recipient#example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
It says "Message delivery failed..." every time!
Can anyone help?

I tried running your code on local server and this is the error i got:
"sendmail_from" not set in php.ini or custom "From:" header missing
You should probably set "From" header and it should work just fine
...
mail ($to,$subject,$body,'From: sender#example.com');

Antispam softwares are very picky, if you send emails that way you have few chanches to get them in your recipients' inbox.
If you want more control on your messages, consider sending emails with a mailer library. http://swiftmailer.org/ is a really good choice. This is not a complete solution to spam problems, but it helps.

Related

send mail without passwords

Is there any way in php or javascript to send mail without passwords.
I need to send mail without asking for his passwords.
Please help me finding the solution.
Thanks in advance.
EDIT
this the code i tried
<?php
error_reporting(E_ALL);
$to = "person2#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "person2#gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers) or die("errors");
echo "Mail Sent.";
?>
and it displays errors as o/p and not sending mail
With PHP you can just use the mail() function, this does not require the user to enter his/her password.
The SMTP server can be set in php.ini.
you can refer below link for this
http://www.w3schools.com/php/php_mail.asp
mail() function does not work on localhost so check this on server.

Cannot use variable $_SERVER['HTTP_HOST'] in php mail()

I tested this:
<?php
$to = "recipient#example.com";
$subject = "Hi!";
$server = $_SERVER['HTTP_HOST'];
$body = "From: ". $server. "<br>";
$body .= "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
This will never send emails to me although the feedback "successfully" is displayed.
The code will work (email actually sent) however if I removed the inclusion of
$server = $_SERVER['HTTP_HOST'];
in the email body.
Very weird, it doesn't make sense ?
This is just a PHP page. I call this page from a browser !! Please try ...
UPDATE!! okay, Instead of using $_SERVER['HTTP_HOST'], I use a string "user.server.com" directly. AND, it did not work !!
But, when I modified a string a bit, like "user.server.com.us", it works !!
So basically, the mail server filler its own reference to its domain, not sure why its doing this...
Some spam servers grade your message as 'spammy' for urls in the body. Unfortunately you'll be hard-pressed to find a more specific error message with mail() unless you have access to the sender and recipient's mail server logs. Give SwiftMailer or another PHP Mailing library for better support.
When using mail() function you are required to specify 'From' header. Please refer to this documentation page, look for 'additional_headers' parameter's Notes section.
So your mail() call will look like this:
mail($to, $subject, $body, 'From: some.guy#example.com');
Of course you don't receive it because there these days all major webmails use anti spam filters and probably your message is detected as a spam.
The solution is to specify exactly your email for $from variable, if you don't specify your real email your message is detected as spam.

PHP mail script does not send mail

I have this mail script on my page: mail('myadress#server.com', 'New client added by user', 'test message');
but I do not receive anything! (of course I added my real adress). I tried it with 2 different adresses, looked in my spam folder, etc... just nothing. but the script executes just fine.
Is there any log I can view or invoke to see exactly what happened?
thank you for your help!
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
try this it will going to work for u ....
1) Check the return value from the mail() call:
$status = mail(...);
if (!$status) {
die("Mail failed");
}
If this fails, then PHP cannot even get the mail out the front door, and you'll have to figure out why - e.g. are you on a Windows box and haven't configured the mail options in php.ini?
2) Check your mail server's logs. Most Unix/Linux systems have a local mail server (the MTA) which will accept the mail from PHP. If it's misconfigured or having trouble, it may still accept the mail from PHP but then leave the mail to rot in a queue.
Perhaps your server's been placed on spam blackhole lists and it simply cannot deliver mail anywhere, which means you've probably got all of your test mails stuck in an outgoing queue that can't go anywhere.
Had to add the header "from" and use an email adress created on the server.

Sending e-mail with php

I am trying to send mail from my site.But its not working now. Can you please tell what all changes should I make in php.ini configuration file for achieving this functionality? Using windows OS. Here is my code for your reference.
$to = "name#gmail.com";
$subject = $subject;
$body = $message;
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Windows doesn't contain an sendmail like Linux does.
So for Windows you have to provide an SMTP server: http://www.geeklog.net/faqman/index.php?op=view&t=19
There may be a lot of things wrong, but for starters, the PHP manual for mail() says:
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.
You're not setting the From header in your code, so that would be the first thing to check.
(Also: $subject = $subject; is odd.)
Try setting these as well.
$headers = "MIME-Version: 1.0\r\n";
phpini_set("sendmail_from", "info#mydomain.com"); // at the beginning of yoru script

PHP: cannot send sms, but can send regular old email

I am trying to send text messages to my phone from my server using php. I recently configured the server to send email, which it does (verified). It, however, goes into my spam box. When I try to send a message via sms I do not receive anything.
This is the script I am using:
$to = "myemailaddress#gmail.com";
$subject = "testing";
$body = "";
$headers = 'From: testemailaddress#gmail.com';
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
The address that I am using for sms is myphonenumber#txt.att.net in the 'To' field.
I am going to go out on a limb here and say this is a authentication issue, maybe.
Is there anything I need to configure further? (i.e. php.ini)
Most email providers/servers today rely heavily on spam filtering / dnsbl. Your webserver is not a know mail server, and you probably did not set up SPF or anything.
An approach to avoid all those issues would be to utilize a Google Mail address (or any other providers). And instead of the PHP mail function use something more complex like Swiftmailer, which generates Mail headers that are less commonly autoclassified as spam.
See also: Using php's swiftmailer with gmail

Categories