Answer the question
In order to leave comments, you need to log in
How to work with smtp.gmail.com?
Hello. Who will tell you how to work with smtp.gmail.com?
Before that, I used ssl://smtp.yandex.ru, but since access has now been closed, I can already use this method.
I use this script
function smtpmail($to='', $mail_to, $subject, $message, $headers='') {
$mft_config['smtp_username'] = 'username';
$mft_config['smtp_port'] = '465';
$mft_config['smtp_host'] = 'ssl://smtp.gmail.com';
$mft_config['smtp_password'] = 'password';
$mft_config['smtp_debug'] = true;
$mft_config['smtp_charset'] = 'utf-8';
$mft_config['smtp_from'] = 'Administrator';
$SEND = "Date: ".date("D, d M Y H:i:s") . " UT\r\n";
$SEND .= 'Subject: =?'.$mft_config['smtp_charset'].'?B?'.base64_encode($subject)."=?=\r\n";
if ($headers) $SEND .= $headers."\r\n\r\n";
else
{
$SEND .= "Reply-To: ".$mft_config['smtp_username']."\r\n";
$SEND .= "To: \"=?".$mft_config['smtp_charset']."?B?".base64_encode($to)."=?=\" <$mail_to>\r\n";
$SEND .= "MIME-Version: 1.0\r\n";
$SEND .= "Content-Type: text/html; charset=\"".$mft_config['smtp_charset']."\"\r\n";
$SEND .= "Content-Transfer-Encoding: 8bit\r\n";
$SEND .= "From: \"=?".$mft_config['smtp_charset']."?B?".base64_encode($mft_config['smtp_from'])."=?=\" <".$mft_config['smtp_username'].">\r\n";
$SEND .= "X-Priority: 3\r\n\r\n";
}
$SEND .= $message."\r\n";
if( !$socket = fsockopen($mft_config['smtp_host'], $mft_config['smtp_port'], $errno, $errstr, 30) ) {
if ($mft_config['smtp_debug']) echo $errno."<br>".$errstr;
return false;
}
if (!server_parse($socket, "220", __LINE__)) return false;
fputs($socket, "HELO " . $mft_config['smtp_host'] . "\r\n");
if (!server_parse($socket, "250", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Can`t send HELO!</p>';
fclose($socket);
return false;
}
fputs($socket, "AUTH LOGIN\r\n");
if (!server_parse($socket, "334", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Can`t find answer on request login.</p>';
fclose($socket);
return false;
}
fputs($socket, base64_encode($mft_config['smtp_username']) . "\r\n");
if (!server_parse($socket, "334", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Login wasn`t access by server!</p>';
fclose($socket);
return false;
}
fputs($socket, base64_encode($mft_config['smtp_password']) . "\r\n");
if (!server_parse($socket, "235", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Login wasn`t access by server like true! Error authentification!</p>';
fclose($socket);
return false;
}
fputs($socket, "MAIL FROM: <".$mft_config['smtp_username'].">\r\n");
if (!server_parse($socket, "250", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Can`t send MAIL FROM: </p>';
fclose($socket);
return false;
}
fputs($socket, "RCPT TO: <" . $mail_to . ">\r\n");
if (!server_parse($socket, "250", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Can`t send RCPT TO: </p>';
fclose($socket);
return false;
}
fputs($socket, "DATA\r\n");
if (!server_parse($socket, "354", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Can`t send DATA</p>';
fclose($socket);
return false;
}
fputs($socket, $SEND."\r\n.\r\n");
if (!server_parse($socket, "250", __LINE__)) {
if ($mft_config['smtp_debug']) echo '<p>Can`t send letter body. Letter wasn`t send!</p>';
fclose($socket);
return false;
}
fputs($socket, "QUIT\r\n");
fclose($socket);
return TRUE;
}
function server_parse($socket, $response, $line = __LINE__) {
$mft_config['smtp_username'] = 'username';
$mft_config['smtp_port'] = '465';
$mft_config['smtp_host'] = 'ssl://smtp.gmail.com';
$mft_config['smtp_password'] = 'password';
$mft_config['smtp_debug'] = true;
$mft_config['smtp_charset'] = 'utf-8';
$mft_config['smtp_from'] = 'Скидка 10%';
while (@substr($server_response, 3, 1) != ' ') {
if (!($server_response = fgets($socket, 256))) {
if ($mft_config['smtp_debug']) echo "<p>Trable with send letter!</p>$response<br>$line<br>";
return false;
}
}
if (!(substr($server_response, 0, 3) == $response)) {
if ($mft_config['smtp_debug']) echo "<p>Trable with send letter!</p>$response<br>$line<br>";
return false;
}
return true;
}
$message = 'Заявка на скидку 10%';
smtpmail('<email>', 'email', 'Скидка 10%', $message, 'utf-8');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question