Mail() problem, the mail that i send from php doesnt arrive - php

I'm trying to send emails with php in local, i have modified php.ini and sendmail.ini but still still doesnt work.
The IDE doesn't even give me Errors or a Warnings like Failed to connect to mailserver at "localhost" port 25 or something like that
php.ini
SMTP=smtp.gmail.com
smtp_port=465
sendmail_from = biagiosani2005#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
and sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=biagiosani2005#gmail.com
auth_password=xxxxxxxx
force_sender=biagiosani2005#gmail.com
<?php
$to = "luciasani2005#gmail.com";
$subject = "this is a subject";
$message = "this is a message";
mail($to, $subject, $message);
?>

It is not SMTP send mail. You have SMTP configurations, it is okay, but your sending way is wrong. You need to use PHPMailer package and to correct script such:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';
$html = "<h1>Hello </h1>";
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.titan.email';
$mail->SMTPAuth = true;
$mail->Username = 'contact#domain.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('contact#domain.com', 'domain Team');
$mail->addAddress('receiver1#gmail.com', 'Joe User');
$mail->addAddress('receiver2#gmail.com', 'Joe User');
// Attachments
// Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = $html;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo true;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Related

When i tried to send mail using phpmailer, i am getting error

Could not instantiate mail function.
Message couldnot be sentMailer Error: Could not instantiate mail function.
require 'PHPMailer/PHPMailerAutoload.php';
$mail =new PHPMailer;
$mail->HOST ='localhost';
$mail->PORT = 25;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'Username';
$mail->Password = 'Pass';
$mail->setFrom('info#zoeticpolymers.com');
$mail->addAddress('nitesh54546#gmail.com','Nitesh');
$mail->AddReplyTo("info#zoeticpolymers.com");
$mail->isHTMl(true);
$mail->Subject = 'PHP Mailer Subject';
$mail->Body = '<h1>You are Welcome Here.....</h1>';
if(!$mail->send()){
echo 'Message couldnot be sent';
echo 'Mailer Error: ' . $mail->ErrorInfo; die;
}else{
echo 'Message has been sent'; die;
}
Have you tried with smtp? The cause of Could not instantiate mail function error may be more than one reason. when you try to send large emails and your PHP error log contains the message Can not send a message: Too big then your mail transfer agent (Sendmail, postfix, Exim, etc) is refusing to deliver these emails.
The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication), consider the given example.
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Your Mistake: You used HOST as Localhost it's SMTP.mail.com change mail to your server
You are using older version of PHPMailer also let me help you by typing the correct one
You should download from PHPMailer GitHub
Not vendor for this example
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; //for Gmail
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = 'your Gmail pass';
$mail->Port = 587; // TCP port
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}```
**Your error was that you used HOST as localhost and older version of PHPMailer.
But use default mail() but change the PHP version to 7.3 since it's better now.**
If your code looks good, try to install PostFix on your server.
sudo apt-get install postfix
It worked for me on digital ocean.

XAMPP send mail not working PHP

here are the details
[php.ini]
SMTP = smtp.gmail.com
smtp_port = 465
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off
[sendmail.ini]
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
error_logfile=error.log
debug_logfile=debug.log
auth_username=[email]#gmail.com
auth_password=[email password]
pop3_server=
pop3_username=
pop3_password=
force_sender=[email]#gmail.com
force_recipient=
hostname=smtp.gmail.com
[mail function]
mail('[email]#gmail.com','sample mail','sample content','From: anotheremail#gmail.com');
i have checked smtp settings properly, but its still not working please suggest.
php.ini
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP=localhost
; http://php.net/smtp-port
;smtp_port=25
sendmail_path = C:/xampp/sendmail/sendmail.exe -t
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header=On
; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog
Working For me.
You have to configure SMTP on your server
You can use the googles free SMTP server Pretty easy to setup too.
<?php
$mail = new PHPMailer(true);
//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$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 = "your-gmail-account#gmail.com"; // GMAIL username
$mail->Password = "your-gmail-password"; // GMAIL password
}
//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail :(";
}
?>
You can try below as well.
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.iniandc:\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 = test#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=test#gmail.com
auth_password=my-gmail-password
force_sender=test#gmail.com
//mail function
$mail=test#test.com
mail($mail, 'Subject', 'sample mail', 'From: test#axxx.ba');
You should use PHPMailer instead:
download it from here
copy the PHPMailerAutoload.php from the downloaded Zip folder & paste into your directory where you've your contact us page
Then paste the following code in your contact page script:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Don't forget to change the dummy values
For those who are using Windows and XAMPP, you have to replace the backslash \ with / on your php.ini file. A sample value could be
sendmail_path=C:/xampp/sendmail/sendmail.exe -t -i
https://dzone.com/articles/how-send-emails-php-windows

Mailer error: Could not instantiate mail function for php mailer function

I want to sent mail by using xampp.So I used phpmailer function.But I got these error "Mailer error: Could not instantiate mail function".
My php code is following
$mail = new PHPMailer();
$mail->Host = "localhost"; // SMTP server
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "myothantspo#gmail.com";
$mail->Password = "password";
$mail->From = "myothantspo#gmail.com";
$mail->AddAddress("webdev3#myanmars.net");
$mail->Subject = "no subject";
$mail->Body = "this is a test message";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
My setting for php.ini is following
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
sendmail_from = myothantspo#gmail.com
Most of your configuration (Host, Port, SMTPAuth etc) relates to PHPMailer's own SMTP handler rather than PHP's built-in mail function.
If, as it therefore appears, you want PHPMailer to send email using SMTP instead of mail then you need to add $mail->IsSMTP(); to your code somewhere before you call $mail->Send().
Did you include phpmailer? Like this:
require 'PHPMailerAutoload.php';
Or you might want to check this:
https://github.com/PHPMailer/PHPMailer

PHP mailer smtp setup with gmail

Hello guys I have stuck with this problem for a while now. I am trying to send mail from xampp. I have went through several solutions posted here but none of them seem to work still. kind of need someone to point me to the right direction.
so in my php.ini
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = user#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
I also have the sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" commented with a ;
now in my sendmail.ini
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=user#gmail.com
auth_password=123456
force_sender=shadidhaque2014#gmail.com
P.s: I only have the above code in sendmail.ini
now for the php script I have something very simple:
$send=mail('rzs2009#yahoo.com', 'subject', 'blah blah blah');
if ($send) {
# code..
echo "yes";
}else
{
echo "no";
}
now everytime I try to run the program I get a no. So no email is being sent. Where might I be going wrong.
Thanks in advance.
Modify your code like this:
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

XAMMP Sendmail issue

I am having an issue getting my sendmail to work in Xammp.
The mail was working perfectly when using sendtodisk but now I get a crash log using sendmail:
PHP to send emails (Using PHP Mailer):
$mail = new PHPMailer;
$mail->setFrom($from_email, 'Thomas ****');
$mail->addAddress('thomas#****.co.za', 'Client');
$mail->Subject = 'Sign Offs';
$mail->Body = generateMailTemplate($job_id, $to_email, $from_email, $name);
$mail->addAttachment('tmp/'.$job_id.'.pdf');
$mail->isHTML(true);
if (!$mail->send()) {
return $mail->ErrorInfo;
} else {
return true;
}
php.ini
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
sendmail.ini
smtp_server=outbound.mailhop.org
smtp_port=25
crash.log
command line : C:\xampp\sendmail\sendmail.exe -t -fthomas#****.co.za
Any ideas?
try to set smtp settings for your phpmailer script, not for php
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'outbound.mailhop.org';
$mail->Port = 25;
// if need auth
$mail->SMTPAuth = true;
$mail->Username = 'user';
$mail->Password = 'password';
$mail->Subject = 'subject';
$mail->Body = 'body';
$mail->send();

Categories