In VB.net this works:

        client.Host = "smtp.gMAIL.com"
        client.Credentials = New System.Net.NetworkCredential("b040Mail2@gmail.com", "*****")
        client.Port = 587
        client.EnableSsl = True

I'm trying this in php (from Xampp) but it gives me an error:

            ini_set("SMTP","ssl://smtp.gmail.com");
            ini_set("smtp_port","587");
            ini_set("username","b040Mail2@gmail.com"); 
            ini_set("password","*****"); 
            $to = "p806Antwerp@gmail.com";
            $subject = "testing";
            $message = "This is the message";
            mail($to,$subject,$message).

Is there anything here I should do, that I'm not doing?

Thank you very much for your help.

Kind regards,

Recommended Answers

All 9 Replies

PHP's mail() function is extremely simple, meant only for the most basic of SMTP servers, like the Unix sendmail. It doesn't support authentication, which GMail requires. - Bottom line is that if you are doing anything more complex than sending text mails, or maybe simple HTML emails, then you should be using a more extensive mailer library.

I'd recommed using either Swift Mailer or PHPMailer. Examples of how they can be used with GMail are all over the net. A Google search should get you plenty of examples. (The PHPMailer docs also include examples.)

Many thanks for you suggestions. I tried PHPMailer and came up with this, but still no joy. (I did not change anything in class.phpmailer.php)

<?php include "class.phpmailer.php";
    $mail = new PHPMailer;
    $mail -> Host = 'smtp.gmail.com:587';
    $mail -> SMTPAuth = TRUE;
    $mail -> Username = 'xxx@gmail.com';
    $mail -> Password = 'xxx';
    $mail -> SMTPSecure = 'ssl';
    $mail -> from = 'xxx@gmail.com';
    $mail -> fromname = 'p0110';
    $mail -> AddAddress('xxx@gmail.com');
    $mail -> Subject = 'this is the subject';
    $mail -> Body = 'this is the body';
    if (!$mail->send()){
        echo 'Message could not be sent. ' . '<br/>';
        echo 'Mailer error : ' . $mail -> ErrorInfo;
        exit;
    };
    echo "Message has been sent";
?>

Please disregard previous. Now it returns "could not connect to smtp server". From what I saw in other threads, this should work so it must be a problem in my setting. Unless you find something obvious in my PHP code.

<?php include "class.phpmailer.php";
    $mail = new PHPMailer;
    $mail -> IsSMTP();
    $mail -> Host = 'smtp.gmail.com:587';
    $mail -> SMTPAuth = TRUE;
    $mail -> Username = 'b040Mail2@gmail.com';
    $mail -> Password = '*****';
    $mail -> SMTPSecure = 'ssl';
    $mail -> from = 'b040Mail2@gmail.com';
    $mail -> fromname = 'p0110';
    $mail -> AddAddress('p806Antwerp@gmail.com');
    $mail -> Subject = 'this is the subject';
    $mail -> Body = 'this is the body';
    if (!$mail->send()){
        echo 'Message could not be sent. ' . '<br/>';
        echo 'Mailer error : ' . $mail -> ErrorInfo;
        exit;
    };
    echo "Message has been sent";
?>`

I believe the problem in the second snippet is the SMTSecure value. Gmail requires TLS for it's SMTP severs; SSL is not enough.

Like I mentioned before, the PHPMailer docs have working examples of how to use Gmail with PHPMailer. See Advanced Example using Gmail.

Atli, thank you very much for your reply. I am using ssl in VB.Net though, and I did read the links that you gave.

cmps, many thanks. I had browsed through that post.

I am using ssl in VB.Net though

TLS is commonly referred to as SSL these days, even though SSL is actually a different protocol. The old-school SSL protocol is just that rarely used for this purpose anymore that it's more or less fair to assume "SSL" actually means TLS. (The STARTTLS SMTP extension, to be precise.)

In PHP, however, when "ssl" is specified as the protocol of a stream, and the OpenSSL extension (or something equivalent) exists to provide the old-school SSL protocol for streams, it is used at the transport level. That includes libraries like PHPMailer. This is different from the .NET client in that it doesn't even support the old-school SSL protocol for it's SmtpClient; it's simply not an option. - PHPMailer requires you to be more specific about which protocol you want to use because it supports both.

Atli, you are perfectly right. tls is ssl, I would not have come up with that one on my own :).

I set SMPTSecure to 'tls' and lo and behold, it works now.

Thank you so very very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.