Wysyłanie wiadomości e-mail za pomocą serwera Gmail SMTP ze strony PHP

Próbuję wysłać e-mail przez serwer SMTP Gmaila ze strony PHP, ale pojawia się ten błąd:

Błąd uwierzytelniania [SMTP: serwer SMTP nie obsługuje uwierzytelniania (kod: 250, odpowiedź: mx.google.com do usług, [98.117.99.235] rozmiar 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]

Czy ktoś może pomóc? Oto Mój kod:
<?php
require_once "Mail.php";

$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "smtp.gmail.com";
$port = "587";
$username = "[email protected]";
$password = "testtest";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>
Author: skb, 2009-04-03

13 answers

// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}
 335
Author: pavan kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-14 02:06:39

Za pomocą Swift mailer, jest to dość łatwe do wysłania poczty przez poświadczenia Gmail:

<?php
require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername('GMAIL_USERNAME')
  ->setPassword('GMAIL_PASSWORD');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test Subject')
  ->setFrom(array('[email protected]' => 'ABC'))
  ->setTo(array('[email protected]'))
  ->setBody('This is a test mail.');

$result = $mailer->send($message);
?>
 99
Author: shasi kanth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-15 05:34:08

Twój kod nie wydaje się używać TLS (SSL), co jest niezbędne do dostarczania poczty do Google (i przy użyciu portów 465 lub 587) .

Możesz to zrobić, ustawiając

$host = "ssl://smtp.gmail.com";

Twój kod wygląda podejrzanie jak Ten przykład , który odnosi się do ssl: / / w schemacie nazwy hosta.

 53
Author: crb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-03 03:00:15

Nie polecam Pear Mail. Nie jest aktualizowana od 2010 roku. Przeczytaj również pliki źródłowe; kod źródłowy jest prawie Nieaktualny, napisany w stylu PHP 4 i wiele błędów / błędów zostało zamieszczonych (Google it). Używam Swift Mailera.

Swift Mailer integruje się z dowolną aplikacją internetową napisaną w PHP 5, oferując elastyczne i eleganckie podejście obiektowe do wysyłania wiadomości e-mail z wieloma funkcjami.

Wysyłanie e-maili za pomocą SMTP, sendmail, postfix lub Transport na zamówienie wdrożenie własnego.

Obsługa serwerów, które wymagają nazwy użytkownika i hasła i / lub szyfrowania.

Ochrona przed atakami header injection bez usuwania danych żądania treść.

Wysyłanie zgodnych z MIME wiadomości HTML/wieloczęściowych.

Użyj wtyczek sterowanych zdarzeniami, aby dostosować bibliotekę.

Obsługa dużych załączników i wbudowanych obrazów z niską pamięcią użyj.

Jest to wolne i otwarte źródło, które możesz Pobierz Swift Mailer i przesłać na serwer. (Lista funkcji jest kopiowana ze strony właściciela).

Roboczy przykład Gmail SSL / SMTP i Swift Mailer jest tutaj...

// Swift Mailer Library
require_once '../path/to/lib/swift_required.php';

// Mail Transport
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
    ->setUsername('[email protected]') // Your Gmail Username
    ->setPassword('my_secure_gmail_password'); // Your Gmail Password

// Mailer
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject Here')
    ->setFrom(array('[email protected]' => 'Sender Name')) // can be $_POST['email'] etc...
    ->setTo(array('[email protected]' => 'Receiver Name')) // your email / multiple supported.
    ->setBody('Here is the <strong>message</strong> itself. It can be text or <h1>HTML</h1>.', 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo 'Mail sent successfully.';
} else {
    echo 'I am sure, your configuration are not correct. :(';
}
Mam nadzieję, że to pomoże. Szczęśliwego kodowania... :)
 32
Author: Madan Sapkota,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-09 00:37:44
<?php
date_default_timezone_set('America/Toronto');

require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = "gdssdh";
//$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "[email protected]";  // GMAIL username
$mail->Password   = "password";            // GMAIL password

$mail->SetFrom('[email protected]', 'PRSPS');

//$mail->AddReplyTo("[email protected]', 'First Last");

$mail->Subject    = "PRSPS password";

//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "[email protected]";
$mail->AddAddress($address, "user2");

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>
 28
Author: Deept Raghav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-23 07:24:35

SwiftMailer może wysyłać wiadomości e-Mail za pomocą zewnętrznych serwerów.

Oto przykład, który pokazuje, jak korzystać z serwera Gmail:

require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

//Connect to localhost on port 25
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));


//Connect to an IP address on a non-standard port
$swift =& new Swift(new Swift_Connection_SMTP("217.147.94.117", 419));


//Connect to Gmail (PHP5)
$swift = new Swift(new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
 20
Author: Pekka 웃,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-20 08:17:08

Kod wymieniony w pytaniu wymaga dwóch zmian

$host = "ssl://smtp.gmail.com";
$port = "465";

Port 465 jest wymagany do połączenia SSL.

 13
Author: s01ipsist,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-16 01:50:49

Wysyłanie poczty za pomocą biblioteki phpMailer przez Gmail Proszę nie pobierać plików bibliotecznych z Github

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
 5
Author: Bhavin Solanki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-26 10:17:58

Gmail wymaga portu 465, A do tego jest to kod z phpmailera:)

 4
Author: Peter Mortensen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-09 00:26:56

Też miałem ten problem. Ustawiłem odpowiednie ustawienia i włączyłem mniej bezpieczne aplikacje, ale nadal nie działało. W końcu włączyłem to https://accounts.google.com/UnlockCaptcha i zadziałało na mnie. Mam nadzieję, że to komuś pomoże.

 3
Author: Strategist,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-25 10:56:37

Aby zainstalować Pear ' s Mail.php w Ubuntu uruchom następujący zestaw komend:

    sudo apt-get install php-pear
    sudo pear install mail
    sudo pear install Net_SMTP
    sudo pear install Auth_SASL
    sudo pear install mail_mime
 2
Author: Nahid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-11 11:08:08

Mam rozwiązanie dla kont GSuite, które nie ma "@gmail.com " sufix. Również myślę, że będzie to działać dla kont GSuite z @gmail.com ale nie próbowałem. Najpierw powinieneś mieć uprawnienia do zmiany opcji "allos¿w mniej bezpiecznej aplikacji" dla Twojego konta GSuite. Jeśli masz uprawnienia (możesz sprawdzić w ustawieniach konta- > bezpieczeństwo), musisz dezaktywować "uwierzytelnianie dwuetapowe" Przejdź na koniec strony i ustaw na " tak " dla Zezwalaj na mniej bezpieczne aplikacje. To wszystko. Jeśli nie masz uprawnień do zmiany tych opcji, rozwiązanie dla tego wątku nie będzie działać. Sprawdź https://support.google.com/a/answer/6260879?hl=en aby wprowadzić zmiany do "Zezwalaj na mniej..."opcja.

 0
Author: Diego Andrés Díaz Espinoza,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-01 15:20:46

Zestaw

'auth' => false,

Sprawdź też, czy działa port 25.

 -2
Author: CookieOfFortune,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-03 02:57:06