How do I use PHPMailer with Outlook Exchange? - php

I've searched all around for some information on sending email with PHP, and it seems that PHPMailer is the way to go.
I don't understand much about SMTP and how mail is generated, so I'm not sure what to do in order to get started here.
My company uses Microsoft Exchange on the backend, and Microsoft Outlook 2007 on the frontend. I am trying to write some PHP code that will utilize this to send an email, but I (surprisingly) can't find anything online about how to do this.
Can anyone point me in the right direction?
More specifically, the PHPMailer example lists these items:
$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
But I have no idea where to find this information apart from username and password.

Related

How to find the information from the hosting service 1&1 in order to send emails using PHP mailer?

I currently have a website which uses PHPmailer to send emails. I am hosting it with 1&1.fr, but cannot find the information in order to actually send emails. Here is the following information that I need:
$mail->SMTPDebug = 2; // Enable verbose debug output
$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
On the 1&1.fr website, they give out the following information:
In the image, they specify multiple ports as well as an entrance/exit server; which ones am I supposed to pick and enter into my PHP file.
The rest of my code works fine (it works when I use my gmail account using 000webhost).
Any help would be greatly appreciated.
// define the $mail // just in case you miss it as it is missing in your code.
$mail = new PHPMailer();
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2; // this is to enable debug if there are errors
$mail->isSMTP(); //Tell PHPMailer to use SMTP
//Set the hostname of the mail server
$mail->Host = 'auth.smtp.1and1.fr';
// Enable authentication so you must provide username and password for SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Here you are telling to use a secure connection with TLS/SSL
//Set the SMTP port number
$mail->Port = 587; // if specified tls. try also 465 as defined in the picture you post
// TCP port for secure connections. 465 is the secure port for outgoing
// emails and 993 is for incoming email using IMAP. If you use POP3 the
//incoming emails are received on 995 port number.
Hope now is more clear.

phpmailer SMTP client information

I am updating some contact forms on several websites i made, and i am using phpmailer for it.
I'm using the SMTP method where you fill in the username and password of the email client where it needs to send to, like below:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mailout.one.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#company.com'; // SMTP username
$mail->Password = 'mypass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('info#company.com', $email);
$mail->addAddress('info#company.com');
$mail->addReplyTo(''.$email.'');
My problem is i have to get my client his/hers emailaddress with their password, how could i solve this without asking my clients for their email information.

Pass SMTP Credentials to PHPMailer Configuration without exposing it publicly in heroku

I'm using the phpmailer library. To send an email I need to specify an smtp server, smtp user and smtp password. The problem is I'm using the cloud platform Heroku where I use a github repository to autodeploy to heroku, I don't want my smtp username and password to be public. Is there a way to solve this?
Below is the code snippet
$mail = new PHPMailer(false); // Passing `true` enables exceptions
//Server settings
//$mail->SMTPDebug = 1;//Enable verbose debug output
$mail->isSMTP();//Set mailer to use SMTP
$mail->Host = 'smtp host';//Specify main and backup SMTP servers
$mail->SMTPAuth = true;//Enable SMTP authentication
$mail->Username = 'user';//SMTP username
$mail->Password = 'password';//SMTP password
$mail->SMTPSecure = 'tls';//Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;//TCP port to connect to
//Recipients
$mail->setFrom('example#example.com','myapp');
$mail->addAddress('hi#examle.com');//Add a recipient
//$mail->addAddress('ellen#example.com');//Name is optional
$mail->addReplyTo('support#example.com','Contact');
//Content
$mail->isHTML(true);//Set email format to HTML
$mail->Subject = 'test';
$mail->Body = 'this is a test';
$mail->send();
One way to not expose but to use it in your code, is by setting your SMTP username and password in environment variables
The procedure to set environment variables for your heroku app is documented in this link - https://devcenter.heroku.com/articles/config-vars
Once you have set your username and password in your environment variables you can access them using the below code
$mail->isSMTP();//Set mailer to use SMTP
$mail->Host = 'smtp host';//Specify main and backup SMTP servers
$mail->SMTPAuth = true;//Enable SMTP authentication
//Assuming SMTP_USERNAME is your environment variable which holds username
$mail->Username = getenv('SMTP_USERNAME');
//Assuming SMTP_PASSWORD is your environment variable which holds password
$mail->Password = getenv('SMTP_PASSWORD');
$mail->SMTPSecure = 'tls';//Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;//TCP port to connect to
References
- Use Heroku config vars with PHP?
There are many possible ways-
Use Heroku CLI
https://devcenter.heroku.com/categories/command-line
You can create an admin page in you website from where you put smtp
serve details that will persist to a file. This file will be
available until the heroku dyno restart.
You can use dropbox for deployment
Instead of Github you can use bitbucket which
is also free with git repo
https://confluence.atlassian.com/bitbucket/deploy-to-heroku-872013667.html

Mail not sending with PHPMailer over SSL using SMTP

I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "myEmail#gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail#gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail#gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail#me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail#gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail#gmail.com : Called Mail() without being connected
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
Obviously it's worth pointing out that I'm not using the literal "myEmail#gmail.com" but I've substituted my own email out for this post.
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "#gmail.com" from the Username property
- A number of other things I can't remember
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
Thanks
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
try to replace what you have
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the
Firstly, use these settings for Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
sudo /etc/init.d/networking restart # for ubuntu
First, Google created the "use less secure accounts method" function:
https://myaccount.google.com/security
Then created the another permission:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.

PHPmailer can't connect to smtp server

I've been using PHPmailer (https://github.com/Synchro/PHPMailer) to send email through amazon SES for a few months. At some time in the last two weeks it has stopped working, and I haven't touched it. I'm getting an error msg:
SMTP Error: Could not connect to SMTP host.
This is my code.
public function sendEmail($to,$subject,$body){
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'amazonaws....'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mySMTPuname'; // SMTP username
$mail->Password = 'smtpPword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'example';
$mail->FromName = 'me';
$mail->AddAddress($to); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
My amazon account is still upto date and active. Is there any way to print out more detailed error msgs for debugging? Has there been any known issues lately?
Try :
$mail->SMTPDebug = 1;
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
This is a very old question but I just had the same problem so it may still be relevant to others.
If it stopped working without you changing anything it is probably connected to your hosting company / isp blocking SMTP traffic from your server to other servers.
There are a few topics on this, as multiple hosting companies using Cpanel and also Godaddy have implemented such measures for combating spam.
Try:
$mail->SMTPDebug = 3;
to get the maximum level of detail on the error.
One solution is to use the mail server in the same hosting account (if it blocks SMTP to the outside it probably has an internal service you can use).
To keep using Amazon SES you need to open SMTP traffic on your server.
IF you have control over Cpanel/WHM "tweak settings" you can do it yourself, otherwise you need to ask your hosting provider.
Check this answer for details "Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

Categories