Im using the following code to send a message:
try
{
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
$smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);
$smtp->setUsername("username");
$smtp->setpassword("password");
$swift =& new Swift($smtp);
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
$message =& new Swift_Message("message title");
$message->attach(new Swift_Message_Part("Hello"));
//Try sending the email
$sent = $swift->send($message, "$myEmail", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
if($sent)
{
print 'sent';
}
else
{
print 'not sent';
}
}
catch (Exception $e)
{
echo"$e";
}
The issue is that it is working fine on my local server (which my xampp server) but not working when the file is uploaded on the real server.
It throwing this error:
'The SMTP connection failed to start [mail.somedomain.net:587]: fsockopen returned Error Number 110 and Error String 'Connection timed out''
Please what should I do to correct this error. Thanks for reading
Make sure that the smtp server domain is valid. Trying pinging it to confirm a response. You may also try trace route to see if any of the switches are returning slow responses.
$smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);
is that '587' the port number to connect to? Any reason you're trying that instead of the normal port 25? Port 587 (submission) is normally used for local users to send mail. Once you're running this script on your remote web server, it's no longer "local", and is most likely firewalled off (or the mail server's not listening to that port on external interfaces).
Try switching to port 25 and see if that helps any.
update:
Connection refused is better than "connection timed out". It means at least that the initial data packet got somewhere and was actively refused. Timed out means things just got dropped silently somewhere en-route.
max_execution_time would only come into play if the php script itself went over the max time. If that was the case, you wouldn't be getting a swiftmailer error, because the script would have simply terminated.
Is your webserver running sendmail? Change the connection host to 'localhost' and see if that helps. If you just want to send an email, then that should work. The only reason you might want to connect to a remote SMTP server is for the From: headers to be correctly set and not be possibly flagged as SPAM on the receving end.
Related
I have this PHP code using the PHPMailer library
$email = new PHPMailer();
$email->IsSMTP();
$email->SMTPAuth = $Email_SMTPAuth;
$email->Host = $Email_Host;
$email->Port = $Email_Port;
$email->Username = $Email_Username;
$email->Password = $Email_Password;
$email->setFrom($result["emailfrom"], $result["fromname"]);
$email->Subject = stripslashes($result["subject"]);
$email->Body = stripslashes($result["message"]);
$email->AddAddress("email#domain.com");
if(!$email->Send()) {
echo $email->ErrorInfo;
} else {
echo 'sent';
}
When it fails to send, it show the error message:
The following From address failed: *EMAIL_ADDRESS_HERE* : Called Mail() without being connected
I believe it can be something to do with your host and port configuration, PHP Mailer might not be able to connect to your server with the settings you provided, thus giving you this error. Try checking again the configuration you set up, the host, port and username with your host and see if everything's all right.
Here is something similar to the error you're getting. Hope it helps :)
It happens frequently to me... when I forget to ask the provider to open a port to the host.
I have several sites hosted by a provider, and a DEM application running on the server. Depending on the provider, the server can be secured so that you are not allowed to connect freely to any SMTP host/port, but you have to be enabled to.
I am trying to send mail via mandrill app using swift mailer. This is my code:
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com',587);
$transport->setUsername($username);
$transport->setPassword($password);
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('New Order '.$reservationNumber)
// Set the From address with an associative array
->setFrom(array('noreply#domain.com' => 'domain.com'))
// Set the To addresses with an associative array
->setTo('test#domain.com')
// Give it a body
->setBody($body,'text/html');
$mailer->send($message);
Credentials are 100% good. And i get timeout error: Connection could not be established with host smtp.mandrillapp.com [Connection timed out #110].
It looks like something is blocking connection. Maybe this is issue with server configurations? We are using WHM software on our centos server
I've just been doing battle with exactly the same problem, but with smtp.gmail.com. It just would not work, even though the username/password etc were all correct.
In my case it seems that when PHP tries to connect to smtp.gmail.com, it gets the IPv6 address back - but their server seems to not be listening on that, since the Swiftmailer responds with the same timeout error you get.
But when I swapped in its IPv4 address (got by ping-ing it), it connected and sent the email just fine.
So find out what smtp.mandrillapp.com's IPv4 address is and try that IP in place of the hostname in the code. Does that connect and send now? It did for me.
It's not ideal coding in an IP address - given that they could change it at any minute - but at least you will get some emails sent
Here it actually worked when I increased the timeout:
$transport = Swift_SmtpTransport::newInstance('mail_server', 'mail_port', 'tls')
->setUsername('mail_user')
->setPassword('mail_pass')
->setTimeout(120)
;
The server I tried to access works with IPv6, maybe there is an issue with related to that.
I have seen similar questions here but , I am not beign able solve my problem .
I am trying to send mail using php... but this is not working .
<?php
$email_from="admin#crorebook.com";
ini_set("sendmail_from", $email_from);
$headers = "From: $email_from";
mail('gitudrebel94#gmail.com','Registration confirmation','Hihihihihihih','$headers');
?>
It gives me the following error on a windows server :
"SMTP server response: 550 No such user here in"
And following error on a nginx server:
"The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request."
Do you have your outgoing mail server setup? If you're running in windows you need an SMTP server, if linux you need sendmail or similar configured and running locally.
The SMTP error message suggests it's NOT an open forwarder - as in it won't just let anyone ask it to send an email to someone/somewhere else... that's good, because spammers would use it if it was. It probably requires some kind of authentication (your username/password) before it will except email for delivery to anyone but those local to the machine (as in email addresses of domains hosted on the machine).
Unfortunately the PHP mail() method doesn't handle that so you need to look at a third party package - PEAR mail, or PHPMailer are good options.
To complete the task with PHPMailer:
require_once /*lib location*/"phpmailer/class.phpmailer.php";
require_once /*lib location*/"phpmailer/class.smtp.php";
$mail=new PHPMailer();
$mail->IsSMTP();
$mail->Host=/*Your host*/;
$mail->Port=465;//For SSL - use this if you can
$mail->SMTPAuth=true;
$mail->SMTPSecure="ssl";
$mail->SMTPDebug=2;//Comment once it works, but the debug info is invaluable
$mail->Username=/*Your username*/;
$mail->Password=/*Your password*/;
$mail->setFrom("admin#crorebook.com");
$mail->Subject='Registration confirmation';
$mail->Body='Hihihihihihih';
$mail->AddAddress('gitudrebel94#gmail.com');
$mail->Send();
getting following error:
SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
SMTP Error: Could not connect to SMTP host. There was a problem sending this mail!
This is my config file setting as I have followed this PHPMailer tutorial
// Configuration settings for My Site
// Email Settings
$site['from_name'] = 'Manish Dekavadiya'; // from email name
$site['from_email'] = 'manish#<my-domain>.com'; // from email address
// Just in case we need to relay to a different server,
// provide an option to use external mail server.
$site['smtp_mode'] = 'enabled'; // enabled or disabled
$site['smtp_host'] = "smtp.<my-domain>.com";
$site['smtp_port'] = 587;
$site['smtp_username'] = "manish#<my-domain>.com";
$site['smtp_password']="<password>";
and used mailer class and an extended class as mentioned in tutorial as following:
/*****sendmail.php****/
// Grab our config settings
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
// Grab the FreakMailer class
//echo $_SERVER['DOCUMENT_ROOT'];
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc');
// instantiate the class
$mailer = new FreakMailer();
// Set the subject
$mailer->Subject = 'This is a test';
$mailer->SMTPDebug = 1;
// Body
$mailer->Body = 'This is a test of my mail system!';
// Add an address to send to.
$mailer->AddAddress('manish.dekavadiya#gmail.com', 'Manish Dekavadiya');
if(!$mailer->Send())
{
echo 'There was a problem sending this mail!';
}
else
{
echo 'Mail sent!';
}
$mailer->ClearAddresses();
$mailer->ClearAttachments();
also getting another error when I tried an example given in phpmailer docs # examples/test_smtp_gmail_basic.php
SMTP -> ERROR: Failed to connect toserver: php_network_getaddresses:
getaddrinfo failed: No such host is
known. (0) SMTP Error: Could not
connect to SMTP host. There was a
problem sending this mail!
so there must be setting or configuration error. there can't be code error.
Is SMTP set up on ? And if so is it configured to listen to smtp..com on port 587? If the server is not set up by yourself it's not uncommon that they listen to mail..com instead. Also, try to connect to port 25 to see if it might be configured to listen to the default smtp port.
The error messages is in any case very clear. The host is not responding to your connection attempt. The reason could be missconfigurations in both the server and in PHP, firewall issues, routing issues, DNS issues etc.
Hopefully this helps someone else out there because I was fighting this same error. First I was getting. I was getting an error that said it couldn't connect(). Then I turned on the debugging and got the error you mentioned. The solution that worked for me was to change the smtp..com to the ip address.
$mail->Host = 'smtp.whateverDomain.com';
to
$mail->Host = 'theIPaddress';
debugging
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
I had this problem a while ago, everything checked out just fine but the problem persisted. A reboot of httpd.service solved it.
Had this issue after restart of computer where docker container with apache webserver was running.
Apache server hosted php code which stopped sending emails after computer restart.
Restarting apache docker container solved the issue.
I'm trying to send an email using the Swift_SmtpTransport but I'm getting the following error:
501 5.5.2 <[::1]>: Helo command rejected: invalid ip address
The SMTP server is a remote server and it works from my production server, but not from my development machine, which is running OS X.
It also doesn't bother to throw an exception, instead it required me to use a logger plugin to find out why it wasn't working.
What can I do to make it use a real IP address?
I did some poking around in the code and found it.
When configuring the SMTP transport, you need to call setLocalDomain(). Using PHP on OS X, this defaults to "::1", which is rejected by the remote server. I've just added a line in my development configuration to set that:
$transport = Swift_SmtpTransport::newInstance('mail.pantsburger.com', 587);
if (SITE_ENV == SITE_ENV_DEV) {
$transport->setLocalDomain('[127.0.0.1]');
}
I think this is also a bug with Swiftmailer - it really should be throwing an exception for something like this, rather than just listing each recipient as "failed".