send email in php works on one instance and not another - php

Hi I have used sendgrid to send emails in the past and am using it as a reset password on an app currently. I want to use it for another app also. My AWS has a new instance for the second app. I have copied the sendmail.ini from my first instance and the php.ini and I am using basically the same code for sending emails in my php files. The first is working, the second isn't. Code below:
$subject = "Don't be stuck for a password!" ;
$Emessage = wordwrap("<html><body>Please click the link below to reset your password. <br><br> <a href=http://xxx.xxx.xx/resetPassword.php?AuthCode=".$Token."&email=".$email."&userid=".$userid." />Reset Password</a><br><br> Thanks, <br></body></html>", 70, "\r\n");
$from= "xxx#xxx.com";
$from_name="xxx Ltd.";
$headers = "Date: ". date("r") ."\r\n". "From: $from_name <$from>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//echo $message;
$mailSent= mail($email, $subject, $Emessage, $headers);
if (!$mailSent){
//$reset= error_get_last();
$reset='Not OK';
}
Note the mailSent is returning true. It must be some config issue. Also, I am running the same version of xampp on both instances.
My sendmail.ini is as follow:
[sendmail]
smtp_server=smtp.sendgrid.net
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=xxx
auth_password=yyy
force_sender=xxx#xxx.com
and my php.ini has the following in the [mail function]:
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" - t"

Since the same code is working on one instance and not the other, if we assume the instances are exactly the same, it must be external to your instance. AWS has an initial outbound email limit, so it's possible you're hitting that, but it's been lifted on your other instance.
One way to get around this, is to send via SendGrid's Web API (which is not limited on AWS [as it sends over port 80]).
You won't be able to send using the mail function, however, we have a PHP Library to send email that makes it easy enough to do.

Turns out I made the rookie mistake of not restarting my Apache! That was all that was wrong!

Related

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..

Configuring PHP to Function with SMTP

I have a register script that sends an email to who ever signs up.
//// Set headers ////
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "Content-type: textrn";
$headers .= "From: $from";
/// Send the email now ////
mail($email1, $subject, $message, $headers, '-f nleachman#stayontrack.net');
//mail($email1, $subject, $message, $headers, '-f noreply#your-email.com');
$db->commit();
echo "Thanks for joining! Check your email in a few moments to activate your account so that you may log in. See you on the site!";
$db = null;
exit();
}
catch(PDOException $e){
$db->rollBack();
echo $e->getMessage();
$db = null;
exit();
}
Here is my SMTP server used for configuration:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = nleachman#stayontrack.net
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
When ever I try to run my log-in script I get this:
Thanks for joining! Check your email in a few moments to activate your account so that you may log in. See you on the site!
My problem is that who ever signs - up never recieves the email. At first I thought that it may just be an impossible function on localhost. Then however, I saw some codes that worked for other people but gave me an error like...
Variable $From not set
How could I configure my code so that it runs without errors?
Also, how could I configure the code to make sure who ever the email is sent to recieves it?
For using SMTP within PHP, you should have a look at phpmailer. https://github.com/Synchro/PHPMailer
easy to configure and does all the work for you.
You're using mail() to send the email - I've had problems with this before not sending to certain domains like hotmail. I'd give PEAR mail a go - http://pear.php.net/package/Mail/redirected.

PHP Mail sending to external e-mail addresses but not internal

Started off with e-mails not being sent at all, but then I used the "-f" parameter in the function, which then works to send to external addresses (Gmail and Hotmail tested so far) but it won't work for addresses that are on the domain though. Just wondering if it's in the code or is it a problem with the server setup?
if ($Valid == 1) {
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('stripslashes', $_POST);
$To = "user#domain.ca";
$Subject = "Online Driver Application";
$Body = "All the values of the form that was filled out (removed because there was a lot and it doesn't affect the problem)";
$Headers = 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Headers .= 'From: Company <info#domain.ca>' . "\r\n";
$Headers .= 'Reply-To: no-reply#domain.ca' . "\r\n";
$Headers .= 'X-Mailer: PHP/' . phpversion();
mail($To, $Subject, $Body, $Headers, -finfo#domain.ca);
echo '<div class="success">Thank You! Your form has been successfully submitted.</div>';
} else {
if ($ErrorMsg != '') {
echo '<div class="error">'.$ErrorMsg.'</div>';
}
Again, unless I have the -finfo#domain.ca in the mail function, e-mails don't get sent out at all.
Thanks.
The additional_parameters parameter (-f or -r) can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.
The additional_parameters(> 4.2.2) is disabled in safe_mode and the mail() function will expose a warning message and return FALSE when used.
The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.
(or, remove sendmail and install postfix (much easier) if still in problem.)
The code seems OK. This is probably something not setup correctly (like PHP.ini not having the SMTP server or a Firewall blocking something). Also, make sure newlines are "\n" in $Body I've seen some mail servers rejecting mails because of this. Using mail directly is always tricky. Try to use a library like Swiftmailer or PHPMailer instead. This way you don't have to worry about how to send HTML, attachments...

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";

Send mail through mail relay with PHP

I ran into a wee issue when using mail(). I wasn't able to send to addresses off the domain I was hosting the form. I understand this is for security reasons but it makes the creation of a 'send to friend' system a little tough.
Here's what I had working (albeit it only sent to my address):
<?php
$senderName = $_POST['name'];
$friendsEmail = $_POST['friendsEmail'];
if ($_POST['formName'] == 'refer') {
$to = $friendsEmail;
$subject = "$senderName has referred you";
$message = "Message goes here";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: Me <no-reply#test.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
header("Location: referConfirm.html");
?>
I've talked to my hosting company who setup a mail relay (yay!). Trouble is, I have no idea how to get what I working above through a mail relay. I have the following details:
IP: 000.000.000.000
Domain: domain.company.com
UN: username
PW: password
(Details are dummy.)
Can anyone give me a clue?
Thanks,
#rrfive
mail() uses the smtp/sendmail settings found in php.ini. If you need to send it via another smtp, or one with authentication (like in your example) mail is simply not enough.
There are good mailer libraries out there, just to name a few:
Swift Mailer
Zend_Mail
PHPMailer
They are all capable of sending emails via an authenticated smtp server.

Categories