How to use my customers' email sending their email as the "Sender" - php

I used to use "Php mailer" to authenticate using a user's email and password to send from their address in my app, but it is such an inconvenience.
So I want to do something like "Mailchimp" does. Mailchimp allows a user to set their email and then Mailchimp just uses user's email as the "Sender".
Please anyone help me with this email issue.

You can't. MailChimp requires you to verify yourself as the owner of the e-mail address and domain through DNS settings. See more details

What you need to do is first verify the user's email address is correct. Here is a good walk-through that shows how to do that:
http://code.tutsplus.com/tutorials/how-to-implement-email-verification-for-new-members--net-3824
You'll also want a disclaimer as proof that they have given you permission to send emails on their behalf.
Disclaimer: by enrolling for this service you are granting this
app permission to send emails on your behalf.
Next you'll want to create a functional email account that belongs to your app only. For example:
clientmailer#myapp.com
Then, last but not least- continue to use PHPMailer, but use it differently. Authenticate and send emails from your app's functional account, but set the AddReplyTo address as your customer's address and name. For example:
select email, firstname from client;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tsl';
$mail->SMTPDebug = 1;
$mail->Host = yourhost#myapp.com;
$mail->Port = EMAIL_PORT;
$mail->Username = clientmailer#myapp.com;
$mail->Password = yourpass;
$mail->SetFrom('clientmailer#myapp.com', 'Functional Mailer');
$mail->AddReplyTo('(email from select)', '(firstname from select)');

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

Send mail from Gsuite mail alias with PHPMailer

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 :)

How to set FROM in php mailer using Gmail SMTP

I was trying to set from in php mailer (gmail smtp). But in inbox it shows from mygmailaccount#gmail.com. here is my code :
$this->_mail->Username = "mygmailaccount#gmail.com";
//Password to use for SMTP authentication
$this->_mail->Password = 'mypassword';
$this->_mail->From="support#mydomain.com";
$this->_mail->FromName="Support Team";
//Set who the message is to be sent from
$this->_mail->setFrom('support#mydomain.com', 'Support Team',false);
But when I receive email in inbox its show from : mygmailaccount#gmail.com
and I want to show from : support#mydomain.com
Can anyone help me to find out what I am missing.
This is a gmail restriction, though it's not all bad news! In your gmail preferences you can configure aliases for your account, and you can send out using them as your from address. So you can set the from address, but only to a choice from that preset list, not any arbitrary address. This question has been asked on here before.

use of PHPMailer about some of its methods e.g. `addReplyTo()` & `setFrom()`

$mail->Host = "smtp-mail.outlook.com";
$mail->Port = 25;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "h*****#outlook.com";
$mail->Password = "********";
$mail->setFrom("h****#gmail.com", "Z***** Hao");
$mail->addReplyTo("a********#yahoo.com", "Z****** Hao");
$mail->addAddress("h******#qq.com", "Z**** Hao");
Above is my code try to use PHPMailer sending email through the outlook smtp server,and my qq mailbox received an email from my outlook account,but I thought it would be sent from my gmail account since I used the setFrom() method,and also how can the $mail->addReplyTo statement play a part in it?
And as a beginner I write it down by referencing an example on github page of PHPMailer project,it's the Link.
As currently configured, you're doing this:
Host, Port, SMTPSecure, SMTPAuth: Configure the mail server you want to use to send out the e-mail. In this case it's the Outlook SMTP server.
Username and Password: Credentials you're using to login to the Outlook SMTP server.
setFrom sets the From header in the email message. You're currently setting this to your Gmail address, so most of the time, the receiving party sees this address in his/her email client in the from field. But: this header isn't always respected by the sending SMTP server. It could be replaced by the email address belonging to your credentials to prevent spam.
addReplyTo allows you to set a different reply-to address. If not set, the client will suggest to sent an email to the address as specified in the From field. This allows you to overwrite it.

Email System in PHP using PHPMailer

Hi i am currently implementing an email system for a customer in php. I'm having a bit of trouble in figuring out something. Here's a sample code:
$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 = "***missing part***";
$mail->Password = "***missing part***";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
My customer already created a business email account on gmail for this website. My question is should i put this business email and password in these missing parts? Anyone could help me please? Thanks.
whose username and password should i put there?
If you want to use GMail, you need to put in the username and password that belong to the GMail account you want to send the message from.
.... which is why sending E-Mail through GMail is a bit of an imperfect solution IMO - you put your personal Google login data, with which you can access everything you do on Google, into a script on a server. It's not great practice security-wise.
It might be more feasible to create a SMTP account on the client's web site, and use that. That has the additional advantage that you can use a email#clients-domain-name.com sender address.
you must post the username and password so we can help you.. LOL, just kidding. You should put there the username and password of the account that would be sending the email. In this case, probably your webpage's gmail account.
Often web hosts don't use authentication for their web servers to send email using their smtp servers, i would suggest contacting the people who host the website and ask them about using their smtp servers.
Using gmail (if you can from a script) might end up with the rather annoying reply thing that you get from gmail where the reply address is a gmail account.
e.g. from mrKoz#gmail.com on behalf of mr#koz.com which doesn't look awesome :)

Categories