Tzahi_ben_artzi Posted October 2, 2014 Share Posted October 2, 2014 (edited) Hey guys. I am currently hosting my website on my own dedicated server, and i am trying to use the mail() function, but for some reason it doesn't send the email. This is my php code: the *** are real addresses. The from is admin@host.com <?php $to = "****"; $subject = "Test mail"; $message = "email message."; $from = "***"; $headers = "From:" . $from; mail($to,$subject,$message,$headers); echo "Mail Sent."; ?> Edited October 2, 2014 by Tzahi_ben_artzi Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/ Share on other sites More sharing options...
ginerjm Posted October 2, 2014 Share Posted October 2, 2014 One thing the manual suggests is that the from address match the domain from which this script is sending it. Also - try including a space in your header following 'from: ' PS - Since mail() and many other functions return a result value (Boolean in this case) you REALLY should be checking that result before simply echoing out a success message. Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492575 Share on other sites More sharing options...
jcbones Posted October 3, 2014 Share Posted October 3, 2014 I would suggest PHPmailer, as the mail function is not really suited for casual use. PHPmailer handles all of this. Just looking at all of the warnings, and notes on the mail() functions manual page, tells you that it is difficult to get it right 100% of the time. Especially of note is the one at the bottom: for this reason alone, just checking if it was accepted before saying "success" doesn't mean the message was sent. 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. Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492579 Share on other sites More sharing options...
Tzahi_ben_artzi Posted October 3, 2014 Author Share Posted October 3, 2014 I would suggest PHPmailer, as the mail function is not really suited for casual use. PHPmailer handles all of this. Just looking at all of the warnings, and notes on the mail() functions manual page, tells you that it is difficult to get it right 100% of the time. Especially of note is the one at the bottom: for this reason alone, just checking if it was accepted before saying "success" doesn't mean the message was sent. Hello. I am trying to use PHPMailer, but for some reason it doesn't print or do anything. THis is my code: <?php echo "yoo i got here already (first)"; #require '/etc/phpmail/phpmailer/phpmailerautoload.php'; echo "test"; $mail = new PHPMailer; $mail->isSendmail(); $mail->setFrom('@@@@', 'First Last'); $mail->addAddress('@@@@', 'John Doe'); $mail->Subject = 'PHPMailer sendmail test'; $mail->Body = 'This is a plain-text message body'; echo "yoo i got here already (second)"; if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?> If i comment the required, it'll echo the test, if i dont comment out the require, it wont echo test. I dont know whats the problem, and the link to the file is correct, tried it out my self. Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492583 Share on other sites More sharing options...
mac_gyver Posted October 3, 2014 Share Posted October 3, 2014 when debugging php code, you need to have php's error_reporting set to E_ALL and display_errors set to ON so that php will report and display all the errors it detects. you should have these settings in your php.ini so that they are easy to change in just one place and so that you don't have to remember to put them into code for debugging and remove them when you are done. Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492595 Share on other sites More sharing options...
Tzahi_ben_artzi Posted October 3, 2014 Author Share Posted October 3, 2014 when debugging php code, you need to have php's error_reporting set to E_ALL and display_errors set to ON so that php will report and display all the errors it detects. you should have these settings in your php.ini so that they are easy to change in just one place and so that you don't have to remember to put them into code for debugging and remove them when you are done. ok did that, anyways now it says that the message was sent, but i didn't get it to my folder or spam folder. Code: <?php require '/etc/phpmail/phpmailer/phpmailerautoload.php'; $mail = new PHPMailer; $mail->isSendmail(); $mail->setFrom('Admin@xroleplaycommunity.com', 'First Last'); $mail->addAddress('t@bena.co.il'); $mail->Subject = 'PHPMailer sendmail test'; $mail->Body = 'This is a plain-text message body'; if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492613 Share on other sites More sharing options...
mikosiko Posted October 3, 2014 Share Posted October 3, 2014 Hey guys. I am currently hosting my website on my own dedicated server...... and the obvious question is... do you have configured a local email server in that server or properly configured sendmail? ... otherwise you have to use a SMTP host to deliver your emails... PHP Mailer allows you to configure that... read the documentation Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492617 Share on other sites More sharing options...
Tzahi_ben_artzi Posted October 4, 2014 Author Share Posted October 4, 2014 and the obvious question is... do you have configured a local email server in that server or properly configured sendmail? ... otherwise you have to use a SMTP host to deliver your emails... PHP Mailer allows you to configure that... read the documentation I may be retarded, but i cant find the docs regarding creating the smtp server Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492729 Share on other sites More sharing options...
jcbones Posted October 4, 2014 Share Posted October 4, 2014 While PHPmailer is a great little application, it's documentation is something to be desired. $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "mail.yourdomain.com"; // SMTP server $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only $mail->SMTPAuth = true; // enable SMTP authentication $mail->Host = "mail.yourdomain.com"; // sets the SMTP server $mail->Port = 26; // set the SMTP port for the GMAIL server $mail->Username = "yourname@yourdomain"; // SMTP account username $mail->Password = "yourpassword"; // SMTP account password Quote Link to comment https://forums.phpfreaks.com/topic/291402-using-the-mail-function-doesnt-work/#findComment-1492742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.