0

I have an Android HTML5 php application where I want to enable users to send emails to other people by using Gmail or Yahoo. This is the code that I use

<?php


$smtp=$_GET["smtp"];
$youremail= $_GET["youremail"];   
$emailpassword=$_GET["emailpassword"]; 
$companyemail=$_GET["companyemail"]; 
$messagetitle= $_GET["messagetitle"];
$messagetext=$_GET["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->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";

$mail->AddAddress("$emailChunks[$i]"); // Email on which you want to send mail

$mail->AddReplyTo("$emailpassword", "Reply to"); //optional

$mail->IsHTML(true);

$mail->Subject = "$messagetitle";

$mail->Body = "$messagetext";

if(!$mail->Send())
{
echo $mail->ErrorInfo;
}else{
echo "email was sent";

}

}


?>

Error that i get is: Invalid address: mysmtppassxxxxSMTP -> ERROR: Failed to connect to server: Connection timed out (110)


SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host

when I do a var_dump($email) I get

object(PHPMailer)#1 (53) {
  ["Priority"]=>
  int(3)
  ["CharSet"]=>
  string(10) "iso-8859-1"
  ["ContentType"]=>
  string(9) "text/html"
  ["Encoding"]=>
  string(4) "8bit"
  ["ErrorInfo"]=>
  string(0) ""
  ["From"]=>
  string(18) "me@gmail.com"
  ["FromName"]=>
  string(18) "me@gmail.com"
  ["Sender"]=>
  string(0) ""
  ["Subject"]=>
  string(4) "test"
  ["Body"]=>
  string(10) "my 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(18) "me@gmail.com"
  ["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) "jvkrneta@yahoo.com"
      [1]=>
      string(0) ""
    }
  }
  ["cc:private"]=>
  array(0) {
  }
  ["bcc:private"]=>
  array(0) {
  }
  ["ReplyTo:private"]=>
  array(1) {
    ["me@gmail.com"]=>
    array(2) {
      [0]=>
      string(18) "me@gmail.com"
      [1]=>
      string(8) "Reply to"
    }
  }
  ["all_recipients:private"]=>
  array(1) {
    ["joovkrneta@yahoo.com"]=>
    bool(true)
  }
  ["attachment:private"]=>
  array(0) {
  }
  ["CustomHeader:private"]=>
  array(0) {
  }
  ["message_type:private"]=>
  string(0) ""
  ["boundary:private"]=>
  array(0) {
  }
  ["language:protected"]=>
  array(0) {
  }
  ["error_count:private"]=>
  int(0)
  ["sign_cert_file:private"]=>
  string(0) ""
  ["sign_key_file:private"]=>
  string(0) ""
  ["sign_key_pass:private"]=>
  string(0) ""
  ["exceptions:private"]=>
  bool(false)
}
Jovo Krneta
  • 558
  • 1
  • 12
  • 33
  • You will need **$mail->SMTPSecure = "ssl";** for google for example. Some SMTP provider need ssl for access. worth a try. – Michael Mitch Sep 15 '13 at 00:18
  • I get the same error again! – Jovo Krneta Sep 15 '13 at 00:35
  • somehow if you try SSL on nonSSL server will timeout too. which server you are trying to connect to? could you make a dump of all variables **except user and password**? – Michael Mitch Sep 15 '13 at 00:36
  • I am using bluehost servers , I can send emails from php scripts without using SMTP but if I do so I will be in problems with great number of emails being send from my server. The data is sent ok to the php page , I have a list of email separated by "," I explode those and I send emails in a loop – Jovo Krneta Sep 15 '13 at 00:40
  • As Michael said, could you make a dump of all variables? The key ones talking about the protocols, and the addresses used. – Shade Sep 15 '13 at 01:03
  • I updated the question there is no my password in the var_dump!? – Jovo Krneta Sep 15 '13 at 01:17
  • is that a `var_dump()` of `$mail` or `$_GET` ? – Shade Sep 15 '13 at 02:11
  • it is a var_dump of $mail! – Jovo Krneta Sep 15 '13 at 10:20

1 Answers1

1

The $mail->AddAddress() section has invalid syntax.

Change it from $mail->AddAddress("$emailChunks[$i]"); to $mail->AddAddress("${emailChunks[$i]}");

Basically, the parser doesn't recognize that the subscript isn't part of the string.

This article explains it in more depth: Can I subscript an array variable inside a double quoted PHP string?

Community
  • 1
  • 1
Shade
  • 765
  • 3
  • 16
  • I get the same error after change SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
    SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host.
    – Jovo Krneta Sep 15 '13 at 00:34