How to connect a php testing server like https://mailtrap.io/ .
If i have defined variables like
$host = 'mail.sample.com';
$port=25;
$user="username";
$pass="secret";
maybe also $Auth = "PLAIN, LOGIN and CRAM-MD5" $TLS="Optional (STARTTLS on all ports)";
How to use these variables, nowhere i can find samples.
I want to use simple php mail function.
mail($to, $subject, $message, $headers);
Thank you
Mailtrap is a fake SMTP server. However, the built-in PHP mail() function is simple and lacks SMTP authentication support ā which is required by mailtrap.
So, move on an and use a PHP mailer package like PHPMailer, Swift Mailer, PearMail, etc as shown here https://blog.mailtrap.io/php-email-sending/
E.g. with PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
require_once './vendor/autoload.php';
$mail = new PHPMailer();
// configure an SMTP
$mail->isSMTP();
$mail->Host = 'smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = '1a2b3c4d5e6f7g';
$mail->Password = '1a2b3c4d5e6f7gā;
$mail->SMTPSecure = 'tls';
$mail->Port = 2525;
$mail->setFrom('confirmation#hotel.com', 'Your Hotel');
$mail->addAddress('me#gmail.com', 'Me');
$mail->Subject = 'Thanks for choosing Our Hotel!';
$mail->isHTML(TRUE);
$mail->Body = '<html>Hi there, we are happy to <br>confirm your booking.</br> Please check the document in the attachment.</html>';
$mail->AltBody = 'Hi there, we are happy to confirm your booking. Please check the document in the attachment.';
if(!$mail->send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
There are other, haphazard options but I bet they are not working in your case. One of them is changing mail directives in php.ini and sendmail.ini (if you have access to it and feel adventurous).
Related
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.
I'm guessing that this:
<?php
$emailTo = 'user1#gmail.com';
$subject = 'I hope this works!';
$body = 'Blah';
$headers='From: user#gmail.com'
mail($emailTo, $subject, $body, $headers);
?>
is not going to cut it. I searched for ways that I can send to email with SMTP auth and mail clients and programs such as PHPMailer, but I don't have a concise and direct answer yet thats helpful. Basically, I want a way to send emails to gmail, hotmail, etc (which will be my email) from another email (sender) through a form on my website
Questions:
Do I need to download a 3rd party library to this?
If not, how can I change the code above to make it work.
Thanks!
Use PHPMailer library.It is better than use mail native function because in PHPMailer you can use user authentication to avoid to send the email to spam.You can use this library without to configure a mail server. You can download in this link https://github.com/PHPMailer/PHPMailer
See an example:
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 587; // SMTP port
$mail->Username = "yourusername#gmail.com"; // username
$mail->Password = "yourpassword"; // password
$mail->SetFrom('user#gmail.com', 'Test');
$mail->Subject = "I hope this works!";
$mail->MsgHTML('Blah');
$address = "test#test.com";
$mail->AddAddress($address, "Test");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
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
I don't know if my code is the issue or if I had configured Zoho's SMTP's settings incorrectly.
Basically I want the ability to dynamically send emails with a simple php function like for example
phpMail("to#example.com", $subject, $body, "from#example.com", "replyto#example.com");
This is my PHPMailer.php script (it sees the function and its settings)
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**REMOVED**'; // SMTP username
$mail->Password = '**REMOVED**'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true); // Set email format to HTML
// SYTAX: phpMail($from, $reply, $to, $subject, $body);
function phpMail($to, $subject, $body, $from = "from#example.com", $reply = "replyto#example.com") {
if (isset($from))
$mail->From = $from;
$mail->FromName = "testing";
if (isset($to))
$mail->addAddress($to);
if (isset($reply))
$mail->addReplyTo($reply);
if (isset($subject))
$mail->Subject = $subject;
if (isset($body))
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Now the issue with this is that, I'm not receiving emails nor am I receiving any errors / messages but it's also not sending me an email.
When you make a PHP function, make sure your objects aren't outside of your function.
My error wasn't that though, it was basically my own stupidity which had nothing to do with the code but more of my "form post data".
The following configuration works for me using zoho mail.
Give it a try:
$mail=new JPhpMailer;
$mail->IsSMTP();
$mail->Host='smtp.zoho.com';
// Enable this option to see deep debug info
// $mail->SMTPDebug = 4;
$mail->SMTPSecure = 'ssl';
$mail->Port='465';
$mail->SMTPAuth=true;
$mail->Username='your_email_address';
$mail->Password='your_eamil_address_password';
$mail->isHTML(true);
$mail->SetFrom('your_email_address','Your Name');
$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('<h1>JUST A TEST!</h1>');
$mail->AddAddress('destination_email_address','John Doe');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I want to use Mandrill for sending my emails. I am customizing my phpmailer which I had used to send the mail with mandrill phpmailer. (found here: http://help.mandrill.com/entries/23737696-How-do-I-send-with-PHPMailer- )
require 'mandrillmailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MANDRILL_USERNAME'; // SMTP username
$mail->Password = 'MANDRILL_APIKEY'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Your From name';
$mail->AddAddress($to); // Add a recipient
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
echo $mail->Subject;
//$mail->Send();
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'hiii';
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
else{
echo 'hello';
echo 'Message has been sent';
}
die('here');
I am not getting response messages i.e mail is sent or not. I debugged where it is getting stuck by echo statements. I can see the messages till echo $mail->Subject; but not beyond that. I am guessing that $mail->Send() is not working that's why messages beyond that are not displaying and email sending is not working.
How do I fix that?
Assuming you've put in the proper authorization, try it by requiring the autoloader rather than phpmailer class. This worked for me:
require 'mandrillmailer/PHPMailerAutoload.php';
Alternatively you could try something a little easier. Something like the official Mandrill PHP client or a separate service like sendwithus.
First of all check the PHPMailer class version If Its below Version 5 then use the latest version 5.2.9
My issue resolved as I used the Version 5.2.9