I was testing out my mail configuration. First I did a normal mail() call in a separate .php file. That worked fine. Then I copied the exact same code inside a route callback in Laravel 4 and now it doesn't work. How is that possible?
This is the code that works:
$from_add = "name#your-web-site.com";
$to_add = "someone#gmail.com"; //<-- put your yahoo/gmail email address here
$subject = "Test Subject";
$message = "Test Message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
mail($to_add,$subject,$message,$headers);
The adresses don't matter because I am testing with Test Mail Server Tool
When working with Laravel 4, it's always advisable to use the built-in Mail function.
This has a couple of advantages I wouldn't want to miss:
You can easily test mail features by setting the pretend option to true
It will allow you to send HTML mails using Blade views
You can choose which way your mails should be sent depending on your server config (sendmail, phpMailer)
You can queue mails so they will be sent later when there's less server load
You can do stuff after mail delivery in a callback function
If you still insist on using the php mail() function, set your driver in app/config/mail.php to "mail".
Read Email Function in Laravel
Related
I am creating a small company, and would like to send out emails to my clients once they have signed up for my service to activate their accounts. I am currently using PHP's mail() function, however I am worried that my emails are being filtered out by spam filters. Is there a better way to go about this?
$email = 'XZY Client Email address # somedomain.com';
$emailSubject = "Welcome to XYZ Service!";
$to = $email;
$subject .= "".$emailSubject."";
$headers .= "From: no-reply#XYZService.com\r\n" .
"X-Mailer: php";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= "Welcome to XYZ Service! \n Activate your account by clicking the following link: link...";
mail($to, $subject, $message, $headers);
Is there a way to authenticate these emails so that my clients know that they are from my actual service? Thank you in advance!
You can try to get your mail server white listed by some of the major postmasters (AOL, Gmail, Hotmail, etc.).
I am not sure if this is what you want, but you can setup a mail server to handle all your emails.
Try using that mail server for sending emails and see if they are still filtered or not.
BTW, setting up a mail server on *nix systems is free and worth giving a try.
I have a simple arcade site that people from my highschool use and for quite a while I've been using the method that you showed. I didn't look at my code to verify that they're identical, but they look fairly close and mine works with gmail and all other big name services I've ran into!
Just remember that some hosting companies limit the amount of PHP mail that can be sent per minute. Mine only allows 9 per minute, I know I've gotten suspended a few times on accident while running test mail sends, haha.
I have created a form and written a php script to send a mail after clicking the submit button, and display a thank you message after sending the mail. The "Thank you" message is put into another HTML page.
I put those to WWW folder in WAMP server and try it. The "Thank you" page is displayed but the mail is not sent to my mail account.
Why did it not send my mail? Is it necessary to host the web site to do this?
This is the code that I'm using:
<?php
$email_from = 'aa#gmail.com';
$email_subject = "Arrange new Safari service";
$email_body = "You have received a new message from $name.\n"
$to = "aa#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: thanking.html');
?>
Please read here Sending email from localhost
And here http://forums.devshed.com/mail-server-help-111/how-to-set-up-php-ini-to-send-mail-from-106329.html
If you use the php mail() function, you need an smtp server. Right, that might run on you hosting.
You might also use a librairy like http://swiftmailer.org/ to get it easier (you will be able to use remote smtp servers without pain)
There appear to be some syntax errors in the code you have supplied:
$email_from = 'aa#gmail.com';
$email_subject = "Arrange new Safari service";
$email_body = "You have received a new message from $name.\n";
$to = "aa#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: thanking.html');
I have attempted to fix the syntax above.
You need to configure PHP to use a working SMTP service in order to send SMTP email.
Basically, it appears that the SMTP configuration for your PHP instance is using default values, which point to localhost. But your local computer doesn't seem to be running an SMTP service. So you'll need to point is to a server that is running one, and one which your application is permitted to use.
You have to configure your php.ini with a valid smtp server and email address.
Check this article http://www.sitepoint.com/advanced-email-php/
I have a php file that I am trying to send the output in a email using the php mail function (PHPMailer is not an option as the server I am working restricts their SMTP server). The code for the mail function is
$to = "xxx#example.com";
$subject = "Outdoor Grill Service Request";
ob_start();
require 'grill-form.php';
$body = ob_get_clean();
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers = "From: xxx#example.com\n";
mail($to,$subject,$body,$headers);
echo "Mail sent to $to";
grill-form.php is a php file that contains a html file that has tables that are populated with php variable from a form. This worked perfectly using PHPMailer but once I migrated over to standard php mail is got "screwy".
The issue I am running into is when the email sends I am getting raw HTML code and not the output of the grill-form.php (a styled table with values). I have little knowledge with the php mail function so I might be missing something stupid.
Was wondering what I am doing wrong. Thank you in advance for you help you people are the best.
Assuming that's your actual code, you're overwriting the $headers variable with the third declaration, not appending to what you have. The Content-type header is never making it in.
PHPMailer does support SMTP, by the way--what exactly do you mean by "restricts their SMTP server"? (Here's an example: http://phpmailer.worxware.com/index.php?pg=examplebsmtp)
Send email using mail function working fine. but my question is how to auto save the mail in sent items folder when mail sent
i m using own smtp server and port no is 25
is any config needed on php ini file or i may use imap
my code is as follows
<?php
function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "Content-type: text/html\r\n";
if (mail($to,$subject,$message,$headers) ) {
echo "email sent";
} else {
echo "email couldn't be sent";
}
}
$subject = "Helloooo!";
$message .= "<html><body>";
$message .= "<b>Hey! How are you today?</b>";
$message .= "<br>Regards";
$message .= "</body></html>";
send_email("frm addr", "MYEMAILADDRESS#gmail.com",
$subject ,
$message);
?>
"frm addr" is my own web address which is provided by my mail server
how to store or automatically save the mails to my sent item folder whenever i send a mail using the above function
with thanks in adv
R.Saravanakumar
You have to understand the mail() function works on a remote server. The function you send the E-Mail with does not know the concept of a "Sent mail" folder.
That said, it would probably be possible to have the mail server place a copy of the message in the "sent" folder of an IMAP mailbox running on the same server. That includes a lot of hassle, though, and requires root access to the server.
A much easier solution is having the mail() function send you a copy of every sent mail, and to set up an Outlook rule to copy those mails into your "sent" folder.
For that, you could for example add a specific string (like "mail sent from form 1234567", some random number that is unlikely to be repeated in a normal mail) to the subject when sending your copy. Your Outlook rule would then look out for mails containing that subject, and move them into the "Sent" folder (or any other folder, for that matter).
Storing a sent mail in a "sent item"-folder is done by the client which sends out emails, not the mailserver (which is used by mail()).
The PHP-mail()-function will "just" send out email, communicating with the server, without storing it anywhere, so to have your send messages stored anywhere you'll have to create your own "mail-client" which handles mails and folders. I doubt that's worth the effort, so why not just send your mails to a second mail-account and store them there?
I am trying to send an email from a site I am building, but it ends up in the yahoo spam folder. It is the email that sends credentials. What can I do to legitimize it?
$header = "From: site <sales#site.com>\r\n";
$header .= "To: $name <$email>\r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site <sales#site.com>" . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
Don't use HTML in your email.
Send it via a legitimate mail server with a static IP and reverse-DNS (PTR) that points to the machine's real host name (and matches a forward lookup).
Include a Message-ID (or ensure that the local mailer adds one for you).
Run your email through SpamAssassin and see which bad-scoring rules it matches. Avoid matching them.
Use DomainKeys Identified Mail to digitally sign your messages.
I just successfully tried the following from my Yahoo! Web Hosting account:
$email = "me#site.com";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "To: $name \r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site " . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
However, you have some duplication in your header you should only need to do the following:
$email = "me#site.com";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
In addition to Ted Percival's suggestions, you could try using PHPMailer to create the emails for you rather than manually building the headers. I've used this class extensively and not had any trouble with email being rejected as spam by Yahoo, or anyone else.
There is also the possibility that 'sendmail' (which is underneath the PHP mail() function) needs extra parameters. If you have a problem with return headers (such as Return-Path) not being set with what you set them to be, you may need to use the fifth mail() parameter. Example:
mail('recipient#domain.com', 'Subject', $mail_body, $headers, " -f sender#domain.com");
There is some further evidence that true vanilla sendmail may have problem with this! Hopefully you have 'postfix' as PHP's underlying mail() support on your target server.
In addition to Ted Percival's suggestions, make sure that the IP address the email is coming from is a legitimate source for email according to the SPF record of site.com. If site.com doesn't have an SPF record, adding one (which allows the IP address in question, of course) may help get the emails past spam filters.
And if absolutely do need to use HTML in your email, make sure that you also include a plain text version as well; you'd use the content type of "multipart/alternative" instead of "text/html".
Ted's suggestions are good, as are Tim's, but the only way I've ever been able to reliably get email through to Yahoo/Hotmail/etc is to use the PEAR email classes. Try those & (assuming your server is OK) I can pretty much guarantee it'll work.
Ted and Tim have excellent suggestions. As does Shabbyrobe. We use PHPMailer and don't have any problems with spam filters.
One thing to note is that many spam filters will count NOT having a text version against you if you are using a MIME format. You could add all of the headers and the text version yourself, or just let PHPMailer or the PEAR mail library take care of that for you. Having a text version may or may not help, but it is good practice and user friendly.
I realize that your code sample is just that - a sample, but it is worth saying: Do not ever just drop user provided data into your mail headers. Make sure you validate that it is data you expect. It is trivial to turn a php mail script into an open relay, and nobody wants that.
Check rfc 822 and rfc 2045 for email format. I find python's Email class really easy to work with. I assume php's PEAR does the same (according to earlier mails). Also the header and the body are separated by a "\r\n\r\n", not sure if your code automatically inserts that, but you can try appending that to the header.
I dont think that DK/SPF might be necessary (since there are lots of webservers out there without DK/SPF support). There can be alot of factors that might be causing it to get blocked(atleast 10K different criterions and methods.. p0f,greylisting,greylisting, blacklisting etc etc). Make sure that your email is properly formatted(this makes a BIG difference). Look into libraries that generate the complete header for you.. that way you have least chances of making any mistake.
Adding a SPF record is very easy. You should try.
This one is for dreamhost plus googlemail
You should also ad you webserver ip address (in my case, the line before googlemail)
The last line tells the server to do a soft reject (mark as spam but don't delete) I'm using it instead of "-" (delete) because google documentation says so :-)
It's a TXT record
v=spf1
ip4:64.111.100.0/24 ip4:66.33.201.0/24 ip4:66.33.216.0/24
ip4:208.97.132.0/24 ip4:208.97.187.0/24 ip4:208.113.200.0/24 ip4:208.113.244.0/24
ip4:208.97.132.74 ip4:67.205.36.71
include:aspmx.googlemail.com
mx ~all
Hope it helps