php отправлять электронную почту с помощью gmail smtp

Я пытаюсь отправить электронную почту с помощью Gmail (возможно, даже SMTP Yahoo), у меня есть следующий код

require("class.phpmailer.php"); //ini_set("SMTP","smtp.google.com" ); $smtp=$_GET["smtp"]; $youremail= $_GET["youremail"]; $emailpassword=$_GET["emailpassword"]; $companyemail=$_GET["companyemail"]; $messagetitle= $_GET["messagetitle"]; $messagetext=$_GET["messagetext"]; echo "_GET variables dump" ; var_dump($smtp); var_dump($youremail); var_dump($emailpassword); var_dump($companyemail); var_dump($messagetitle); var_dump($messagetext); //this is a path to PHP mailer class you have dowloaded //include("class.phpmailer.php"); $emailChunks = explode(",", $companyemail); for($i = 0; $i < count($emailChunks); $i++){ // echo "Piece $i = <br />"; $mail = new PHPMailer(); $mail->IsSMTP(); $mail->SMTPDebug = 1; // errors and messages //$mail->SMTPSecure = "tls"; // sets the prefix to the servier $mail->SMTPSecure = "ssl"; $mail->Port = 587; $mail->Host = $smtp; $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $youremail; // SMTP username $mail->Password = $emailpassword; // SMTP password $mail->From = $youremail; //do NOT fake header. $mail->FromName = $youremail; $adr=$emailChunks[$i]; $mail->AddAddress($adr); // Email on which you want to send mail $mail->AddReplyTo($emailpassword, "Reply to"); //optional $mail->IsHTML(true); $mail->Subject = $messagetitle; $mail->Body = $messagetext; echo "mail variable dump" ; var_dump($mail); if(!$mail->Send()) { echo $mail->ErrorInfo; }else{ echo "email was sent"; } } 

Когда я вызываю код – я использую var_dump для цели отладки, которую я получаю

 ALL EMAIL MESSAGES HAVE BEEN WITH STATUS :_GET variables dumpstring(14) "smtp.gmail.com" string(18) "me@gmail.com" string(8) "mypass" string(18) "sendTo@yahoo.com" string(2) "message subject" string(6) "message" Invalid address: mypassmail variable dumpobject(PHPMailer)#1 (53) { ["Priority"]=> int(3) ["CharSet"]=> string(10) "iso-8859-1" ["ContentType"]=> string(9) "text/html" ["Encoding"]=> string(4) "8bit" ["ErrorInfo"]=> string(25) "Invalid address: mypass" ["From"]=> string(18) "me@gmail.com" ["FromName"]=> string(18) "me@gmail.com" ["Sender"]=> string(0) "" ["Subject"]=> string(2) "ja" ["Body"]=> string(6) "message" ["AltBody"]=> string(0) "" ["WordWrap"]=> int(0) ["Mailer"]=> string(4) "smtp" ["Sendmail"]=> string(18) "/usr/sbin/sendmail" ["PluginDir"]=> string(0) "" ["ConfirmReadingTo"]=> string(0) "" ["Hostname"]=> string(0) "" ["MessageID"]=> string(0) "" ["Host"]=> string(14) "smtp.gmail.com" ["Port"]=> int(587) ["Helo"]=> string(0) "" ["SMTPSecure"]=> string(3) "ssl" ["SMTPAuth"]=> bool(true) ["Username"]=> string(18) "me@gmail.com" ["Password"]=> string(8) "mypass" ["Timeout"]=> int(10) ["SMTPDebug"]=> int(1) ["SMTPKeepAlive"]=> bool(false) ["SingleTo"]=> bool(false) ["SingleToArray"]=> array(0) { } ["LE"]=> string(1) " " ["DKIM_selector"]=> string(9) "phpmailer" ["DKIM_identity"]=> string(0) "" ["DKIM_domain"]=> string(0) "" ["DKIM_private"]=> string(0) "" ["action_function"]=> string(0) "" ["Version"]=> string(3) "5.1" ["smtp:private"]=> NULL ["to:private"]=> array(1) { [0]=> array(2) { [0]=> string(18) "sendTo@yahoo.com" [1]=> string(0) "" } } ["cc:private"]=> array(0) { } ["bcc:private"]=> array(0) { } ["ReplyTo:private"]=> array(0) { } ["all_recipients:private"]=> array(1) { ["sendTo@yahoo.com"]=> bool(true) } ["attachment:private"]=> array(0) { } ["CustomHeader:private"]=> array(0) { } ["message_type:private"]=> string(0) "" ["boundary:private"]=> array(0) { } ["language:protected"]=> array(17) { ["provide_address"]=> string(54) "You must provide at least one recipient email address." ["mailer_not_supported"]=> string(25) " mailer is not supported." ["execute"]=> string(19) "Could not execute: " ["instantiate"]=> string(36) "Could not instantiate mail function." ["authenticate"]=> string(35) "SMTP Error: Could not authenticate." ["from_failed"]=> string(35) "The following From address failed: " ["recipients_failed"]=> string(45) "SMTP Error: The following recipients failed: " ["data_not_accepted"]=> string(30) "SMTP Error: Data not accepted." ["connect_host"]=> string(43) "SMTP Error: Could not connect to SMTP host." ["file_access"]=> string(23) "Could not access file: " ["file_open"]=> string(33) "File Error: Could not open file: " ["encoding"]=> string(18) "Unknown encoding: " ["signing"]=> string(15) "Signing Error: " ["smtp_error"]=> string(19) "SMTP server error: " ["empty_message"]=> string(18) "Message body empty" ["invalid_address"]=> string(15) "Invalid address" ["variable_set"]=> string(30) "Cannot set or reset variable: " } ["error_count:private"]=> int(1) ["sign_cert_file:private"]=> string(0) "" ["sign_key_file:private"]=> string(0) "" ["sign_key_pass:private"]=> string(0) "" ["exceptions:private"]=> bool(false) } SMTP -> ERROR: Failed to connect to server: Connection timed out (110) <br />SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host. , ALL EMAIL MESSAGES HAVE BEEN WITH STATUS :_GET variables dumpstring(14) "smtp.gmail.com" string(18) "me@gmail.com" string(8) "mypass" string(18) "sendTo@yahoo.com" string(2) "message subject" string(6) "message" Invalid address: mypassmail variable dumpobject(PHPMailer)#1 (53) { ["Priority"]=> int(3) ["CharSet"]=> string(10) "iso-8859-1" ["ContentType"]=> string(9) "text/html" ["Encoding"]=> string(4) "8bit" ["ErrorInfo"]=> string(25) "Invalid address: mypass" ["From"]=> string(18) "me@gmail.com" ["FromName"]=> string(18) "me@gmail.com" ["Sender"]=> string(0) "" ["Subject"]=> string(2) "ja" ["Body"]=> string(6) "message" ["AltBody"]=> string(0) "" ["WordWrap"]=> int(0) ["Mailer"]=> string(4) "smtp" ["Sendmail"]=> string(18) "/usr/sbin/sendmail" ["PluginDir"]=> string(0) "" ["ConfirmReadingTo"]=> string(0) "" ["Hostname"]=> string(0) "" ["MessageID"]=> string(0) "" ["Host"]=> string(14) "smtp.gmail.com" ["Port"]=> int(587) ["Helo"]=> string(0) "" ["SMTPSecure"]=> string(3) "ssl" ["SMTPAuth"]=> bool(true) ["Username"]=> string(18) "me@gmail.com" ["Password"]=> string(8) "mypass" ["Timeout"]=> int(10) ["SMTPDebug"]=> int(1) ["SMTPKeepAlive"]=> bool(false) ["SingleTo"]=> bool(false) ["SingleToArray"]=> array(0) { } ["LE"]=> string(1) " " ["DKIM_selector"]=> string(9) "phpmailer" ["DKIM_identity"]=> string(0) "" ["DKIM_domain"]=> string(0) "" ["DKIM_private"]=> string(0) "" ["action_function"]=> string(0) "" ["Version"]=> string(3) "5.1" ["smtp:private"]=> NULL ["to:private"]=> array(1) { [0]=> array(2) { [0]=> string(18) "sendTo@yahoo.com" [1]=> string(0) "" } } ["cc:private"]=> array(0) { } ["bcc:private"]=> array(0) { } ["ReplyTo:private"]=> array(0) { } ["all_recipients:private"]=> array(1) { ["sendTo@yahoo.com"]=> bool(true) } ["attachment:private"]=> array(0) { } ["CustomHeader:private"]=> array(0) { } ["message_type:private"]=> string(0) "" ["boundary:private"]=> array(0) { } ["language:protected"]=> array(17) { ["provide_address"]=> string(54) "You must provide at least one recipient email address." ["mailer_not_supported"]=> string(25) " mailer is not supported." ["execute"]=> string(19) "Could not execute: " ["instantiate"]=> string(36) "Could not instantiate mail function." ["authenticate"]=> string(35) "SMTP Error: Could not authenticate." ["from_failed"]=> string(35) "The following From address failed: " ["recipients_failed"]=> string(45) "SMTP Error: The following recipients failed: " ["data_not_accepted"]=> string(30) "SMTP Error: Data not accepted." ["connect_host"]=> string(43) "SMTP Error: Could not connect to SMTP host." ["file_access"]=> string(23) "Could not access file: " ["file_open"]=> string(33) "File Error: Could not open file: " ["encoding"]=> string(18) "Unknown encoding: " ["signing"]=> string(15) "Signing Error: " ["smtp_error"]=> string(19) "SMTP server error: " ["empty_message"]=> string(18) "Message body empty" ["invalid_address"]=> string(15) "Invalid address" ["variable_set"]=> string(30) "Cannot set or reset variable: " } ["error_count:private"]=> int(1) ["sign_cert_file:private"]=> string(0) "" ["sign_key_file:private"]=> string(0) "" ["sign_key_pass:private"]=> string(0) "" ["exceptions:private"]=> bool(false) } SMTP -> ERROR: Failed to connect to server: Connection timed out (110) <br />SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host. - ALL EMAIL MESSAGES HAVE BEEN WITH STATUS :_GET variables dumpstring(14) "smtp.gmail.com" string(18) "me@gmail.com" string(8) "mypass" string(18) "sendTo@yahoo.com" string(2) "message subject" string(6) "message" Invalid address: mypassmail variable dumpobject(PHPMailer)#1 (53) { ["Priority"]=> int(3) ["CharSet"]=> string(10) "iso-8859-1" ["ContentType"]=> string(9) "text/html" ["Encoding"]=> string(4) "8bit" ["ErrorInfo"]=> string(25) "Invalid address: mypass" ["From"]=> string(18) "me@gmail.com" ["FromName"]=> string(18) "me@gmail.com" ["Sender"]=> string(0) "" ["Subject"]=> string(2) "ja" ["Body"]=> string(6) "message" ["AltBody"]=> string(0) "" ["WordWrap"]=> int(0) ["Mailer"]=> string(4) "smtp" ["Sendmail"]=> string(18) "/usr/sbin/sendmail" ["PluginDir"]=> string(0) "" ["ConfirmReadingTo"]=> string(0) "" ["Hostname"]=> string(0) "" ["MessageID"]=> string(0) "" ["Host"]=> string(14) "smtp.gmail.com" ["Port"]=> int(587) ["Helo"]=> string(0) "" ["SMTPSecure"]=> string(3) "ssl" ["SMTPAuth"]=> bool(true) ["Username"]=> string(18) "me@gmail.com" ["Password"]=> string(8) "mypass" ["Timeout"]=> int(10) ["SMTPDebug"]=> int(1) ["SMTPKeepAlive"]=> bool(false) ["SingleTo"]=> bool(false) ["SingleToArray"]=> array(0) { } ["LE"]=> string(1) " " ["DKIM_selector"]=> string(9) "phpmailer" ["DKIM_identity"]=> string(0) "" ["DKIM_domain"]=> string(0) "" ["DKIM_private"]=> string(0) "" ["action_function"]=> string(0) "" ["Version"]=> string(3) "5.1" ["smtp:private"]=> NULL ["to:private"]=> array(1) { [0]=> array(2) { [0]=> string(18) "sendTo@yahoo.com" [1]=> string(0) "" } } ["cc:private"]=> array(0) { } ["bcc:private"]=> array(0) { } ["ReplyTo:private"]=> array(0) { } ["all_recipients:private"]=> array(1) { ["sendTo@yahoo.com"]=> bool(true) } ["attachment:private"]=> array(0) { } ["CustomHeader:private"]=> array(0) { } ["message_type:private"]=> string(0) "" ["boundary:private"]=> array(0) { } ["language:protected"]=> array(17) { ["provide_address"]=> string(54) "You must provide at least one recipient email address." ["mailer_not_supported"]=> string(25) " mailer is not supported." ["execute"]=> string(19) "Could not execute: " ["instantiate"]=> string(36) "Could not instantiate mail function." ["authenticate"]=> string(35) "SMTP Error: Could not authenticate." ["from_failed"]=> string(35) "The following From address failed: " ["recipients_failed"]=> string(45) "SMTP Error: The following recipients failed: " ["data_not_accepted"]=> string(30) "SMTP Error: Data not accepted." ["connect_host"]=> string(43) "SMTP Error: Could not connect to SMTP host." ["file_access"]=> string(23) "Could not access file: " ["file_open"]=> string(33) "File Error: Could not open file: " ["encoding"]=> string(18) "Unknown encoding: " ["signing"]=> string(15) "Signing Error: " ["smtp_error"]=> string(19) "SMTP server error: " ["empty_message"]=> string(18) "Message body empty" ["invalid_address"]=> string(15) "Invalid address" ["variable_set"]=> string(30) "Cannot set or reset variable: " } ["error_count:private"]=> int(1) ["sign_cert_file:private"]=> string(0) "" ["sign_key_file:private"]=> string(0) "" ["sign_key_pass:private"]=> string(0) "" ["exceptions:private"]=> bool(false) } SMTP -> ERROR: Failed to connect to server: Connection timed out (110) <br />SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host. 

И отправка по электронной почте не удалась!

Related of "php отправлять электронную почту с помощью gmail smtp"

Вы должны заменить значение SMTPSecure с SSL на TLS . Он разрешит вашу проблему подключиться к серверу и отправлена ​​почта. Я проверил ваш код, он работает нормально.

$mail->SMTPSecure = "tls"; // sets the prefix to the servier //$mail->SMTPSecure = "ssl";

Вторая замена – это ваш replayto email $mail->AddReplyTo($youremail, "Reply to"); //optional $mail->AddReplyTo($youremail, "Reply to"); //optional Вы $emailpassword что это неправильно.

 $mail->AddReplyTo($emailpassword, "Reply to") ????? 

Адрес для почты == mail пароль?