PHP 페이지에서 GMail의 SMTP 서버를 통해 이메일을 보내려고하지만이 오류가 발생합니다 :
인증 실패 [SMTP : SMTP 서버가 인증을 지원하지 않습니다 (코드 : 250, 응답 : mx.google.com, 서비스 : [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]
누구든지 도와 줄 수 있습니까? 여기 내 코드가 있습니다 :
<?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>");
}
?>
// 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>');
}
스위프트 메일러 를 사용하면 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);
?>
코드가 TLS (SSL) (TLS (SSL))를 사용하는 것으로 보이지 않습니다. 이는 { Google에 메일을 전달하는 데 필요합니다 (포트 465 또는 포트 587 사용 ).
당신은 이것을 설정함으로써 할 수 있습니다.
$Host = "ssl://smtp.gmail.com";
코드는 호스트 이름 체계에서 ssl : //을 참조하는 이 예제 와 같이 의심스러워 보입니다.
배 메일은 권장하지 않습니다. 2010 년 이후로 업데이트되지 않았습니다. 또한 소스 파일을 읽습니다. 소스 코드가 거의 구식이며 PHP 4 스타일로 작성되었으며 많은 오류/버그가 게시되었습니다 (Google it). 나는 Swift Mailer를 사용하고있다.
Swift Mailer는 PHP 5로 작성된 웹 응용 프로그램에 통합되어 다양한 기능을 갖춘 이메일을 전송할 때 유연하고 우아한 객체 지향 접근 방식을 제공합니다.
SMTP, sendmail, postfix 또는 사용자 지정 전송 구현을 사용하여 전자 메일을 보냅니다.
사용자 이름 및 암호 및/또는 암호화가 필요한 서버를 지원하십시오.
요청 데이터 컨텐츠를 제거하지 않고 헤더 주입 공격으로부터 보호합니다.
MIME 호환 HTML/multipart 이메일을 보냅니다.
이벤트 기반 플러그인을 사용하여 라이브러리를 사용자 정의하십시오.
메모리 사용량이 적은 대형 첨부 파일 및 인라인/포함 된 이미지 처리.
그것은 Swift Mailer 다운로드하여 서버에 업로드 할 수있는 무료 오픈 소스입니다. (기능 목록은 소유자 웹 사이트에서 복사됩니다.).
Gmail의 SSL/SMTP 및 신속 메일 서버의 실제 예가 여기 있습니다 ...
// 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. :(';
}
이게 도움이 되길 바란다. 해피 코딩 ... :)
<?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!";
}
?>
SwiftMailer 는 외부 서버를 사용하여 전자 메일을 보낼 수 있습니다.
다음은 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));
질문에 나열된 코드에는 두 가지 변경 사항이 필요합니다.
$Host = "ssl://smtp.gmail.com";
$port = "465";
SSL 연결에는 포트 465가 필요합니다.
Gmail을 통해 phpMailer 라이브러리를 사용하여 메일 보내기 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!";
}
Gmail은 포트 465가 필요하며 phpmailer의 코드이기도합니다. :)
Ubuntu에 PEAR의 Mail.php를 설치하려면 다음 명령을 실행하십시오 :
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
나는이 문제도 가지고 있었다. 올바른 설정을 지정하고 덜 안전한 응용 프로그램을 활성화했지만 여전히 작동하지 않았습니다. 마지막으로,이 https://accounts.google.com/UnlockCaptcha 를 활성화 했으므로 저에게 효과적이었습니다. 나는 이것이 누군가를 돕기를 바랍니다.
나는 "@ gmail.com"sufix를 가진 GSuite 계정에 대한 해결책을 가지고있다. 또한 @ gmail.com과 함께 GSuite 계정에서는 작동하지만 havent에서는 시도해 보았습니다. 먼저 GSuite 계정에 대해 "덜 안전한 응용 프로그램"옵션을 변경할 수있는 권한이 있어야합니다. 권한이있는 경우 (계정 설정 -> 보안을 확인할 수 있음) 보안 수준이 낮은 응용 프로그램을 허용하려면 "2 단계 인증"을 비활성화하고 페이지 끝으로 이동하여 "예"로 설정해야합니다. 그게 다야. 이러한 옵션을 변경할 수있는 권한이 없으면이 스레드에 대한 솔루션이 작동하지 않습니다. https://support.google.com/a/answer/6260879?hl=ko 를 선택하여 'less allow ...'옵션을 변경하십시오.