How to use PHPmailer? Help me with its API - php

I have created a PHP page, whereby having the page to send automatically to user, i tried using PHPMailer but it only says "SMTP ERROR: cannot access host or somthing like that "
here's my code:
PHPMailer x = new PHPMailer();
x->isSMTP();
x->Host = myhost;
x->Port = myport;
x->AddAddress(recipientAddress);
x->From = myEmail;
x->Username = username;
x->Password = password;
x.Send();

here is quick fix of your code
$mail = new PHPMailer();
$mail->IsMail();
$mail->setCharset = "UTF-8";
$mail->Host = "localhost";
// $mail->Port = "587"; you don't need it now
$mail->AddAddress("recipientAddress");
$mail->setFrom = "myEmail";
$mail->Subject = "Subject";
$mail->Username = "username";
$mail->Password = "password";
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->Send();
so now you would get more debug messages and track your errors and fix them :)
update : okay that is easy :
The function $mail->IsMail(); indicates that the letter must be sent using mail() function. Other methods are:
IsSendmail - via sendmail command.
IsQmail - directly via qMail MTA.
IsSMTP - via SMTP server.

Try removing the isSMTP line so it will use the mail settings configured in PHP.

This looks that your SMTP settings on the www server are wrong.
Check if you can send the e-mail with the SMTP configuration you specified - if not:
is there a SMTP server running on myhost:myport?
Does it listen on the interface you're using to talk to it?
Is it configured to accept mail from your host?
Is it configured to accept mail for the recipients?

Related

DKIM error when sending mail with GSuite using PHP Mailer

I am hosted at A2 hosting, but I am using GSuite to handle all my mail.
When I send test message to mail-tester.com from Gmail I get an awesome rating.
However when I sent a message using my PHP script:
$mail = new PHPMailer(true);
ob_start();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $pickuploc . '#xxxx.com'; // SMTP username
$mail->Password = 'xxx';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom($pickuploc . '#xxx.com', 'xxx xxxx');
$mail->addAddress($email, $fname . " " . $lname); // Add a recipient
$mail->addBCC($pickuploc . '#xxx.com');
// Content
$mail->isHTML(true); // Set email format to HTML
include 'email-confirmed.html';
$mail->Subject = 'Your Reservation Has Been Confirmed!';
$mail->Body = ob_get_clean();
$mail->AltBody = 'Your reservation has been confirmed.';
$mail->send();
I get an error from mail-tester.com that reads my DKIM is not valid.
I think this is because I am sending form a foreign server (not Google) and my MX records point to Google, but I really need these emails to get though, how should I work this problem?
Is there a way to configure this in PHP Mailer? Thanks.
The reason it's complaining is because you're not signing with DKIM at all!
You can do DKIM signing with PHPMailer, but there's a certain amount of setup required. PHPMailer provides some code to help you do this. Make sure you're using PHPMailer 6.1.1 or later; older versions have bugs affecting DKIM signing.
First of all you need to create your DKIM keys and put them in your DNS.
Now you need to alter your script to sign messages using your private key, as shown in this example; the parts you need to add are:
//This should be the same as the domain of your From address
$mail->DKIM_domain = 'example.com';
//See the DKIM_gen_keys.phps script for making a key pair -
//here we assume you've already done that.
//Path to your private key:
$mail->DKIM_private = 'dkim_private.pem';
//Set this to your own selector
$mail->DKIM_selector = 'phpmailer';
//Put your private key's passphrase in here if it has one
$mail->DKIM_passphrase = '';
//The identity you're signing as - usually your From address
$mail->DKIM_identity = $mail->From;
//Suppress listing signed header fields in signature, defaults to true for debugging purpose
$mail->DKIM_copyHeaderFields = false;
//Optionally you can add extra headers for signing to meet special requirements
$mail->DKIM_extraHeaders = ['List-Unsubscribe', 'List-Help'];

Fatal error: Class 'PHPMailer' not found in

Fatal error: Class 'PHPMailer' not found in C:\wamp\www\sendemail.php on line 13
This is line 13:
$mail = new PHPMailer();
I have already researched and they say you need to have these:
require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
But I already have those and they reside in the same folder as sendemail.php but still the same error.
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "email#email.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('email#email.com', 'name');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('email#sample.com', 'name');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Here is my file layout:
I encountered the same issue, and managed to solve it by entering the following two lines at the very top of the PHP script used for sending the mail - not just in the files I required.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
?>
Let me know if it works for you as well.
Add all those extra require lines are not going to help; Just load the autoloader, like the original example code says. Try loading it from an absolute path:
require '/full/path/to/PHPMailerAutoload.php';
If that works, you need to check that your include_path setting in php.ini includes the directory you're loading from - for example that it contains . as one of the paths.
I'm pretty convinced this is an environment/config problem, not code, so try a completely minimal script, just this:
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
If you're doing new development, you should really be using composer anyway - it completely solves include problems and you'll never have to worry about where your libraries are again.

phpMailer: Could not instantiate mail function

I created a form that uses phpMailer to email the results to the website owner. Of course, before I add the owner's email address I use mine to test that the form works. When I use my email the form sends perfectly, however, when I use the owner's address it throws the error "could not instantiate mail function" and won't send the form. The error only occurs with email addresses associated with the owner's domain. Any idea why this could be happening?
If I type this into the command line it works fine:
echo 'test' | mail -s 'test' me#example.com
edit: I wasn't initially using SMTP, but it's now configured as shown below. The error message is now "SMTP Error: The following recipients have failed xxx#somedomain.com" and the end result is still the same. It can e-mail to a number of gmail test addresses but has issue with the owner's email#hisdomain.com. Further, with SMTPDebug it's now producing the error "RCPT TO command failed: 550 No Such User Here" The owner's e-mail, however, works without issue when e-mailed through gmail, outlook, etc.
phpMailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = "error_log";
$mail->Host = "mail.mydomain.com";
$mail->SMTPAuth = true;
$mail->Username = "admin#mydomain.com";
$mail->Password = "XXXXXXXXXXXXXXX";
$mail->CharSet = 'UTF-8';
$mail->AddReplyTo($emailAddress);
$mail->SetFrom("admin#mydomain.com");
$mail->AddAddress($toAddress,$toName);
$mail->Subject = $emailSubject;
$mail->isHTML(true);
$mail->Body = $emailBody;
$mail->AltBody = strip_tags($emailBody);
// Attempt to send the e-mail
if (!$mail->send()) {
//error handling
}
There are couple of things you should try and check with this particular error message:
Make sure you can use regular php mail() function. Create a blank page and use the php mail() to send a test email. If that works, maybe its your SMTP that's having issues with the particular user domain. Setup gmail SMTP or a different SMTP to send emails:
$mail->IsSMTP();
$mail->Host = "smtp.domain.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Can you share your phpMailer source for us to view?
Set $mail->SMTPDebug = 2; so you can see what the server has to say, and read the troubleshooting guide.
You're using authentication without encryption, which is not a good combination and many servers won't allow that. Add this:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You've based your code on an old example, so you're probably using an old version of PHPMailer too; get the latest from github.

Mails sent using PHP taking too much time

I have written a class in PHP which I use for sending mails making use of a Gmail account. This class in turn uses the PHPMailer library. The setup is WAMP 2.4 on Windows Vista. Using the microtime() function in PHP, I see that it takes anywhere between 5 to 6 seconds to send a single mail. Is it normal for a PHP script running on the kind of set up that I have to take as much as 5-6 seconds for a single mail going out. Here is code for the class.
<?php
require_once("phpmailer/class.phpmailer.php");
require_once("phpmailer/class.smtp.php");
class Mailer {
// Needs to be set per object
public $subject;
public $message;
public $to_name;
public $to;
private $mail; // This is the main mail object that'll be initialized
public function __construct() {
// Need to create a PHPMailer object in the constuctor and return it for use in this class.
$mail = new PHPMailer();
$from_name = "bleh";
$from = "bleh#gmail.com";
$username = "bleh";
$password = "bleh";
$mail->FromName = $from_name;
$mail->From = $from;
$mail->Username = $username;
$mail->Password = $password;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
// $mail->Port = 587; // Turns out, I dont need this one.
$mail->SMTPAuth = true; // gmail requires this
$mail->SMTPSecure = 'tls'; // gmail requires this
$this->mail = $mail;
}
function send() {
$mail = $this->mail; // The mail object
$mail->Subject = $this->subject;
$mail->Body = $this->message;
$mail->AddAddress($this->to, $this->to_name);
$result = $mail->Send();
return $result;
}
}
?>
Code used to test this -
$startTime = microtime(true);
require_once("mailer.php");
$mailer = new Mailer();
$mailer->subject = "Test";
$mailer->message = "Test";
$mailer->to_name = "My Name";
$mailer->to = "anemail#address";
$mailer->send();
echo "Time: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
It is very common for SMTP to take a long time - it's even used as an anti-spam measure in the form of greetdelay/tarpit mechanisms. RFC2821 section 4.5.3.2 allows up to a 5 minute delay before traffic starts. SMTP is not intended for interactive use (since it can't queue in that situation), and sending via SMTP during web page submission can suffer because of that. Sendmail or SMTP via an async process would avoid the issue.
In PHPMailer you can enable SMTP debug output and it will show you what's happening so you'll be able to see what's taking the time:
$mail->SMTPDebug = 2;
As mentioned in my comment, Gmail may be rate limiting you. There could also be some aspect of your network communication with Gmail that is causing the issue.
You can manually begin an SMTP conversation with Gmail from the command line. Watch for how long each step takes, and check for any codes / messages that may come back from Gmail indicating a problem.
For details on how to create a manual SMTP conversation see
Connecting to smtp.gmail.com via command line
Messages that come back will be Base64 encoded as indicated in that answer. You can use an online Base64 decoder to convert back to plain text.
Note: The link shows instructions for Linux. If you don't have a Linux server to test from, you can use Cygwin (for windows) or an OpenSSH for Windows package that does not require a full Cygwin install
I have faced to same problem and its getting around 30s to send the email.
The problem was system running PHP server got hang until the email is sent.
I tried many solutions but easiest way was start another PHP server locally on different port for handle the mail sending.
The main PHP server (system running server) will handed over the mail sending request to the mail PHP server and continue no the process.
Still 30s will take to send the email, but not effect to the main PHP server.

php and send mail

I search to send mail with php script.
$mail = new PHPmailer();
$mail->IsSMTP();
$mail->Host='mail.mydomaine.com';
$mail->From='xxx#mydomaine.com';
$mail->AddAddress('xxx#yahoo.fr');
$mail->AddReplyTo('xxx#mydomaine.com');
$mail->Subject='test';
$mail->Body='example for mail';
if i make From address yyy#mydomaine.com it is work but if i change it to example yyy#gmail.com or yahoo.fr it do not work. this the error message
SMTP Error: The following recipients failed: xxx#yahoo.fr
SMTP server error: 5.7.1 : Relay access denied
If you want to use yyy#gmail.com or yyy#yahoo.com as from address, You need to configure respective mail server with authentication(mail account). For example, if you want to configure gmail configure like this..
$mailObj->Host = 'smtp.gmail.com';
$mailObj->Port = '465';
$mailObj->Username = 'yyyy#gmail.com';
$mailObj->Password = 'passwordofaboveaccount';
Now
$mailObj->From='xxx#gmail.com';
will work
you can add the mail for site....
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->Username = 'yyy#gmail.com';//your mail is valuable
$mail->Password = 'password';//your mail pass
if you need to change the mail to yahoo then simply change the host name with the yahoo smtp.......
How could you ever send email with someone elses email address? You can ofcourse only send from your own domain, if this PHP code is on your server.
do you have the following, yes?
$mailObj->SMTPAuth = TRUE;
$mailObj->SMTPSecure = "ssl";

Categories