11

I have been looking for an answer and tried many things to this problem.

My script works fine on my webhost but when moving it to an other dedicated server the mail never gets delivered. Now i need to set the SMTP server but don't get it right.

Using Gmail apps btw. This is how the code looks like.

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("@",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','telephone','message');
    $required = array('name','email','telephone','message');

    $your_email = "xxx@example.com";
    $email_subject = "New Messag: ".$_POST['subject'];
    $email_content = "New message:\n";

    foreach($values as $key => $value){
      if(in_array($value,$required)){
        if ($key != 'subject' && $key != 'telephone') {
          if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
      }
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!'; 
    } else {
        echo 'ERROR!';
    }
}

$mail->Mailer = "smtp";  
$mail->Host = "ssl://smtp.gmail.com";  
$mail->Port = 465;  
$mail->SMTPAuth = true; // turn on SMTP authentication  
$mail->Username = "user@gmail.com"; // SMTP username  
$mail->Password = "password"; // SMTP password 

?>

So how do i set the SMTP settings right?

Kenster
  • 23,465
  • 21
  • 80
  • 106
inpbox
  • 129
  • 1
  • 1
  • 5
  • 2
    check out this a perfect working soultion http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – Vishwanath Dalvi May 23 '11 at 07:23

3 Answers3

8

Under Windows only: You may try to use ini_set() functionDocs for the SMTPDocs and smtp_portDocs settings:

ini_set('SMTP', 'mysmtphost'); 
ini_set('smtp_port', 25); 
hakre
  • 193,403
  • 52
  • 435
  • 836
heximal
  • 10,327
  • 5
  • 46
  • 69
2

Check out your php.ini, you can set these values there.

Here's the description in the php manual: http://php.net/manual/en/mail.configuration.php

If you want to use several different SMTP servers in your application, I recommend using a "bigger" mailing framework, p.e. Swiftmailer

Nick F
  • 9,781
  • 7
  • 75
  • 90
Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • I have the right settings there. But still dossent work. Its a simple form so i think php mail function is the best way to go this time. – inpbox May 23 '11 at 12:41
  • There are a few details involved in the SMTP handshakes (with/without authentication and such), php mail doesn't know them all. For simple tasks the build-in php mail is a good and fast way to solve issues at hand, but keep in mind it has its limits. – Bjoern May 23 '11 at 12:50
  • All right, but in my code on the last lines i have set the settings that is requierd by google. But since im using an external smtp server for send mail to the given email adress do i need to change something in php.ini maybe? – inpbox May 23 '11 at 13:04
  • 1
    In php.ini, the following parameters are of interest here: smtp_server, smtp_port, auth_username and auth_password. Maybe enable error_logfile and debug_logfile, they might give you additional informations of whats not right here. – Bjoern May 23 '11 at 13:16
0

Try from your dedicated server to telnet to smtp.gmail.com on port 465. It might be blocked by your internet provider

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72