Jump to content

Using the mail() function doesn't work


Tzahi_ben_artzi
 Share

Recommended Posts

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 by Tzahi_ben_artzi
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!";
}
?>
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.