sending e-mail from mail function php - php

I can send an e-mail using php mail function. Other languages also have similar functions.
mail(to,subject,message,headers,parameters)
I was under an impression that to send an e-mail you need SMTP credentials. How does this function send emails? Which SMTP server does it use? How does it work?

In php.ini, there is a field called SMTP. It is where you set the SMTP server. I think it defaults to localhost. Other related fields include smtp_port and sendmail_from. You use these for a Win32 system.

you have to configure your server to send emails ..
You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP.
So if you are using XAMPP then you can easily send mail from localhost.
for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.
in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
in php.ini file find [mail function] and change
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id#gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id#gmail.com
Now you have done!! create php file with mail function and send mail from localhost.
PS: don't forgot to replace my-gmail-id and my-gmail-password in above code.
Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" in the php.ini file
REF : LINK

The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn't include a local mail server.
Using the mail() function can be a lot of work. Instead use an external library which greatly simplifies this work for you. One such example is PHPMailer

Related

Can't change from header on localhost mail PHP

I'm trying to send emails from my localhost, but I'm running across an issue that keeps coming up. I found this answer which explained how to set up XAMPP to send email, and the emails are sent, but when I try changing the From header, nothing happens. The email gets sent, but it's sent from my personal email.
I tried removing both the sendmail_from in php.ini and force_sender in sendmail.ini, but neither worked. I tried adding the -f parameter to the mail function, and it didn't work. I've even tried restarting XAMPP several times, but still nothing. Is there something I've overlooked or is there no way to do this?
PHP
<?php
$headers = "From: Joe Smith <joe#joesmith.com>" . "\r\n";
mail("recipient#gmail.com", "This is a test message", "Yup, it's a test message all right.", $headers, "-f joe#joesmith.com");
?>
php.ini
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from =
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
sendmail.ini
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=mypersonalemail#gmail.com
auth_password=mypersonalpassword
It is because you are using Google's SMTP servers and they don't allow changing the 'From' header.
This was answered here:
How to change from-address when using gmail smtp server
I just did a project similar to this. Depending on your needs, you can use PHPMailer to change your "reply to" address. Although it's not the same as changing the from address, it achieves a similar goal, is easy to use, and has a good community. Also, it works perfectly with Google Mail.

php.ini, sendmail configuration to send an email using a php script

I need some help. I am trying to send an email from a php script. My environment comprises of the following:
Operating System: Windows 8
XAMPP version: 1.8.2
php version: 5.4.19
I have the following php script:
<?php
mail('sugar.donkey#gmail.com','Helo','This is a test','From:salt#goodness.com');
?>
The following configuration on send mail configuration file:
smtp_server=smtp.gmail.com
; smtp port (normally 25)
smtp_port=465
auth_username=sugar.donkey+gmail.com
auth_password=[MYPASSWORDHERE]
The configurations on php.ini:
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = smtp.gmail.com
; smtp_port = 465
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster#localhost
I dont get error when I run the php script, but I also dont seem to receive an email. Where am I going wrong ?
I am not sure what os you are running on your webserver, however;
Most Linux installations have sendmail preinstalled, there is always a hassle of setting up SPF/PTR records, to ensure that the email sent by your PHP script is not flagged as spam. A SMTP client called MSMTP can be used to send emails using third-party SMTP servers, this can also be used by PHP's mail() in the place of sendmail.
I hope this helps
https://www.digitalocean.com/community/articles/how-to-use-gmail-or-yahoo-with-php-mail-function
Also for localhost testing, check this out.
http://blogs.bigfish.tv/adam/2009/12/03/setup-a-testing-mail-server-using-php-on-mac-os-x/
Even I am trying to get this configuration work :)
in your case i believe you need to comment out in php.ini
the configuration should be
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = smtp.gmail.com
smtp_port = 465
; For Win32 only.
http://php.net/sendmail-from
sendmail_from = postmaster#localhost
Thank you

Apache localhost mail() not being received with Mercury Server & Thunderbird

I upgraded Windows XAMPP to 1.8.3 and am trying to get php working (again) on localhost with Apache 2.4.4, Mercury Mail 4.6, and Thunderbird client 24.1.1. Thunderbird can send and receive mail between two accounts. php's mail() is returning true but no mail is coming through.
php.ini is set to listen on port 25.
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = localhost (I've also tried 127.0.0.1)
smtp_port = 25
Any ideas what I might be missing? Thanks in advance.
Note: sendmail.ini also has
smtp_server=localhost
smtp_port=25
I commented out the following line and it worked.
; sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
For good measure, I changed this to:
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
I don't know why mailtodisk.exe is defaulted to being used, but it appears this simply writes the mail to a folder and it doesn't get delivered.

Mail() PHP xampp/gmail

Ive been trying desperately all day to get an email sent from a php file running on xampp from a gmail account.
I want to use Gmails SMTP as I tried forever to get Mercury to work and it was very problematic. So far I set it up as described below:
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=25
smtp_ssl=auto
auth_username=justinharr6#gmail.com
auth_password=*password*
force_sender=justinharr#gmail.com
[mail function]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header = Off
(everything else commented out)
Keep in mind this is one of many configurations I have tried none of which work. My php files is returning true from the Mail call but its never getting to the destination. Any help would be greatly appreciated.
i am assuming you are trying to send a mail through local host
i suggest you should use PHP mailer class

php send mail local not woking?

I tried to send mail in localhost.
but working.
i change the php.ini
[mail function]
SMTP=SMTP.gmail.com
smtp_port =587
sendmail_from =postmaster#testmail.com
sendmail_path = "C: \xampp\sendmail.exe\" -t"
this my php mail function
public function sendMail($to,$subject,$from=null,$headers = null)
{
if($headers == null) {
$headers = $this->setMimes();
}
$headers .= $this->setFrom($from);
return mail($to,$subject,$this->getMessage(),$headers);
}
this mail not sending.
To check/change your PHP mail configuration:
Open your php.ini file (if you don't know where this is, see below) Search for the line that reads [mail function] Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP. Save/close the php.ini file Restart your web server
An example of what the mail settings could look like when you first open the php.ini file:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
Additional info is in echoing phpinfo() you can view your PHP configuration details. You can do this by creating a .php file with the following line on it: . When you run this in your browser, you will see a full list of PHP configuration variables. Simply search for the lines that contain php.ini and sendmail_path to see the values you need to use.
Another idea is you might use ini_set() to properly config your mail setting like this
Add the following code to the top of your email script if your mail script continues to fail.
// Please specify your Mail Server - Example: mail.example.com.
ini_set("SMTP","mail.example.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'example#YourDomain.com');

Categories