Sending e-mail with php - 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

Related

mail() function doesn't appear to be working

I am using the following code to try and send an email - I can't see any problem with it - but it's not working, it displays the message "email sent" but I don't receive anything.
<?php
$to = "email#address.com";
$subject = "Query";
$message_body.="Name: ".$_POST["name"]."<br>\n";
$message_body.="JobTitle: ".$_POST["jobtitle"]."<br>\n";
$message_body.="Phone: ".$_POST["phone"]."<br>\n";
$message_body.="Email: ".$_POST["email"]."<br>\n";
$header = "Reply-To: ".$_POST["email"]."\n";
$header .= "Content-type: text/html;
mail($to, $subject, $message_body, $header);
echo "Email sent";
?>
Can anyone help?
Check 1) For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
You mentioned that there are no errors.. so next..
Check 2) Check the return value of function.. TRUE or FALSE
Check 3) See if any warning by enabling error_reporting(E_ALL)
Check 4) Actually mail sent, but went to Spam folder.
For mail() , 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.
Also there is a difference in a way how it works in Windows and Unix.
Your example is missing a " in the last $header line
try adding:
error_reporting(E_ALL);
ini_set("display_errors","On");
at the beginning of the file to show the errors / warnings you get. Most likely sendmail is not running on your server
Try to check your SMTP server configuration.
http://php.net/manual/en/mail.configuration.php
Probably it blocks your mail.

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.

Mail() not working, any ideas why?

Hi guys i am using the mail() from the contact form and for some reason it is not working.
The php coding i have setup is as follows:
// sending email to sales#xxx.com
$to = "hello#xxx.com";
$subject = 'Email From {$name} from your website';
$message = "$name has contacted you from the website and says:
$mcontent
$name 's contact email address is: $email";
$headers = $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers) or die ("Failure");
// end of sending mail
returnheader("/xxx/email-sent");
i get no errors at all, it even goes to the success page when completed.
Any ideas why this would not work.
I have checked your code and it is working fine on my server , i am getting e-mail.
Here , it looks like some problem with your SMTP server settings.
There is nothing wrong with your PHP script.
You can find your solution here.
php.ini & SMTP= - how do you pass username & password
Also in windows environment ,
http://www.ruhanirabin.com/php-sendmail-setup-with-smtp-iis-and-windows-servers/
If you are on WIndows, make sure you have an SMTP server in your
php.ini
If you are una Unix, make sure the MTA is running: If it is (at least partly) installed but not runnng, you will get exactly this effect
Edit
If your MTA is not running, and you start it, the mails sent with PHP will go out! They have beein queued, but not processed.
This is probably related to the mail setup. Do you have a mailserver running on the machine? Check sendmail / smtp_server settings in php.ini.
mail() uses the sendmail, by default: sendmail -t -i
It returns TRUE if the mail has been accepted, not if it has been sent:
Return Values
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.
I would suggest using http://swiftmailer.org/ with SMTP rather than mail().
The first line of $headers is invalid if $email is just an email address. Presumably you would want something like:
$headers = "Cc: " . $email . "\r\n";
or
$headers = "From: " . $email . "\r\n";
The cause can be for example, you don't have setup a mail server, or configuration of firewall if you has it.
You should use double quotes instead of single quotes in the variable $subject
$subject = "Email From {$name} from your website";

Why can’t I send email via 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.

Categories