Jump to content

How to send smtp mail


ryy705

Recommended Posts

Hello,

How can I send smtp mail?  Am I allowed to do it the following way? 

The current code generates the following error:

'Failed to connect to smtp.gmail.com:587 [sMTP: Failed to connect socket: Connection refused (code: -1, response: )]'

What am I doing wrong?  Thanks in advance.

 

<?php
include("Mail.php");
/* mail setup recipients, subject etc */
$recipients = "[email protected]";
$headers["From"] = "[email protected]";
$headers["To"] = "[email protected]";
$headers["Subject"] = "Test";
$mailmsg = "Hello, This is a test.";
$smtpinfo["host"] = "smtp.gmail.com";
$smtpinfo["port"] = "587";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "me";
$smtpinfo["password"] = "****";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail = $mail_object->send($recipients, $headers, $mailmsg);
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/111914-how-to-send-smtp-mail/
Share on other sites

What grlayouts means is that Gmail (assumed from code) is disallowing you to connect on the smtp port specified... check with Gmail again to see if your using the right username format (some like just "username" some want "[email protected]" ) and re-check to make certain that your attempting all this on the right port (587 may or may not be correct, I'm not sure).

 

If you still get errors, hit us back and we'll try again!

Any firewalls installed that could be blocking access?

 

Also, so your details would look like this:

 

include("Mail.php");
/* mail setup recipients, subject etc */
$recipients = "[email protected]";
$headers["From"] = "[email protected]";
$headers["To"] = "[email protected]";
$headers["Subject"] = "Test";
$mailmsg = "Hello, This is a test.";
$smtpinfo["host"] = "localhost;
$smtpinfo["port"] = "587";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "me";
$smtpinfo["password"] = "****";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail = $mail_object->send($recipients, $headers, $mailmsg);
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}

 

if the mail server is on your PC then it should be localhost as the host, else you have an essetially useless piece of software as your bypassing it.

I am putting up code on an actual server.  And its not working.

 

When I run the mail function on my computer it sends out smtp mails using my gmail account.  Because ssmtp is configured that way on my computer.  However that isn't the case when I run the code on the real server.  I need a way to connect to a particular email account from an actual server.  I hope I was able to clearify.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.