电子邮件服务的冗余

This is a little bit a newbie question I know. But however I couldn't find an answer to this question.

I have made some websites that leverage the functionality of automatic emailling. I have made this websites using PHP. Every website I do, in the mailling part, I come accross some "redundancies". Let me give an example, from the examples of PHPMailer library:

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = 'mail.domail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'someuser@domain.com';                 // SMTP username
    $mail->Password = 'secret';                       
    $mail->Port = 587;

    $mail->setFrom('someuser@domain.com', 'Mailer');
    $mail->addAddress('to@gmail.com', 'Joe User');     // Add a recipient
    $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';

In these two statements, is where I thought there are redundancies: $mail->Username = "someuser@domain.com; $mail->Password = 'secret';" and $mail->setFrom('someuser@domain.com'). Here is my question? Why do I need to provide a "from" address if I already given a username and password. Shoudln't it simply log in to my email account and sent it? If I provide a user name, why do I also provide a "from" address? And vice versa.

Could someone explain the reason why mailling systems work like this? I have alson seen similar structure in python's standard mailling library.

Because "from" does not have to be the same as your log in username. You can try to send yourself a message with a different from value and check your inbox or spam folder to see the result.

In a Microsoft Exchange server (just an example), you are able to Send As many users mailboxes under your mailbox login when the correct permissions are applied. So in this case, if you were using PHPMailer to send mail to a Microsoft Exchange server and authenticate, you would use the same credentials to access your mailbox, but then specify the mailbox you would like to send as in the From variable.

As well as the above, From is part of the current SMTP standard (RFC 821) and MUST be used when communicating with an SMTP server. So this means all mail-servers using SMTP should ask for the From field to meet this standard despite if they understand who is sending the email.