How to make localhost to send email? [duplicate] - php

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.

Related

Use php mailer to send via Gmail - SMTP error [duplicate]

This question already has answers here:
Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?
(13 answers)
Closed 2 years ago.
I just registered a domain via gsuite/gmail to use this as a new mailserver domain for my php mailer.
Now I read through most related topics and adapted the hints, but I still receive the error message:
Message could not be sent.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Here is my code:
$mail->Subject = "Tutor found for help";
//From email address and name
$mail->setFrom('frank#xxx.net', 'xxx.net');
$mail->addAddress("$clientmail", "$clientname");
//$mail->addAddress("nachhilfeanfrage#xxx.net", "xxx.net");
$mail->addReplyTo('frank#xxx.net', 'xxx.net');
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled required for GMail
$mail->SMTPAutoTLS = false;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587; // Port for TLS Gmail
$mail->Username = 'xx'; // SMTP username
$mail->Password = 'xx';
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
This is possible duplicate of Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?
Probably you didn't allow for less secured apps in Gmail account security dashboard.
Before sending emails using the Gmail's SMTP Server, you to make some of the security and permission level settings under your Google Account Security Settings.
Make sure that 2-Step-Verification is disabled.
Turn ON the "Less Secure App" access or click https://myaccount.google.com/u/0/lesssecureapps.
If 2-step-verification is enabled, then you will have to create app password for your application or device.
For security measures, Google may require you to complete this additional step while signing-in. Click here to allow access to your Google account using the new device/app.
Note: It may take an hour or more to reflect any security changes

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).

PHPMailer using SMTP username as senders email address

So I'm trying to send an email using current version of PHPMailer (https://github.com/PHPMailer/PHPMailer). The SMTP connection and the mail sending is working fine, except the script is not using the "SetFrom" $email variable for the senders mail, but instead the "Username" email.
I've tried using multiple forms of setting the "From" mail and nothing seems to work, although it's important to note that the "name" in the "SetForm" is set just fine, but "email" is not.
Here's the code:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'USERNAME(MY EMAIL)';
$mail->Password = 'PASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
$mail->SetFrom($email,$name);
$mail->addAddress($email_received);
$mail->Subject = $subject;
$mail->Body = $user_msg;
This is a common question, asked many times on here. gmail does not allow you to set arbitrary senders - if you're sending through gmail, you have to use either your gmail address, or a pre-configured alternative. See here for google's docs on the matter. Generally it's a bad idea to try to do this anyway as it's likely to cause your message to fail SPF tests that the address owner may have configured.
If you check header of the received mail, you will find your SetFrom email id defined as X-Google-Original-From.
It's because Gmail automatically rewrites the From line of any email you send via its SMTP server to the default 'Send mail as` email address in your Gmail or Google Apps email account Settings.
If you really want to use Gmail as the email provider, then you need to open a Google Apps account and verify that you own the domain which you are using for your email id.
There is also a workaround for this problem (but not recommended):
In your Gmail Settings, go to the 'Accounts' tab and add another email address you own in Send mail as section. This will cause Google's SMTP server to re-write the From field with whatever address you enabled as the default Send mail as address. Here, you need to enter smtp settings of your domain.
So, when you send email using this setting, PHPMailer will connect to Gmail and Gmail will connect to your SMTP server before sending any email.

How to send email with different 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

Help with PHP mail() function [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php mail() function on localhost
I'm trying to do some localhost testing for password recovery on my site, but when I try to send an email, I get the following error:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
Here are the relevant settings in my php.ini file.
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you#yourdomain
I am not sure what to set these to for localhost testing. I realize that I need to set SMTP to whatever my provider's mail server is, but I work in a shared office building so I don't know how to even find out who provides the internet here.
Thanks in advance.
PHP's mail() function doesn't implement SMTP protocol directly. Instead it relies on sendmail() MTA (SMTP server), or a replacement like postfix or mstmp.
It works fine on Unix as long as MTA installed.
On Windows (from PHP.net manual):
The Windows implementation of mail() differs in many ways from the
Unix implementation. First, it doesn't use a local binary for
composing messages but only operates on direct sockets which means a
MTA is needed listening on a network socket (which can either on the
localhost or a remote machine).
So - the moral of the story - you need to install mail server.
However - if it's for test purpose only - simply get a PHP library that actually implements SMTP protocol and use your regular gmail email address to send emails:
Instead of using PHP's mail() use one of these:
PHPmailer
SwiftMailer
Zend\Mail
These PHP libraries actually implement SMTP protocol so one can easily send emails from any platform, without email server installed on the same machine:
PHPMAILER example:
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "some_email#gmail.com"; // GMAIL username
$mail->Password = "pass111"; // GMAIL password
$mail->SetFrom('some_email#gmail.com', 'My name is slim shady');
$mail->AddReplyTo("some_email#gmail.com","My name is slim shady");
$mail->Subject = "Hey, check out http://www.site.com";
$mail->AltBody = "Hey, check out this new post on www.site.com"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "some_email#gmail.com";
$mail->AddAddress($address, "My name is slim shady");
PHP's mail requires a local mailserver to run.
Edit: As the PHP documentation site for mail() reveils, you may use a Mail package from PEAR.
For testing purposes, I'd recommend setting up a fake mail server like Dumbster.
http://quintanasoft.com/dumbster/

Categories