Sending mail on PHP using LAMP - php

I have been trying for awhile now to send mail using PHP, and I can't seem to get it to work.
I looked at the tutorials, and they make it seem simple enough. Yet, it seems to actually require more work than what is mentioned. Through research (to make this short), it seems that instead of the "From: " header actually having the address I give to it, it seems that... sendmail replaces it or something with daemon#ubuntu? Ok, even if I don't include a header I still get spam from my gmail account from the daemon#ubuntu thing (I am running ubuntu on a vm, and sending mail to my gmail account). I guess this could explain why I incessantly get errors when I try to send mail saying how there is an error in the "MAIL FROM command".
So before I get myself involved with messing around with sendmail, is this the (likely)culprit of the daemon#ubuntu nonsense? The tutorials make mention of having things like sendmail, but they really don't go into any details of what to do with them. I did make sure that sendmail is being used in my php configuration file.
I have heard of alternative mail servers like PHPmail or something like that. I really don't want to get involved in installing and configuring more software unless I really have to. So unless you guys really think sendmail isn't worth it, I would prefer advice on sendmail if it is the problem.
Thank you for your time.
edit: I am using the PHP mail() function on an Ubuntu vm by VMware Player.
So someone wanted to see my code. I somewhat wanted to avoid this simply because I tried many different possibilities. I will just include what I have in my file right now:
<?php
$myEmail = "#####gmail.com";
$fromAddr = $_POST['sender-email'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromAddr\r\n";
$headers .= "'X-Mailer: PHP/'" . phpversion() . "\r\n";
$message = $_POST['message'];
$status = mail($myEmail, $_POST['subj'], $message);
if($status) {
echo "Your message has been sent, " .$fromAddr. "!";
}
else {
echo "An error occurred while trying to send the mail.";
}
?>
The MIME type thing, and Content-type thing I added later on. I have tried this without them. I have also tried sending mail without using any headers at all, and still apparently get the daemon#ubuntu thing. Also, I edited out my e-mail address to post the code on here. Ok I guess I should add this too: I am getting the data from an HTML form from another page. On submit, I come to this php page where it grabs that data and then sends the email (this isn't for anything professional, I am just doing this to learn more about web stuff).

Postfix is a very easy to use and install email server, in my experience. You need an email server installed and configured before you're able to send email on your localhost.

Related

Is there a way reduce the Email Delay of PHP Mail()

I am using a fastcomet website, and I am trying to create a feedback messaging system.
I have created the following function, for organizing a template for sending emails along with the headers accompanied to it.
send_email.php
<?php
function send_email($to,$from,$title,$sending_message){
$message="<html><body>";
$message.=$sending_message;
$message.="</body></html>";
$headers = "From: ". $from . "\r\n";
$headers .= "Reply-To: ". $from ."\r\n";
$headers .= "Return-Path:". $from . " \r\n";
$headers .= "CC:\r\n";
$headers .= "BCC:\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
return mail($to, $title,$message,$headers);
}
?>
With other pages, I use the following code include 'send_email.php', and use the function send_email($to, $title,$message,$headers), and it works. The only problem is that the receival of the email either takes too long, or arbitrary in nature.
Sometimes, I test the feedback system by sending 3 emails. For example: Email1, Email2, Email3 are sent in the following order. Sometimes, I'll receive Email3 first than Email1 or Email2 first than Email3.
In terms of time, sometimes I'll receive the email in a few minutes, but some in a few hours which makes testing difficult because I am not able to know the results of the code until a few hours, which leaves me to wonder whether my code worked or not.
Note:
By using the following code echo "<p>". error_get_last()['message'] . "</p>"; I am able to figure out that the function did run successfully.
I tried testing using both AJAX request and Form submission, and I found that generally Form submission takes less time for it to be received. (although I like the concept of AJAX request, where I don't have to change page)
My question is, is there a way to reduce the time it takes to receive the email? or is there a better way to approach this problem?
You're asking yourself questions where the answer would really lie with a dedicated email provider.
PHP does not "send" the email with mail(). PHP delegates to either a SMTP server or sendmail on the server PHP is running on; from there, each avenue has additional latency:
Depending on the version of software providing sendmail, your mail may fall into a queue to be processed/sent later
Depending on the SMTP server, this queue may also exist there
The recipient SMTP server may have its own processing requirements and ruleset in place, which might introduce more delay.
The "delay" difference you are seeing between AJAX and/or form submission are not due to the method you're doing your request with. They're purely down to statistics and underlying queues.
Switching and effectively "outsourcing" (it's not, but it's the closest word) your outbound email capabilities to a third-party will provide some clarity into this along with a lot of additional features. Most will also allow you to see emails being delayed/not sent/bounced on top, which is another major win. And, to be fair, it's also pretty cheap.

Mail ends up in junk folder - PHP contact form [duplicate]

I wrote a PHP script to send emails.
My script is like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: abc#yahoo.com' . "\r\n";
// Email Variables
$toUser = "someone#yahoo.com"; // recipient
$subject = "testing"; // subject
$body = "<html><body><p>
Example of including an image via html \<img\> tag:
<br>
<img src='../images/profile.jpg'>
<br>
My new picture
<br></p></body></html>"; // content
if (mail($toUser,$subject,$body,$headers)) {
echo "sent";
} else {
echo "failed";
}
Well, of course I use a valid email address for sender and receiver. I did receive the email, but it goes to junk mail. So I went for google research. Is it because of my "header" script problem? If it isn't, then what could cause my script to send a junk mail? Any solution?
Please try this:
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
Perhaps the problem is that yahoo uses domainkeys verification, which will likely fail for your application given that the mail is not actually coming from yahoo's servers.
When I've once had a similar problem I looked at the headers and found out that my host uses SpamAssassin. So I googled for 'SpamAssassin score' and found a multitude of information on how to incorrectly (and thus correctly) form an email.
For example: SpamAssassin score list
1. Check mail content
As others have hinted it is probably marked as spam because your mail looks like spam.
I am not sure if you the script that you have posted is the actual one that you are testing.
If it has the actual mail body & headers, then running this message through a standard installation of SpamAssassin gives it a spam score of 4.9
X-Spam-Status: No, score=4.9 required=5.0 tests=BAYES_50,HTML_IMAGE_ONLY_04,
HTML_MESSAGE,MIME_HTML_ONLY,NO_DNS_FOR_FROM,NO_RELAYS autolearn=no
version=3.2.5
Since the email body has only HTML it has a greater chance of being handled with suspect by most anti-spam solutions.
2. Mail server's IP
Another aspect worth checking will be the IP address of your mail server. Any mail originating from dynamic IP addresses will potentially be considered as SPAM.
3. Blocklists
Also check if your IP address is listed in one of the block lists. To start with please check your IP address with http://www.spamhaus.org/lookup.lasso.
Use mxtoolbox.com to check the servers IP to be blacklisted or not. As well this website can help you with a couple of email related checks.
Of course there are a long list of checks running inside spam filters. As already suggested, check the email headers for details about the spam filters rating of the spam email.
Hope that helps!
**This Works Perfectly fine for me**
$to="reciever#reciever.com";
$subject="This is Your Message";
$from = 'Sender <noreply#sender.com>';
$body='Hi '.$name.', <br/><br>Now You can See Yor main in inbox';
$headers = "From: " .($from) . "\r\n";
$headers .= "Reply-To: ".($from) . "\r\n";
$headers .= "Return-Path: ".($from) . "\r\n";;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($to,$subject,$body,$headers);
I was having the same problem:
The problem is that when you specify content-type before the "From:" part , the mail comes as a spam.
But if you specify "From:" before the content part it comes as a normal mail and makes you smile and curious.
As schnalle said, one problem surely is that the smtp server that you use to send the email and the one thet you specify as From, is different.. the from's domain whould be the same that the server youre running on.
So, you can use the yahoo server to send the email (check if they allow the smtp remote connection, but i guess they do) connecting by smtp, and this will solve 1 problem.
Another one is the html contents without the alternative plain text contents, but, this one is less important.
I suggest you phpMailer, free and open-source php class to send email, easly to use (i use it event o send mail through gmail server)
On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
Make sure you send your e-mails from an existing account on your server/domain.
Make sure you have the reply-to address in your header.
These are the basic things you can try.
if your website domain is mydomain.com then in From headers make sure to use someone#mydomain.com
Remove the Content-type: text/html and add $headers .= "X-Priority: 2\nX-MSmail-Priority: high"; to get rid of Spam. This method has been tried and tested.
the problem is, the server you're sending the mail from is not a yahoo server. most spam filters check if they match, otherwise it would (and is - or was) possible to easily fake the sender. ever wondered why you get spam from bill.gates AT microsoft.com or your own mail address?
You've got two solutions:
use Yahoo's SMTP using abc#yahoo.com credentials to send mail from abc#yahoo.com;
use other from, with your own domain;
You can try the mail class and test file that I have created here. I have tested the files and can send emails to my hotmail and gmail under a different mail name. The main reason why the emails are mark as junk is because the structure (both header and message) is not correctly done. In most cases, it is the line feed that is causing the problem.
I can use it to send mail with attachments to Gmail. However, the attachments dont work for hotmail. Hope this helps =)
You can check the files here..

PHP mailer setting from address in mail function

Ok so I set up a php mailer on 2 separate pages on a site I am working on. I had previously worked through everything in the mailer on a site I built and it was straight forward how to set the email after setting all the variables:
mail("$email",
"Receipt: $thesubject",
"$message",
"From: $replyemail\nReply-To: $replyemail");
So this was what was at the end of the contact (processing) page. I have $email and $replyemail being put into the $message and they come out correctly in the message that gets sent to my email specified by $email.
The part I don't really understand is the address the message says it is coming from is not that $replyemail but instead it says it is being sent from:
rtl.srv#gmail.com
I saw a few posts that were similar but none of them fixed the issue, I followed this and checked to make sure the servers php.ini files had SAFE_MODE = off. Then added
'-f $replyemail'
to the end of the mail function above like that link advised but it didn't change anything... I saw somewhere else that it explained you may have to add the email address to the file /etc/mail/trusted-users but the issue is that email is dynamic. Since that email name is whatever the user input as their email. Then the email is sent to the site admin to review, and I want the email to say it is from the users email that filled out the form.
I know the variables are set correctly because they are being printed out correctly inside the message of the email. So if anyone has any idea why this is happening or how I could go about fixing it I would appreciate any insight.
The project is built in wordpress but I just dropped the files onto the server through SSH, not sure if that has anything to do with it.
Thanks,
-Alan
There are a number of issues that can cause this, and I encountered this issue a while ago when I built my site. I solved it by first creating a function to handle the mailout for me:
function mailouthtml($to, $title, $body, $from){
if(!isset($from)){
$from = 'Default Sender<address#example.com>';
}
$header .= "Reply-To: $from\r\n";
$header .= "Return-Path: $from\r\n";
$header .= "From: $from\r\n";
$header .= "Organization: Example.com, Inc.\r\n";
$header .= "Content-Type: text/html\r\n";
mail($to,$title,$body,$header,"-f $from");
}
Let me break this down a bit. The function calls for a To address, the email title, the email body, and the From address. The From address is used to add information to the email header. The header information of your email is what tells your mail server what to do with your email, and it needs a bit of info in order to handle the message properly.
The mail() function in PHP is formatted as follows:
mail($to,$title,$body,$headers,$additional_parameters);
The mailouthtml() function I've created here constructs the header manually, and adds the additional parameter "-f $from" to set the "From" field explicitly. The $from variable is optional in the function call; if it isn't present, it sets a default. You can find more information on the mail function Here.
Note the following:
Most MTA's require a Reply-To, Return-Path and From field, or it may be flagged as spam.
Content-Type is required tell what type of content the mail server is delivering, typically text/html or text/plain.
I have an if(){} statement that sets a default address if none is specified. You can omit 'Default Sender' if you would rather not specify a name. You can also change that to whatever you'd like.
Organization is optional.
I would also recommend you have SPF enabled on your domain name, and DKIM signing on your messages. These are both implemented via DNS entries for your domain name. If you don't have access to that, no big deal; they're meant to control spam.
Also, if you have a dedicated server and your own IP address, make sure you have a Reverse DNS record set up. This will also help foreign MTA's identify your message as authentic.
don't make this to yourself. Use some mature library like phpmailer or swiftmailer, they will help you to avoid these kind of troubles...I know there are more includes, etc, but there is not point to give fight to this. If you still want to do this, try setting the header Returh-path

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 :)

PHP Mail will not process ".com"

I know this is weird, but when a ".com" is added to the subject or body text to send PHP mail (like - xyz.com), the mail will not send. It's a problem with this hosting company (ipage) but i want to see if their is something i can do in the script itself, because it's a CMS of mines and i would like to keep generalized for everyone who uses it.
script is simple and as follows:
function confirmEmail($username, $email, $confirm_code) {
$sitename = str_replace(".com", " .com", site_name);
$subject = "Your confirmation link here";
$headers .= 'To: '.$username.' <'.$email.'>' . "\r\n";
$headers .= "From: ".$sitename." <".support_email.">". "\r\n";
$message = "Your Comfirmation link \n
Click on this link to activate your account \n
".root."/confirm.php?confirm=$confirm_code";
$message = wordwrap($message, 70);
$sendmail = mail($username,$subject,$message,$headers);
if ( $sendmail ) {
return true;
} else {
return false;
}
}
Now as you can see, i replace the ".com" with " .com"(note the space at the beginning) for the $sitename variable, and that works but the problem is the "root" contains the full path to the givin site so the link is like "xyz.com/confirm.php?confirm=$confirm_code". I can't allow a space before the '.com' in the site path like
"xyz .com/etc..." because the obviously the user can't click the link from within their email.
I know this is a weird problem and I have no clue on what's going on or what to even ask the hosting to change.
*note: site_name and root are defined previously in another page, and they work fine. Also no errors are being returned.
Any ideas out there?
Your code is correct. Your workaround should not be necessary.
This is very likely a provider issue.
Don't know about your environment, but if you have not full control of your mailserver, try sending a mail with a link using telnet so you really see everything. Often spam filters etc are badly configured and don't return the correct codes.
If you don't have any luck with that I recommend switching to an external SMTP server that is well configured. You can perfectly use gmail for that if you dont send tons of mails.

Categories