Want to display the from email using PHPMailder and Gsuite

subrat4u

New member
Hi,

I want to display from email by using PHPMailer and Gsuite.

Example:

$mail->setFrom("xyz@gmail.com", "xyz") is not working. I am sending it via subrat*****@gmail.com but want to show xyz@gmail.com in the mail as from email.

Please help.
 

Attachments

  • Capture.PNG
    Capture.PNG
    8.3 KB · Views: 0
Did you check the official documentation?
By default the gmail will mark your app as unsecure and block. Last time when I used the gmail and php there was an option to unblock the unsecure app, but now it was deprecated
 
When using PHPMailer and G Suite, you need to ensure that the "From" email address matches an existing email address associated with your G Suite account. G Suite has certain restrictions in place to prevent unauthorized usage of email addresses.
To display "xyz@gmail.com" as the "From" email address in the sent emails, follow these steps:
Make sure you have added and verified the "xyz@gmail.com" email address in your G Suite account.
Set up the SMTP credentials and configuration for PHPMailer to use your G Suite SMTP server. Here's an example:
php
Copy code
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Adjust the path to the PHPMailer autoloader
// Create a new PHPMailer instance
$mail = new PHPMailer(true);
try {
// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'subrat*****@gmail.com'; // Your G Suite email address
$mail->Password = 'your_password'; // Your G Suite email password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Set the "From" email address and name
$mail->setFrom('xyz@gmail.com', 'xyz');
// Add recipients, subject, and content to your email
// Send the email
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo 'Email could not be sent. error:
', $mail->ErrorInfo;
}
In the code above, replace "subrat*****@gmail.com" with your actual G Suite email address and "your_password" with your G Suite email password.
Set the "From" email address with $mail->setFrom('xyz@gmail.com', 'xyz'). like you said. This sets the From email address to xyz@gmail.com and the From name to xyz.
Make sure the email address 'xyz@gmail.com' is associated with your G Suite account and has been added and verified.
After following these steps, you should be able to use PHPMailer to send emails where the 'From' email address appears as 'xyz@gmail.com'.
 
Back
Top