Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting SMTP details for php mail () function [duplicate]

Tags:

php

email

smtp

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 = "[email protected]";
    $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 = "[email protected]"; // SMTP username  
$mail->Password = "password"; // SMTP password 

?>

So how do i set the SMTP settings right?

like image 517
inpbox Avatar asked May 23 '11 07:05

inpbox


People also ask

Does PHP mail function use SMTP?

PHP mailer uses Simple Mail Transmission Protocol (SMTP) to send mail. On a hosted server, the SMTP settings would have already been set. The SMTP mail settings can be configured from “php. ini” file in the PHP installation folder.

How send mail in PHP explain in detail?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );

What is PHP mail () function?

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.


1 Answers

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); 
like image 150
heximal Avatar answered Sep 27 '22 22:09

heximal