I have a website hosted on server A. I use Cloudflare services, so I can not send mails to my users from the same server. I have server B for mail sending. Installed Exim and configured it as follows:
internet site, mail is sent and received directly using SMTP
Machines to relay mail for: [IP address of the server A]
On serevr A I use Phpmailer to send mail:
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->SMTPSecure = "";
$mail->Host = "IP address of server B";
$mail->Port = 25;
$mail->Username = "";
$mail->Password = "";
Unfortunately, it doe snot work. Tried to change $mail->SMTPAuth to "true" but it does not help.
SMTP Error: Could not connect to SMTP host.
You've not posted much info to go on, but it's likely that outbound traffic to port 25 is blocked - see if you can telnet serverb 25 from server A. Normally you can't send out (relay) through port 25 anyway, but use an external authenticated submission host on port 587 instead. You should try reading the troubleshooting guide which covers all kinds of connection issues.
Related
I was trying to send emails from my website which is hosted by Hostgator.
whenever I try to send SMTP emails via any common port such as 587 or 25 the email works fine with the same code, but when I try to send using port 465 following the host settings it doesn't work and the website stops responding for few minutes on any device connecting from any other IP.
I'm posting this since the host doesn't provide any coding help.
below is the code:
$email = new PHPMailer();
$email->isSMTP();
$email->SMTPDebug = 0;
$email->Debugoutput = 'html';
$email->SMTPAuth = true;
$email->Host = "gatorxxxx.hostgator.com";
$email->Port = 465;
$email->Username = "email#mydomain.com";
$email->Password = "emailpassword";
$email->setFrom('email#mydomain.com', 'Sender Name');
$email->Subject = 'Subject';
$email->MsgHTML($body);
$email->AddAddress( "useraddress" );
$email->AddReplyTo('email#mydomain.com');
if(!$email->Send()) {
header("xxx");
die ();
} else {
header("yyy");
die ();
}
My concern is Port 465 is for authenticated email sending, therefore it has fewer chances to land in recipient's spam folder, while using ports such as 25 or 587 might be unsafe, hence can trigger spam filters from the client side.
Port 465 is not open on all SMTP hosts, it sounds like you are experiencing a TCP timeout, or your email class doesn't support SSL. Port 465 requires SSL, not STARTTLS as 587 does.
Also the port you submit through has no impact on the spam folder on the client, once your SMTP server # HostGator get's the email it will relay it to the target server on port 25. The "authenticated email" is just proving you are authorised to relay through the host, it has nothing to do with the content of the email or if it ends up in the spam box.
To avoid spam filtering you need to ensure you have valid SPF records configured, your RDNS is configured and valid in both directions and you DKIM sign your messages.
The best way to handle all this is to run a local SMTP server that you send through that relays to the upstream SMTP server. For example, Postfix with OpenDKIM.
This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 7 years ago.
I am unable to send mails through localhost Xampp even after making changes in php.ini and sedmail.php files as per THIS.
I have adoubt in sendmail.php file what email & PassWord to give here;
auth_username=
auth_password=
Please somebody get me through this.
You can use SMTP mail sending library and try that function to send the mail from localhost
Let we resolve this issue by follow some steps.
1. Make sure error reporting is enabled and set to report all errors(use code in .php file)
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
2. Check your server's mail logs
Your localhost should be logging all attempts to send emails through it. The location of these logs will user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.
3. Make sure you have a mail server set up on localhost
If you are developing on your local workstation using XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.
You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.
You can also use SMTP to send your emails. See this answer to re-check how to do this.
4. Check to see if mail() returns true or false with message
Please use below code and let me know what happen. This code will display actual error message and we can resolve this issue.
Replace
mail($to, $subject, $message, $headers);
With
$mailReturn = mail($to, $subject, $message, $headers);
print('<pre>');
print_r($mailReturn);
print('</pre>');
exit();
If above 3 steps done perfect but no success then follow step 4. Let me know what this code print.
5. SMTP settings(in php.ini) for send mail from localhost
If you are using Gmail then you got lucky. Gmail lets us use their SMTP provided that you will have to authenticate it using your own username and password. To use Gmail SMTP the values will be like below:
//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com";
//enable this if you are using gmail smtp, for mandrill app it is not required
//$mail->SMTPSecure = 'tls';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "YOUR.ID#GMAIL.COM";
//Password to use for SMTP authentication
$mail->Password = "YOUR_PASSWORD";
Or, if you don't want to use your Gmail account then I would suggest to create one account on Mandrill and get your SMTP host, username and password from there. I have tested both gmail and mandrill and they are working pretty good.
//Set the hostname of the mail server
$mail->Host = "smtp.mandrillapp.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "YOUR_REGISTER_EMAIL#MANDRILL.COM";
//Password to use for SMTP authentication
$mail->Password = "YOUR_PASSWORD";
Make sure all variables value are checked and change if require.
I have opened the port 465 on my server:
iptables -A INPUT -p tcp -m tcp --dport 465 -j ACCEPT
iptables-save | sudo tee /etc/sysconfig/iptables
service iptables restart
And I can see the port is ACCEPT when I run iptables -L -n
But still when I try to send mail:
$mail = new PHPMailer ();
$mail->IsSMTP ();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "gmailusername";
$mail->Password = "gmailpassword";
$mail->SetFrom ( $from, $title );
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress ( $to );
$mail->Send();
I get the following error:
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
<br />The following From address failed: gmailusername : Called Mail() without being connected
Any help would be appreciated!
Turns out, digitalocean had blocked sending emails for new users. Contacted them and now it's working. Hope this helps someone.
please go through http://aldrin.aquisap.info/2012/08/26/wp-mail-smtp-in-wordpress-hosted-by-bluehost/ or SMTP ERROR: Failed to connect to server: Connection timed out (110) with PHPMailer and Outlook SMTP.. i guess this is related to your error.
Google's SMTP server requires authentication, so here's how to set it up:
SMTP server (i.e., outgoing mail): smtp.gmail.com
SMTP username: Your full Gmail or Google Apps email address (e.g. example#gmail.com
or example#yourdomain.com)
SMTP password: Your Gmail or Google Apps email password
SMTP port: 465
SMTP TLS/SSL required: yes
In order to store a copy of outgoing emails in your Gmail or Google Apps Sent
folder, log into your Gmail or Google Apps email Settings and:
Click on the Forwarding/IMAP tab and scroll down to the IMAP Access
section: IMAP must be enabled in order for emails to be properly
copied to your sent folder.
Sending Limits
Google limits the amount of mail a user can send, via its portable SMTP server. This limit restricts the number of messages sent per day to 99 emails; and the restriction is automatically removed within 24 hours after the limit was reached.
Source: How To Use Google's SMTP Server
with PHP, we can easily send email with current server if we have installed mail server or DirectAdmin or CPanel and so on ...
now think the situation that we need specif server sends emails, one server should be mail server and another should be Apache + PHP ? how can I achieve that ?
I am using ubunto for both server
Try SMTP to send the mail from Another server->
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
There can be at least these two approaches.
1. Using mailer server as SMTP
Install and configure an SMTP software like Postfix on the
system you want to make mail server
Then in the webserver use a library like PHPMailer of
SwiftMailer to send email using the mailserver's ip address and
created smtp user.
2. Using mail sererver's PHP mail()
This is the other way around.
Access the database installed in the webserver in the mailserver
machine by using the webserver's ip address as the hostname and the
respective username and password.
You will need to enable remote connection to the database. If its
mysql then you can see the following question:
Remote Connections Mysql Ubuntu
I want to be able to send mails in Hostgator via office365. I was able to do it with Gmail, but can not set it up to work with office365.
It works on my 2 other servers. The only problem is Hostgator.
Do I have to fix something or Hostgator have to take some action?
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "pod51014.outlook.com";
$mail->Port = 587;
$mail->Username = "usernamehere";
$mail->Password = "************";
/* ... addaddres, reply, subject, message -> the usual stuff you need ... */
$mail->Send();
?>
I just keep getting following response:
SMTP -> ERROR: Failed to connect to server: Connection refused (111)
I was on the support chat with them and the 587 port should be open.
I think hostgator is blocking outgoing emails but accepts incoming emails.
If your hosting provider doesn't allow outbound SMTP mail, I suggest you take a look at Microsoft Graph - a REST API which let's you also send e-mails and do much more. You can use for example oauth2-azure library to interact with it very easily from your PHP code.
Try these things. Maybe something will work.
Set the host to:
$mail->Host = 'smtp.office365.com';
Do not set a port at all:
//$mail->Port = 587;