How to send email with different server - php

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

Related

Cannot send mail from phpmailer on server A + Exim on server B

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.

How to make localhost to send email? [duplicate]

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.

How to mail a log file to yourself using PHP from a Windows machine ?

Situation:
I run a PHP script from a windows 7 machine using CLI.
My script will do its thing and then generate 1 log file at the end.
I already got all that part working.
In addition to that, I want to email myself that log file every time the script runs.
Somewhere in the bottom of my script I tried this :
mail('email#gmail.com', 'Report', strip_tags($response). PHP_EOL );
The script run all the way to the bottom, I got my log file to generate, I also got a report on my CLI as well, but I never receive any email.
Result :
I am not sure is because I :
am on a Windows.
need to allow specific php extension
Need to configure more setting in my php.ini.
Can someone help clarify this issue ?
You need a Mail Server which is configured in the PHP.ini to send your mails.
Here is a short Tutorial:
http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx
Note that the IIS6 console still needed for the Mail Server, also if you're hosting on >=IIS7.
Also you need to make sure, your Mail Server is acceptet by the Mail Server you want to send this mail to. This is definitve not a trival task. Gmail and GMX for example never accept it, if you didn't have Reverse DNS correctly configured.
If you don't know how, I highly recommend to have a talk to your System Administrator. Mail Server are very hard to setup correctly. It's a task I work around.
But here are the good news, if you do not want to configure your own mail server. It is very simple with an Hosted Email Adress and SMTP, if your using the Open Source Project PHPMailer:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//More in the Documentation
It has a powerfull SMTP Class, which helps extreme for login into any SMTP Account (Gmail for example).

Change from PHPmailer to sendmail

I want to send the emails via original old school sendmail.
In what way i need to change this CODE to work with sendmail?
i have tried but i always get very ugly errors, i tried to change IsMail to IsSedmail but still does not send the original way.
Phpmailer sends 3 email and the website is taking a lot time to send, so i want to go to the old plain "mail(to...." but the problem is that i`m lost in all the code so please Help.
function send_mail($rec_email,$subject,$message, $IsHtml=false, $cc=array(), $bcc=array()) {
global $THIS_BASEPATH, $btit_settings;
if (!method_exists('PHPMailer','IsMail'))
include($THIS_BASEPATH.'/phpmailer/class.phpmailer.php');
$mail=new PHPMailer();
if ($btit_settings['mail_type']=='php') {
$mail->IsMail(); # send via mail
if (!empty($cc))
$mail->AddCustomHeader('Cc: '.implode(',',$cc));
if (!empty($bcc))
$mail->AddCustomHeader('Bcc: '.implode(',',$bcc));
} else {
$mail->IsSMTP(); # send via SMTP
$mail->Host = $btit_settings['smtp_server']; # SMTP servers
$mail->Port = $btit_settings['smtp_port']; # SMTP port
$mail->SMTPAuth = true; # turn on SMTP authentication
$mail->Username = $btit_settings['smtp_username']; # SMTP username
$mail->Password = $btit_settings['smtp_password']; # SMTP password
if (!empty($cc))
foreach($cc as $carbon_copy)
$mail->AddCC($carbon_copy[0],$carbon_copy[0]);
if (!empty($bcc))
foreach($bcc as $blind_carbon_copy)
$mail->AddBCC($blind_carbon_copy[0],$blind_carbon_copy[0]);
}
$mail->From = $btit_settings['email'];
$mail->FromName = $btit_settings['name'];
$mail->CharSet = $btit_settings['default_charset'];
$mail->IsHTML($IsHtml);
$mail->AddAddress($rec_email);
$mail->AddReplyTo($btit_settings['email'],$btit_settings['name']);
$mail->Subject = $subject;
$mail->Body = $message;
return ($mail->Send())?true:$mail->ErrorInfo;
}
Thank You very much.
PHPmailer is not the culrpit for "slowness", it's probably the SMTP server you've specified. Do not stop using PHPmailer, though. PHPmailer does tons of extra stuff behind the scenes to send mail correctly.
To send mail out through the local server using PHP's mail() replace:
$mail->IsSMTP(); # send via SMTP
$mail->Host = $btit_settings['smtp_server']; # SMTP servers
$mail->Port = $btit_settings['smtp_port']; # SMTP port
$mail->SMTPAuth = true; # turn on SMTP authentication
$mail->Username = $btit_settings['smtp_username']; # SMTP username
$mail->Password = $btit_settings['smtp_password']; # SMTP password
With:
$mail->isMail();
That's it.
If you are certain that the server has either Sendmail [or a drop-in replacement like Postfix or Exim] installed then you can use:
$mail->isSendmail();
However, by using the web server to send out mail you are now dependent on:
The installed MTA being configured correctly, which they frequently aren't.
The reputation of the web server according to various blacklists. Generally, web servers have s**t reputations because anyone can drop outbound mail into the queue without authentication.

Unable to send mail with PHP (phpmailer) via Office365 in Hostgator. How to fix this?

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;

Categories