How to get email id when using symfony mailer?

Member

by rollin , in category: PHP Frameworks , 9 months ago

How to get email id when using symfony mailer?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 9 months ago

@rollin 

To get the email id when using Symfony Mailer, you can extract the email id from the message object before sending the email. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use SymfonyComponentMimeEmail;

$email = (new Email())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Hello!')
    ->text('This is the email body.')
    ->html('<p>This is the email body.</p>');

$recipientEmail = $email->getTo()[0]->getAddress();

// Now you can use $recipientEmail to get the email id of the recipient
echo $recipientEmail;


In the above code, we create an Email object, set the sender, recipient, subject, and body of the email. We then extract the email id of the recipient from the Email object using the getTo()[0]->getAddress() method.


This is how you can get the email id when using Symfony Mailer.