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

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.

Related

PHP mail function is not working for Webform

Below is the Webform PHP code I used for e-mail function. After I click the SUBMIT button in the form, I am successfully getting redirected to the thankyou.html page but I don't get any e-mail to my e-mail account. Kind help is deeply appreciated.
PHP Code:
<?php
if(isset($_POST['submit'])) {
$emailbody = 'Name: '.$_POST['name']."\n"
.'E-mail: '.$_POST['email']."\n"
.'Phone: '.$_POST['phone']."\n"
.'Address: '.$_POST['addr']."\n"
.'City: '.$_POST['city']."\n"
.'State: '.$_POST['state']."\n"
.'Enquiry: '.$_POST['msg'];
mail('myemail#email.com', 'Subject Line', $emailbody);
header('location: thankyou.html');
} else {
header('location: index.html');
}
?>
You may be missing the "From" field in the $additional_headers argument. Try
$headers = 'From: myemail#email.com';
mail('myemail#email.com', 'Subject Line', $emailbody, $headers);
It might be a problem with your host.
I think due to the email spams that #email.com got from your host it may be now in the blacklist.
This means you have to contact your host about this problem and tell them to talk with #email.com to remove your host from the blacklist.
I had this problem a while ago on 000webhost.com and they told me i can't send emails to an yahoo.com account because yahoo.com added the 000webhost.com on the blacklist because users spammed the Yahoo servers.
It's likely that your server, or one along the route, is refusing to deliver the mail because it has no From: address. You can add one using the headers parameter for mail():
<?php
if(isset($_POST['submit'])) {
$emailbody = 'Name: '.$_POST['name']."\n"
.'E-mail: '.$_POST['email']."\n"
.'Phone: '.$_POST['phone']."\n"
.'Address: '.$_POST['addr']."\n"
.'City: '.$_POST['city']."\n"
.'State: '.$_POST['state']."\n"
.'Enquiry: '.$_POST['msg'];
// Add extra headers here
$headers = "From:address#examle.com"
mail('myemail#email.com', 'Subject Line', $emailbody, $headers);
header('location: index.html');
} else {
header('location: about-us.html');
}
You should check any user-suplied input to ensure it doesn't try to subvert your mail function by adding headers of its own. Check for text like To:, Cc: or Bcc:.
Most ISPs will refuse to deliver email unless it comes from a domain registered with them. Sometimes the from address must exist on the server generating the mail.
Note that there are other reasons why mail might not be delivered: bad addressing and spam filters to name but two.
I found the answer. I just changed $emailbody into $message and it is working fine now. I am not sure that using $emailbody instead of $message was the issue but it is working fine now.
Thank you all.
First check your hosting server is that providing mail functionality for this you just write
echo phpinfo();
you need to check the mail function enable or not

One email to forward to multiple email addresses

I am looking to create one email (example#domain.com) to forward to all email addresses in a database.
People can email to example#domain.com, and then that email would get blasted to a list of predefined email addresses. It would need to include support for attachments.
I realize this is not secure at all and leaves this email address open to anybody to use, but this is what our organization wishes to do.
What would be the best way to do this on a PHP server?
Thanks.
This can be achieved in PHP Server as you have to go in depth of following things:
Email Piping
Email Headers
MIME Email attachments
Mailing List Management
While emailing to a lot of people in the way you mentioned is not a good idea...
You can use PHP's "mail" function:
$to = "user1#domain.com, user2#domain.com"; //(Comma separated list of emails)
$from = "noreply#domain.com";
$subject = "Hello";
$message = "Hello there!";
$headers = "From: ". $from . "\r\n".
"Reply-To: ".$from . "\r\n";
//send it
mail($to, $subject, $message, $headers);
while($row = mysql_fetch_array($emails))
{
$addresses[] = $row['address'];
}
$to = implode(", ", $addresses);
And then send mail using mail function ... mail($to, $subject, $message, $headers);
The way you have worded your query is a bit confusing but it seems you have a hosting server that is going to receive emails to a domain email. You then want to have those emails autoforwarded to a list of recipients. It looks like you want to investigate (given you mention sendmail) linux mail services and autoforwarding. You may be looking at postfix and possibly using procmailrc to trap and redirect incoming mail on your server.
I have done this with procmailrc before but it really depends on what service is handling the incoming mail.
There is no problem calling a CLI PHP script as suggested by #George to then read your recipients from the database and do the sending.
While Arthur's answer made sense and most likely would work, I ended up finding that my host had the feature I was looking for buried deep inside it. For those who were wondering, it is called discussion lists.

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.

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

Odd behaviour with links in PHP Mail function

I'm currently trying to get links working in emails sent via the PHP mail function. Here is my code - I've also included some things I've tried (commented out along with notes):
$to = "test#testing.com";
$subject = "Testing email";
//$body = '<strong>This is strong text</strong>'; <-- Works
//and the text is correctly emphasised.
//$body = 'Link Test'; <-- Works
//but without http:// at the start makes the link relative to the server root
//$body = "<a href='http://www.yahoo.com'>Link Test</a>"; <-- Does not work
//$body = "Link Test"; <-- Does not work
$body = 'Link Test'; //<-- Does not work
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Steven Parler <". $to . ">\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
As can be seen html is working without links, and links also work providing I don't include "http://" in the link itself. If I do include the "http://" then no email is received / sent at all (I'm unsure which as the mail() command returns true to say it was sent).
I'm not really sure why this isn't working. Could it be some setting that needs changing on my webhost's server? I'm on windows shared hosting.
Thanks in advance for any advice anyone can give me - been pulling my hair out over this lol. :)
Don't build MIME messages by hand. It's just too painful and fragile. Use something like PHPMailer or SwiftMailer to do it for your automatically. You provide the HTML, they'll provide the appropriate headers
I completely agree with #Marc B.
Here are a couple more options: XPertMailer, Zend_Mail, Rmail, HTML MIME MAIL
I've worked with all of these but since I'm working mostly with the Zend Framework I'm lately using Zend_Mail.
Having said that, your hosting provider might be blocking your emails because they might think it's SPAM. Try generating valid html markup that passes validation and see if that helps.
That's very odd behaviour, I'd expect the last 2 to work perfectly. I tried this out on my server, and mail($email, $subject, $body, $headers); worked perfectly with a $body of
text text text: \n http://website.com.
Perhaps it's a setting somewhere? If you're using awardspace, they require a from header to be used, and the mail sent from an e-mail registered with them. Other hosts might have a similar procedure.
Thanks for all the responses. I finally got around it by using the SMTP feature of Swiftmailer which correctly created the email with links.
Out of interest I tried the code I used in my original post again... but this time I used a random web address (http://www.trustedreviews.com). The email arrived... I then tried a lot of other web addresses - google.com, hotmail, yahoo.co.uk etc and they all arrived. Changed it back to (http://www.yahoo.com) and lo and behold the message did not arrive again. So it seems out of the millions of web URL's there are I chose the one that my webhost has decided to block lol...
That said the yahoo link does arrive ok using the smtp function of swift mail; it's only with the php mail function it doesn't seem to work. I guess I'll contact them and ask why it's blocked. I'll most likely stick with using the smtp method as at least it seems like it bypasses any restrictions.
Thanks again for everyone's time and help :)

Categories