Send mail from Gsuite mail alias with PHPMailer - php

I'm trying to use PHPMailer on my website to send mail as part of a contact form. I would like to use contact#example.com instead of standard_email#example.com to send mail, for filtering and security. My mail is handled by GSuite (formerly called Google Apps) and is setup like below:
User - standard_email#example.com
Alias - contact#example.com --> standard_email#example.com
I have sending working perfectly when using standard_email#example.com in the PHPMailer configuration, but when I try to send using the alias it does not work. Is this not possible with GSuite aliases?
Contact Controller
define("SMTP_HOST", "smtp.gmail.com");
define("MAIL_ADDRESS", "alias#example.com");
define("SMTP_USERNAME", "alias#example.com");
...
//Configure SMTP authentication
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPSecure = "tls";
$mail->Port = SMTP_PORT;
//Set email headers
$mail->setFrom(MAIL_ADDRESS, "Contact Us Submission");
$mail->addAddress(MAIL_ADDRESS, "Contact Us Submission");
$mail->addReplyTo($form_email, $form_name);
$mail->isHTML(true);
//Set email content
$mail->Subject = htmlspecialchars($form_subject);
$mail->Body = htmlspecialchars($form_comments);
$mail->AltBody = htmlspecialchars($form_comments);
//Attempt to send the message and display the results to the user
if ($mail->send() == false) {
return new MailResponse(1, "Message could not be sent", $contactSubmission);
} else {
return new MailResponse(0, "Message sent successfully", $contactSubmission);
}
I've also tried using standard_email#example.com as the SMTP_USERNAME and alias#example.com as the MAIL_ADDRESS, but that didn't work either.
Results
There are no reported errors, and the page displays the "success" mail message; however, no mail is actually sent/received when I visit my user. Since GSuite apparently routes all of the alias mail to my standard address, I should be seeing it.
Let me know if I'm missing something, and thanks!

The solution from #Synchro works and for convenience here is a short step by step solution:
On your computer, open Gmail.
In the top right, click Settings Settings and then See all settings.
Click the Accounts and import or Accounts tab.
In the "Send mail as" section, click Add another email address.
Enter your name and the address you want to send from.
Click Next Step and then Send verification.
Click Add Account
Learn more: https://support.google.com/mail/answer/22370?hl=en
Everything should be working now :)

Related

SMTP connect() failed Office 365 OVH [duplicate]

I am attempting to set up PHPMailer so that one of our clients is able to have the automatically generated emails come from their own account. I have logged into their Office 365 account, and found that the required settings for PHPMailer are:
Host: smtp.office365.com
Port: 587
Auth: tls
I have applied these settings to PHPMailer, however no email gets sent (The function I call works fine for our own mail, which is sent from an external server (Not the server serving the web pages)).
"host" => "smtp.office365.com",
"port" => 587,
"auth" => true,
"secure" => "tls",
"username" => "clientemail#office365.com",
"password" => "clientpass",
"to" => "myemail",
"from" => "clientemail#office365.com",
"fromname" => "clientname",
"subject" => $subject,
"body" => $body,
"altbody" => $body,
"message" => "",
"debug" => false
Does anyone know what settings are required to get PHPMailer to send via smtp.office365.com?
#nitin's code was not working for me, as it was missing 'tls' in the SMTPSecure param.
Here is a working version. I've also added two commented out lines, which you can use in case something is not working.
<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'somebody#somewhere.com';
$mail->Password = 'YourPassword';
$mail->SetFrom('somebody#somewhere.com', 'FromEmail');
$mail->addAddress('recipient#domain.com', 'ToEmail');
//$mail->SMTPDebug = 3;
//$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
UPDATE: May 2022
So i was struggling at this Problem really hard. For Business Accounts with Exchange Online and access to the Microsoft Admin Center i can provide the answer for this.
TLDR: Goto the Admin Center and select the User you want to send the Mail. Then look under settings after E-Mail and E-Mail Apps after the Setting "authenticated SMTP", simply enable it.
Still not working? I got you covered, here is how i got it fully working.
Use PHP composer, saves a lot of work actually.
Replace your Code with my Code and change it after test
<?php
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer; //important, on php files with more php stuff move it to the top
use PHPMailer\PHPMailer\SMTP; //important, on php files with more php stuff move it to the top
//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 'path/to/vendor/autoload.php'; //important
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
//$mail->SMTPDebug = SMTP::DEBUG_off;
//SMTP
$mail = new PHPMailer(true); //important
$mail->CharSet = 'UTF-8'; //not important
$mail->isSMTP(); //important
$mail->Host = 'smtp.office365.com'; //important
$mail->Port = 587; //important
$mail->SMTPSecure = 'tls'; //important
$mail->SMTPAuth = true; //important, your IP get banned if not using this
//Auth
$mail->Username = 'yourname#mail.org';
$mail->Password = 'your APP password';//Steps mentioned in last are to create App password
//Set who the message is to be sent from, you need permission to that email as 'send as'
$mail->SetFrom('hosting#mail.org', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use yourname#mail.org
//Set an alternative reply-to address
$mail->addReplyTo('no-reply#mail.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('customer#othermail.com', 'SIMON MÜLLER');
//Set the subject line
$mail->Subject = 'PHPMailer 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('replace-with-file.html'), __DIR__); //you can also use $mail->Body = "</p>This is a <b>body</b> message in html</p>"
//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 {
}
This may looks like your file, whats ok, but now comes the easy-tricky part. As like as Google, Microsoft implemented a "switch" for the SMTP stuff. Simply go to your Admin Center from your Business Account, or kindly ask someone with permission to do that part:
navigate to https://admin.microsoft.com/AdminPortal/Home#/users
select to user where you want to send the email from
under the tab "E-Mail" search for "E-Mail-Apps", click on "manage E-Mail-Apps"
here you can select "authenticated SMTP", make sure that option is checked and save the Changes
if you use MFA, then make sure you use an app password as mentioned in https://stackoverflow.com/a/61359150/14148981
Run the script
I hope this helps someone. Took me hell long to find this option on myself.
Summary of all steps to get App password and authentication ON:
With Admin Account:
Active Directory AD -> Properties -> Security Default: Turn OFF.
Active directory Portal -> conditional access -> Configure MFA Trusted IPs -> Allow user App password: Enable
Admin Page User List -> 'Multi factor Authentication' for target user: Enable then Enforce
Admin Page User List -> User details -> Mail -> 'Manage Email Apps' -> 'Authenticated SMTP': Enable
With user account:
User Account Profile -> Security -> add login method: App Password
PHP Mailer Settings:
smtp.office365.com, 587, tls, email, appPassword
UPDATE: April 2020
Using the accepted answer for sending email using Office 365 has the high chance of not working since Microsoft is pushing for their Microsoft Graph (the only supported PHP framework right now is Laravel). If fortunately you were still able to make it work in your application, email will either go to the recipient's Junk, Trash, or Spam folder, which you don't want to happen.
Common errors I encountered were:
Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!
or
Failed to send AUTH LOGIN command.
or
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
In order to still make it work with the accepted answer, we just have to change a single line, which is the Password parameter line:
$mail->Password = 'YourOffice365Password';
Instead of setting the password with the one you use when you login to your Office365 account, you have to use an App Password instead.
Create App Password
First, in order to create an App Password, the Multi-Factor Authentication of your Office 365 account should be enabled (you may have to contact your administrator for this to be enabled).
After that, login your Office 365 in your favorite browser
Go to My Account page (you will see the link to this page when you click your name's initials on the upper right)
Choose Security & Privacy then Additional security verification
At the top of the page, choose App Passwords
Choose create to get an app password
If prompted, type a name for your app password, and click Next
You will then see the password generated by Office 365 as your App Password
Copy the password
After copying the password, go back to your working code and replace the Password parameter with the copied password. Your application should now be able to properly send email using Office 365.
Reference:
Create an app password for Microsoft 365
Try this, it works fine for me, i have been using this for so long
$mail = new PHPMailer(true);
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPSecure = '';
$mail->SMTPAuth = true;
$mail->Username = "email";
$mail->Password = "password";
$mail->SetFrom('email', 'Name');
$mail->addReplyTo('email', 'Name');
$mail->SMTPDebug = 2;
$mail->IsHTML(true);
$mail->MsgHTML($message);
$mail->Send();
I had the same issue when we moved from Gmail to Office365.
You MUST set up a connector first (either an open SMTP relay or Client Send). Read this and it will tell you everything you need to know about allowing Office365 to send email:
https://technet.microsoft.com/en-us/library/dn554323.aspx
Update: Dec 2020
I resolved the problem by NOT setting the App Password and MFA(Disable it!).
I did it by disabling MS security default at
Microsoft 365 admin center
Azure Active Directory admin center
Azure Active Directory
Properties
Manage Security defaults
Enable Security defaults
No
When it's set, send email with your Office365 username(Email Address) and password (Not App Password)
I was facing this error during configuration of PHPMailer 5.2 stable with outlook SMTP. I found this:
SMTP ERROR:
Password command failed, Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant.
SMTP connect() failed.
After some research I have resolved this issue by enabling the Authenticated SMTP
Use the Microsoft 365 admin center to enable or disable SMTP AUTH on specific mailboxes
Open the Microsoft 365 admin center and go to Users > Active users.
Select the user, and in the flyout that appears, click Mail.
In the Email apps section, click Manage email apps.
Verify the Authenticated SMTP setting: unchecked = disabled, checked = enabled.
When you're finished, click Save changes.
I modified the example in the current PHPmailer version 6.5.4 to allow for Microsoft 365 non auth TLS connection (ie you do not need to use an license user account but can use a shared mailbox as the sender)
$mail = new PHPMailer(true);
try {
//Enable SMTP debugging
// $mail->SMTPDebug = 3; // connection, client and server level debug!
//SMTP
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->Host = 'domain-xyz.mail.protection.outlook.com';
$mail->Port = 25;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->SMTPAuth = false;
// as advised OPTION #3 on this page
// https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365
//Auth
$mail->Username = 'website#domain.xyz';
$mail->Password = 'yourpassword'; // set SMTPAuth to false though right so this is not used?
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//you need permission to that email as 'send as'
$mail->SetFrom('website#website#domain.xyz', 'Website');
$mail->addReplyTo('website#website#domain.xyz', 'Website');
// Sending to a external recipient
$mail->addAddress('external#domain.test', 'John doe');
$mail->Subject = 'PHPMailer TESTY TEST';
$mail->isHTML(true);
$mail->Body = "</p>This is a <b>body</b> message in html</p>";
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('img/logo.png');
//send the message, check for errors
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Update september 2021
As of September 2022 (so from now on only a year) Microsoft will deprecating and shut down what they call basic authentication. That includes, but is not limited to, SMTP. They later announced that tenant administrators will be able to re-enable SMTP_AUTH, but (in my opinion) it opens up the door for permanently disabling it in the feature.
For now, Microsoft is not disabling it, but they encourage you to authenticate on different ways. On the documentation site they placed a warning telling you this.
Today I will do some research on how to use this new methods. For my application (PHP, CodeIgniter) I found this Microsoft package. I'm going to try sending my mails using this package, I hope this will be a smooth experience, but I'm afraid it will be a hell of a ride... I'll keep you all posted.
You need an App Password and multi-factor authentication enabled to able to send mails from third party applications using office365 stmp. Google it for further information

Php mailer could not authenticate error all of a sudden with gmail

This code was working since an year but suddenly stopped working today. I didn't change gmail password or anything. Did gmail update something ?
How do I troubleshoot this ?
Error:
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
require_once '../db/config.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function send_email($email, $code) {
require_once '../ml/PHPMailer/PHPMailer.php';
require_once '../ml/PHPMailer/SMTP.php';
require_once '../ml/PHPMailer/Exception.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'aaaabbbbb';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('example#gmail.com', 'mycompany');
$mail->addAddress($email);
$mail->addReplyTo('example#gmail.com', 'mycompany');
// Content
$mail->isHTML(true);
$mail->Subject = 'Verification code';
$mail->Body = 'Use the verification code to activate your account.<br> <span style="font-size: 20px;">'.$code.'</code>';
$mail->AltBody = 'Use the verification code '.$code.' to activate your account';
$mail->send();
echo 'Verification code has been sent to your email '.$email;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
I recently implemented PHPMailer on my website with Gmail as the mail server etc. (had to replace swift mailer), so putting this here for anyone who needs this. For Gmail to work,
First, go ahead and log in to the GMAIL account that you wish to use
as the mail server.
Once logged in, click this link which will take you to your accounts
https://myaccount.google.com/
Once that's loaded, on the left-hand side column, click on the "Security" button.
Once that page loads, you will see some boxes and one of them will
say "Signing in to google". In it, you should see some options with
one of them being "App Passwords". Should look something like
this
(Yellow highlight below is the button you need):
Please note: if you don't see it(yellow highlighted "app password" button, then this means that you have to enable 2-Step verification first.
Assuming you have 2FA enabled, click on the "app passwords" button and
sign in again.
On the next screen, simply hit the "Select App" dropdown button and
then select "Other (Custom Name)". Give it a name and click
"generate".
This will bring up a popup with a long password on it. Make sure to save the password temporarily while you work on your app because once you close out of that popup, you cant view the password again. If you lose it, you'll have to create another one.
From here, all you have to do is go to your code and replace your regular GMAIL email password, with your newly generated app password.
Make sure to have the host address as "smtp.gmail.com"
the port is 465
and the encryption type as SMTP or
"$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;"
And that's it. You should be good to go. Good luck and happy coding
Google stopped less secure app support for non Google Business suite users from 30 May 2022.
You can use any other email service providers. If you have your own domain with email hosting, you can use one email account as smtp.

PHP mail function not working in godaddy account [duplicate]

I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.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 as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.

SetFrom PHPMailer not working

I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "myusername#gmail.com";
$mail->Password = "gmail_password";
$mail->From = 'donotreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->AddAddress('Toreceiver#test.com', 'Receiver'); // Add a recipient
$mail->IsHTML(true);
$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;
$mail->Body = "This is in <b>Blod Text</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;
exit;
}
echo 'Message has been sent';
?>
It is sending mail from myusername#gmail.com but I want it to send with 'donotreply#mydomain.com' as set in $mail->From. Any help will be highly appreciated.
Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.
My solution was to add
$mail->AddReplyTo($FromEmail, $FromName);
This way at least if you reply to the message it will be delivered as described
There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.
Use:
$mail->SetFrom('donotreply#mydomain.com', 'Admin');
Instead of $mail->From and $mail->FromName.
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
settings > Accounts and import > Send mail as:
set another email which you want to use as from:
if you are using zoho then you can follow:
settings > Mail tab > Send mail as > add from address
then verify that email.
For GSuite users...
What Jyohul said, is the correct way of doing it. You can add an alias in Google Admin Console, under "Users". Click on the user's name, and then click on "user information". In there you can add Aliases. Once the aliases are added, you can then do what Jyohul said, which I added a needed step...:
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
Go to your Gmail, then click the gear on the top right, then:
settings > Accounts > Send mail as: then click "Add another email address".
Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.
$mail->SMTPDebug = 2;
You will the identify the error. Also make sure your SMTP Server credentials are correct. Like host, username and password.
Mine worked correctly

How to know mail is send and read by user when sending mail using SMTP,PHPmailer

is anybody know any solution for below problem
in my project, i am send email to client using smtp and php mailer and gmail script. so when i am send mail gmail send mail to particular client. for that i am passing gmail login and user name which is valid. all mail are sending properly. but sometime it may happen that some client not receive mail and at that time i am not able to get or trace any error. so i there any way, when i am sending mail to client , and when client get it and read it at that time i got any kind of confirmation.
Please if anybody have any idea , help me out.
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* #package phpmailer
* #version $Id$
*/
require ('../class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = "Please return read receipt to me.";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "SMTP SERVER IP/DOMAIN"; // SMTP server
$mail->Username = "EMAIL USER ACCOUNT"; // SMTP server username
$mail->Password = "EMAIL USER PASSWORD"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("someone#something.com","SOMEONE");
$mail->From = "someone#something.com";
$mail->FromName = "SOMEONE";
$to = "other#something.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message[Test Read Receipt]";
$mail->ConfirmReadingTo = "someone#something.com"; //this is the command to request for read receipt. The read receipt email will send to the email address.
$mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Some modification need to be done in above script.
Configure SMTP mail server.
Set the correct FROM & FROM Name (someone#something.com, SOMEONE)
Set the correct TO address
from
also
Delivery reports and read receipts in PHP mail
You can always add reply to mail address which will take care if there is any error so you will get email back, and for if it's read, include a picture (blank picture of 1 pixel will do) and add code in that picture like and you can see how many hits that image did recieve or recieved any at all.
You can check that things in two different ways like, by using the header "Disposition-Notification-To" to your mail function, it will not going to work in all case because most people choose not to send read receipts. If you could, from your server, influence whether or not this happened, a spammer could easily identify active and inactive email addresses quite easily.
You can set header like
$email_header .= "Disposition-Notification-To: $from";
$email_header .= "X-Confirm-Reading-To: $from";
now the another method to check is by placing an image and you can add a script to onload of that image, that script will send notification to your system that xxx user have read the mail so, in that way you can track delivery status of the mail.
In fact, I can't think of any way to confirm it's been delivered without also checking it gets read, either by including a image that is requested from your server and logged as having been accessed, or a link that the user must visit to see the full content of the message.
Neither are guaranteed (not all email clients will display images or use HTML) and not all 'delivered' messages will be read.
Though for more info https://en.wikipedia.org/wiki/Email_tracking

Categories