I'm trying to send an email when a form is submitted. I'm using PHPMailer to send the mail using the below configuration.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'user#example.in';
$mail->Password = 'password';
$mail->setFrom("user#example.in" , "User");
$mail->addAddress('receiver#example.in', 'Receiver');
$mail->addBCC('anotheruser#somedomain.com', 'Another user');
$mail->AddReplyTo('user#example.in', 'User');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send())
echo "Your request has been received. We will soon contact you.";
else echo "Unable to send your request. Please try again";
This works fine in localhost. But, when I deploy it to my server (example.in) I get the below exception.
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
--EDIT--
I tried connecting to the SMTP server using telnet command, but I'm unable to add the recipient. I get the below error?
Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Escape character is '^]'.
220 fbs-ho-mailserver.example.in ESMTP Service (Lotus Domino Release 8.5.3FP3) ready at Fri, 16 Sep 2016 11:36:01 +0530
HELO example.in
250 fbs-ho-mailserver.example.in Hello example.in ([111.91.127.222]), pleased to meet you
MAIL from: marketing#example.in
250 marketing#example.in... Sender OK
RCPT to: john.hh#gmail.com
554 Relay rejected for policy reasons.
-- EDIT --
I was able to setup this account in outlook. I'm really confused what's happening.
Your error says:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
So let's look at https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting :
"SMTP Error: Could not connect to SMTP host."
This may also appear as SMTP connect() failed or Called Mail() without
being connected in debug output. This is often reported as a PHPMailer
problem, but it's almost always down to local DNS failure, firewall
blocking (for example as GoDaddy does) or other issue on your local
network. It means that PHPMailer is unable to contact the SMTP server
you have specified in the Host property, but doesn't say exactly why.
It can also be caused by not having the openssl extension loaded (See
encryption notes below).
Some techniques to diagnose the source of this error are discussed
below.
GoDaddy
Popular US hosting provider GoDaddy imposes very strict (to the point
of becoming almost useless) constraints on sending email. They block
outbound SMTP to ports 25, 465 and 587 to all servers except their
own. This problem is the subject of many frustrating questions on
Stack Overflow. If you find your script works on your local machine,
but not when you upload it to GoDaddy, this will be what's happening
to you. The solution is extremely poorly documented by GoDaddy: you
must send through their servers, and also disable all security
features, username and password (great, huh?!), giving you this config
for PHPMailer:
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
GoDaddy also refuses to send with a From address belonging to any aol,
gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This
is because all those domains deploy SPF and DKIM anti-forgery
measures, and faking your from address is forgery.
You may find it easier to switch to a more enlightened hosting
provider.
The problem - rather the two unrelated problems - that you're experiencing are quite straightforward:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
and you have verified that the server is indeed accepting connections:
I tried connecting to the SMTP server using telnet command
Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Your script cannot connect to the SMTP server when run from its production server.
The likely cause is that the production server has a firewall that, to avoid abuse, prevents any connection to the outside. The server can serve Web requests, but no more.
If your test had verified that port 25 was not responding, then (after checking that the host address was correct) you could have tried telnet mail.example.in 587 instead. If that worked, it could have meant that the server is not accepting insecure connections (port 25) but is accepting secure connections. With PHPMailer you could then have tried activating secure connection:
$mail->SMTPSecure = 'tls';
or
$mail->SMTPSecure = 'ssl';
If that does not work, you might still have a firewall issue; or you might need to look at phpinfo() and verify you do have OpenSSL support available in PHP.
What you need to do
ask the IT people that maintain the production server to open the firewall;
more promisingly, ask them how to send emails from that server. Chances are that you need to use the mail() function, or use localhost or 127.0.0.1 as SMTP server. Then the emails will go out through your production server's service network.
They might tell you that port 25 is not allowed, but port (say) 465 or 567 would be allowed. You will have to update your configuration and/or add TLS/SSL accordingly (see above).
or you might be allowed to connect to a third party SMTP server of which you will have to supply the IP address, to allow the IT guys to open a suitable firewall window. Then the emails will go out through the third party server.
The second problem (possibly NOT a problem)
250 marketing#example.in... Sender OK
RCPT to: john.hh#gmail.com
554 Relay rejected for policy reasons
Also to avoid abuse, SMTP Servers will not let everyone connect and send emails, but only their own customers. I see that in the PHPMailer configuration you specified an user and a password. In the telnet session you did not. So it might well be that PHPmailer could send, but not connect, while your telnet can connect, but not send.
Once you solve the connection problem, your authentication problem will either be solved or will have gone away (because you'll be using a different server supplied to you by the IT guys, for example localhost).
The third problem (might never arise)
A third way of abusing services is over-use - sending too many emails to too many people. Verify with the IT guys what the acceptable policies are for sending emails.
Problems, problems
Other things to look into are the credibility of the source (you might want to send emails on behalf of some domain which has not designated your SMTP server of choice as permitted sender), and the confidentiality of the data (even with TLS/SSL connections, if you are given localhost as the SMTP server, your IT guys will have complete, unfettered, undetectable access to any email you send. You might, or might not, be okay with that).
1st quote (from ibm community):
john.hh#gmail.com was a member of a group (Reject) that was listed in
gmail.com's "Deny messages intended for the following internet
addresses" field (in the destination server's Domino Configuration
document's Router/SMTP, Restrictions and Controls, SMTP Inbound
Controls tab's "Inbound Intended Recipient's Controls" section).
Removing Mary's hierarchical name (Mary Jones/ABC) from the members
list in the Reject (group) document allows Mary to receive messages
from the Internet.
2nd quote:
Most mail servers, to prevent them being used as anonymous spam
relays, are configured only to relay mail from certain hosts.
It's a bad idea to use your own SMTP.
Depending of what you have to do with it, you have some great chances to have your emails blocked in some ways or marked as SPAM. And you will have to spend some times to keep your server up to date.
Use online services that are white-listed for every provider and that expose API to send your transactionnal mails :
https://www.mailjet.com/
http://mailchimp.com/
...
They often propose a free account for small volume (under 2000 emails per days).
Using the API is quite trivial and can be put in place in some minutes (ex : https://dev.mailjet.com/)
Ask to your hosting provider if smtp is enabled on not on that server.I had same issue before for smtp and curl both.Contact hosting provider.
What's the phpmailer version?
You might forget to add this line
$mail->IsSMTP(); // use SMTP
after you initiate $mail.
Have a try.
So you have a GoDaddy server and you want to send email through your newly acquired SMTP Relay. Well “you can’t” is probably what you are going to find if you do some poking around Google.
If you poke around a little more you will probably find GoDaddy’s official solution is to use their internal mail relay. This is not ideal, because of arbitrary barriers they have set, such as this one:
Our dedicated servers have an outbound email limit of 1000 per day. If
you need to send more than 1000 emails per day, please call Customer
Support or open a support ticket to request a higher limit.
Quick and Working Solution: Get SMTP Relay that supports sending emails using HTTP API.
My Suggestion: (A simple and effective working solution, I am using personally)
Signup for an account in Sendgrid. Sendgrid provides free 12k emails per month, and I think that should suffice any basic needs.
First Step: Sendgrid will ask you to add few DNS records to authenticate you for sending emails on behalf of that host. So do it.
Second Step Step: Configure White Lables https://sendgrid.com/docs/User_Guide/Settings/Whitelabel/index.html (This is not important, but it will give professional look to the emails you are sending)
Third Step: Generate API keys
You can manage your API Keys from the SendGrid Customer Portal. Additionally, you can manage your API keys via the API itself.
Forth Step: Go ahead and write our codes.
Sending mails using PHP on sendgrid using this library available on github is easy:
// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
require 'vendor/autoload.php';
$sendgrid = new SendGrid("SENDGRID_APIKEY");
$email = new SendGrid\Email();
$email->addTo("test#sendgrid.com")
->setFrom("you#youremail.com")
->setSubject("Sending with SendGrid is Fun")
->setHtml("and easy to do anywhere, even with PHP");
$sendgrid->send($email);
How this works
GoDaddy blocks port 25, 2525, 587 and 485 which are ideal ports for SMTP. So we are using the port 80/443 and requesting the sengrid's API to send the email on our behalf behalf using port 80, and yes, we fooled GoDaddy.
I think within a few steps we can get this working properly.
In your Godaddy/Hostgator Cpanel > Select MX records > Select Domain in question > Select "Remote Mail Exchanger"
You can test mail delivery at this time, issue may be resolved.
I am sure it is set up right but trust me when it comes to good ole Cpanel, PhpMailer and Godaddy/Hostgator. It's a place where common sense makes no sense.
replace:
$mail->Host = 'mail.example.in';
with:
$mail->Host = 'localhost';
I believe this should resolve any issues assuming things like login credentials are valid.
I faced similar timeout issue , the reason was my hosting provider (Linode) .
TLDR; SMTP restrictions are in place by default for Linode on accounts created after November 5th 2019. You'll need to configure rDNS for your instance and open a Support ticket confirming CAN-SPAM compliance, and Support should lift the restrictions pretty quickly. I guess many people face this issue because of smtp restrictions
Timeout error is typical of packets dropped, so could be firewall issues between web server and SMTP server.
If you got ssh access to your server, you can try to connect to smtp using:
telnet <smtp server> 25
On timeout error even with telnet, you're quite sure that the problem is on network/firewall side.
You need to change some setting in your php.ini :
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds.
Were your PHP.ini is depends on your enviroment, more information:
reference
OR
Place this at the top of your PHP script and let your script loose
ini_set('max_execution_time', 600); //600 seconds = 10 minutes or set your time
I got your point. Same issue happen with me , I have just add one line and its working for me. please use ,
$mail->SMTPSecure = 'ssl';
and configure your setting according to ssl . Also properly check mail configure at your serve end.
Related
I recently transferred my server to VPS & now email function doesn't work for external emails.
Following are the settings I'm using:
$transport = Swift_SmtpTransport::newInstance('ns1.example.com', 465, 'ssl')
->setUsername('testing#example.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
And the error that I'm getting is this:
SMTP error from remote mail server after initial connection:
host dedrelay.where.example.net [XX.XXX.XXX.XX]: 554
m1plded02-01.prod.mesa1.example.net : DED :
gWqF1p02c0cB4sG01 : DED : ESMTP
No Relay Access Allowed From XXX.XXX.XXX
I've tried telnet & response is ok. I've tried following:
telnet ns1.example.com 465
Response was:
connected to xx.xxx.xxx.xx
I'm also not able to configure my desktop email client. Can anyone tell a solution? Any help will be appreciated.
EDIT
I'm not even able to send an email through server's webmail. Same error.
You tried telnet ns1.example.com 465 and it connected. This shows the SMTP server is up and running and your computer can reach it. But this is not all you need.
The error message No Relay Access Allowed From 123.123.123.123 means the SMTP server is configured to not accept emails from this IP address for relay, i.e. emails that needs to be passed to another server for delivery.
This is an anti-abuse measure and it means the SMTP server is configured correctly.
There is nothing wrong with your SwiftMailer configuration. Any email client (including the desktop client, as you said) you use, the answer is the same.
You need to contact the system administrator of the SMTP server and ask them to allow your IP address to use their SMTP server as relay. If they are your ISP it's also possible that the server allows relay only after authentication: you have an username/password pair (that you use to read the emails, f.e.) and you need to use it in order to send emails through their SMTP server. But this is only a supposition (this is how it usually works); you have to ask them to know for sure.
Im having trouble being able to send an email. I've tried using Yahoo and Gmail but it seems to be on my end where there isn't a port to send emails from. Do I have to ask my ISP to open up a port on my connection or do I have to open up a port on my machine or the server and if so could some one give me the step/directions to open up a port on my machine or on the server. I have a server which I connect to the internet through so I am on a local machine in the network. Im using PHP to try and send email. I tried configuring my php.ini file but i get an error:=
Failed to connect to mailserver at "localhost" port 587, verify your
"SMTP" and "smtp_port" setting in php.ini or use ini_set()
in C:\wamp\www\buyme\views\forms\register_form.php on line 226
line 226 is where i send an email the default mail($to, $subject, $message, $headers);
Do I need to download and install something else for it to work?
Windows does not have sendmail functionality so its only option is to use SMTP to send mail via another mail server.
Assuming you have the mail settings configured correctly for the mail server you are trying to use. You may depending on your network, be restricted from sending traffic out on the smtp port in use. To verify this, use command prompt and try to open a telnet connection to the mail server on the specified port. You should see some sort of hello message if this works. If not, you may have a firewall somewhere blocking you.
Some SMTP servers require TLS/SSL when sending mail. I'm not sure if PHP supports this with the base mail command, but other mail libraries can be used instead.
Check your hosting's registration email which includes ftp settings, login settings etc. whether there's any information about a SMTP server which you can use in your application. If not, ask your hosting provider if any SMTP service runs on your hosting. If any, ask for it's settings. Maybe there's a different port/security configuration that you don't know.
My ISP have blocked port 25 for sending mails from PHP, and instead have allowed port 587 or 465 to be used. how do i force php mail function to use port 587 instead of default 25? BTW : i am on OSX 10.6.6 using MAMP PRO
UPDATE : i tried changing the settings in php.ini to this
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 587
as i am on mac i don't think this can be the solution for me, and it is not working after i tried. it gives me following error message.
May 6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2822]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May 6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2823]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May 6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2827]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May 6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2825]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May 6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2828]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
you see it is still trying to connect via port 25? how do i change it in mac?
Changing smtp_port only affects how mail() interacts with the server specified by SMTP setting. This isn't the issue. The issue is that:
You are using your local machine as the SMTP server - AND
Your ISP is blocking your local SMTP server (postfix) from relaying messages out to Gmail
First, read this thread. It discusses the same exact issue. The upshot is that you need to use a different mail server, preferably your ISPs mail server. What server and port does your ISP tell you to use for outbound mail if you want to use their Email services? You should be able to use this from your PHP running locally just like you would an email client like Thundebird - and you will be able to send to Gmail.
Set smtp_port = 587 in your php.ini. See http://php.net/manual/en/mail.configuration.php
EDIT
As AJ noted, this won't fix the problem if you're using your local postfix or sendmail, which you do by specifying smtp = localhost. Try setting that to your ISP's SMTP server address instead.
That might lead to the next problem if they also require authentication before allowing you to send mail, which many ISPs do. In that case, your best bet would be the Pear Mail package. That will incidentally also allow you to specify the mail server and port in your script. From the documentation:
$params["host"] - The server to connect. Default is localhost.
$params["port"] - The port to connect. Default is 25.
$params["auth"] - Whether or not to use SMTP authentication. Default is FALSE.
$params["username"] - The username to use for SMTP authentication.
$params["password"] - The password to use for SMTP authentication.
You can edit your php.ini file (if you have access) and set smtp_port = 587 or in your code, ini_set('smtp_port', 587).
If you can, try to override smtp_port setting with ini_set().
Should be something like this:
ini_set('smtp_port', 587);
For those of you using MAMP and not able to send the mail from php mail() function because of port 25 being blocked by ISP (in my case) here is some information for you to solve it. as OSX uses postfix to send mails and if you plan to use external smtp server like smtp.gmail.com which i used here is what you should be doing. you need to configure Postfix to use Gmail as a relay host
a) Open MAMP and in postfix change the domain of outgoing mail to smtp.gmail.com
b) open terminal and type sudo vi /etc/postfix/main.cf this will ask for your admin password enter it and it will open main.cf in vi editor
c) press ctrl+f and come to the end of the file and bring the cursor one line down from the end and press a , the editor will now switch to insert mode to edit the file.
in main.cf append this settings
relayhost = [smtp.gmail.com]:587
smtp_tls_security_level = verify
#smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_session_cache_database = btree:/var/run/smtp_tls_session_cache
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
press :wq to exit vim. Back in the shell type sudo vi /etc/postfix/sasl_passwd and enter the following (substitute your gmail address and gmail password):
[smtp.gmail.com]:587 user#gmail.com:mypassword
again press :wq to save and quit the file, and run the following command
sudo postmap /etc/postfix/sasl_passwd
sudo postfix reload
hope this helps someone with the same problem which i faced.
On my localhost (Linux Mint OS) Swiftmailer is working fine with the following code, but whenever I move it up to my server is just hangs at the send function and then gives me an internal server error.
I currently have my email setup through Google Apps, so maybe SMTP will not work for this situation? If thats the case, how do you suggest I change this from SMTP.
Also, whenever I send an email like this, it is showing up with a from address of the one in the username area. I want it to show up with the from address in the "setFrom" function.
//Include the swiftmailer class
require_once 'comm/swiftmailer/swift_required.php';
//Create a message
//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My Subject');
$message->setFrom(array('noreply#domain.com' => 'No Reply'));
$message->setTo(array('me#domain.com' => 'Me'));
$message->setBody($emailContent, 'text/html');
//Create transport class and email the message
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('useracctname')->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
Thanks a lot for any help!
This might be a port problem on the server. Port 465 could be closed to prevent spamming. It could also be that the server's version of PHP lacks SSL support.
I am wondering though, could it be that I am not using the local mail server so it wont allow SMTP?
If you address gmail as explicitly as you do, it's very unlikely you're using another transport type or a different server.
Also, what about using one of the other transport types?
I think SMTP is your only option to get it running with Gmail. It could, however, be that your server is providing a mail() based service (obviously with a different sender address than GMail though). In that case, you may be able to use Swiftmailer's mail transport.
I had the same problem : I was able to send gmail email in local (after updating that configuration : https://www.google.com/settings/security/lesssecureapps ) but not in production.
To fix my problem I've logged to my gmail account with my production ip / from my production server and answer the gmail security question.
One of the solution to log to your gmail account with your production ip, is to
open an ssh tunnel (ssh -2NfCT -D 5000 yoursshuser#yourdomain.org)
configure you browser to use that proxy (eg Firefox :to Preferences > Advanced > Network > Settings > Manual proxy configuration > SOCKS Hosts: localhost / Post: 5000 / SOCKS v5 )
log to you gmail account as usual
The error outputed by Symfony was :
app.ERROR: Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username "joe.doe" using 1 possible authenticators [] []
well, here is the solution.
if you are sending SMTP mail you should create email account at the server then use this email info at your code.
the server will not send the email if you don't have valid email account to send using it
when you use "from".
this should be a true correct email account a fake one.
the only possible reason to send using fake account is using mail() function.
I'm getting this problem:
PHP Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable to relay for chris.mahan#gmail.com in c:\inetpub\wwwroot\mailtest.php on line 12
from this script:
<?php
$to = "chris.mahan#gmail.com";
$subject = "test";
$body = "this is a test";
if (mail($to, $subject, $body)){
echo "mail sent";
}
else {
echo "problem";
}
?>
section from php.ini on the server:
[mail function]
; For Win32 only.
SMTP = server.domain.com; for Win32 only
smtp_port = 25
; For Win32 only.
sendmail_from = support#domain.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
(note that "server" and "domain" refer accurately to the actual server and domain name)
In IIS, SMTP is running. Under "Access" tab, "Relay" button, the Select which computers may relay through this virtual server is set to checkbox "only the list below" and on the list is "127.0.0.1(xxx.xxx.xxx.xxx)" (x's representing actual server IP address).
Server is running Windows Server 2003 Service Pack 2, fully patched as of 5 PM Sept 1st 2008. I assume it is running IIS7 (how to check?).
Any ideas?
In reponse to Espo: This machine is hosted at a datacenter. We do not want to use a gmail account (were doing it, want to move away from that). Windows server 2003 comes with its own SMTP server.
Update: Per Yaakov Ellis' advice, I dropped all relay restrictions and added the server IP to the allowed list (using the reverse DNS button provided) and the thing started working.
Thanks to both Espo and Yaakov for helping me out.
Try removing the IP restrictions for Relaying in the SMTP server, and opening it up to all relays. If it works when this is set, then you know that the problem has to do with the original restrictions. In this case, it may be a DNS issue, or perhaps you had the wrong IP address listed.
You are using the wrong SMTP-server. If you you are only going to send emails to your gmail-account, have a look at my answer here.
If you also need to send email to other accounts, ask you ISP for your SMTP-details.
EDIT: I think it is always better to use the ISP SMTP-server as they (should) have people monitoring the mail-queues, checking for exploits and updating the mail-software. If you business is developing web-applications it is almost always best to stick with what you do, and let other people do their stuff (eg running mailservers).
If you still for some reason want to use you local SMTP server, the first thing would be to rule out the php-part. Try folowing KB153119 and then check you SMTPServer IISlog for errors.
EDIT2:
That KB-article says it is for exchange, but the same commands are used for other SMTP-servers (including IIS) as well, so please try and see if you can send mails using the examples from the article.
#Espo: I'll do that re KB153119. Thanks.
About the mail server: I hear you.
I'll update when I uncover more.
#Espo, the article in question relates to Exchange servers, not IIS7.0 SMTP server.
From the summary: This article describes how to telnet to port 25 on a computer that runs Simple Mail Transfer Protocol (SMTP) services to troubleshoot SMTP communication problems. The information in this article, including error messages, only applies to issues when attempting to resolve SMTP communication issues with Microsoft Exchange-based servers and is not intended for general troubleshooting purposes.
I had the same problem, php 5 on iis6, 2003 server. Php always failed when trying to use mail().
I've managed to get it accepting mail from php by changing the Relay Restrictions from 'Only the list below' (which is empty by default) to 'All except the list below' .
The relay restrictions can be found in the Access tab in the smtp servers properties screens.
Of course if the server is open to the internet then one would have to be more sensible about these relaying restrictions but in my case this is on a virtual server on a dev box.
hope that helps.