Sending a mail using PHP
I have a piece of code which upon approval sends a mail to an email address. The email is not being sent. Do I need to configure something on my web server?
function mailpassword($email, $password){
//notify that the password has been changed
mysqli_select_db($connect,"members");
$query = "select email from users where email = '".$email."'";
$mailquery = mysqli_query($connect,$query);
if(!$mailquery)
{
throw new Exception ('The entered email address could not be found');
}
else if($mailquery->num_rows==0)
{
throw new Exception ('The entered email address could not be found');
//username not in database
}
//if no errors, se开发者_开发问答nd mail
else
{
$row = $mailquery->fetch_object();
$email = $row->email;
$from = "From : support@example.com \r\n";
$mesg = "Hey,\n\n You requested for a new password. We have generated a completely random password for you, use it to login.\n\n
New Password - ".$password."\r\n
Please change this random password to a password of your choice once you log in. To change your password, click on the Accounts tab present in your dashboard.\r\n
Cheers\r\n
Hap";
if(mail($email, 'Password Change Account Details', $mesg, $from))
{
return true;
echo "great";
}
else
{
echo "Something went wrong";
}
}
$mails = mailpassword();
}
There's an easy solution to email problems: use a hosted solution that makes sure all email sent from your application gets through. There are some alternatives like Amazon and Postmark. We use Postmark with great success (not affiliated though ehe;)
There's this PHP class for Postmark that I'm kinda proud of... :-)
This wasn't directly an answer to your question, but I hope it was helpful anyway!
Try to remove the space before the colon in the From header:
$from = "From: support@example.com \r\n"
精彩评论