Back to Mail class

Method setSender

public \Joomla\CMS\Mail\Mail|bool
setSender
(mixed $from)
Set the email sender
Parameters
  • mixed $from email address and Name of sender <code>array([0] => email Address, [1] => Name)</code> or as a string
Returns
  • \Joomla\CMS\Mail\Mail|bool Returns this object for chaining on success or boolean false on failure.
Since
  • 1.7.0
-
  • \UnexpectedValueException if the sender is not a valid address
  • \PHPMailer\PHPMailer\Exception if setting the sender failed and exception throwing is enabled
Class: Mail
Project: Joomla

Method setSender - Source code

/**
 * Set the email sender
 *
 * @param   mixed  $from  email address and Name of sender
 *                        <code>array([0] => email Address, [1] => Name)</code>
 *                        or as a string
 *
 * @return  Mail|boolean  Returns this object for chaining on success or boolean false on failure.
 *
 * @since   1.7.0
 *
 * @throws  \UnexpectedValueException  if the sender is not a valid address
 * @throws  phpmailerException 			if setting the sender failed and exception throwing is enabled
 */
public function setSender($from)
{
    if (\is_array($from)) {
        // If $from is an array we assume it has an address and a name
        if (isset($from[2])) {
            // If it is an array with entries, use them
            $result = $this->setFrom(MailHelper::cleanLine($from[0]), MailHelper::cleanLine($from[1]), (bool) $from[2]);
        } else {
            $result = $this->setFrom(MailHelper::cleanLine($from[0]), MailHelper::cleanLine($from[1]));
        }
    } elseif (\is_string($from)) {
        // If it is a string we assume it is just the address
        $result = $this->setFrom(MailHelper::cleanLine($from));
    } else {
        // If it is neither, we log a message and throw an exception
        Log::add(Text::sprintf('JLIB_MAIL_INVALID_EMAIL_SENDER', $from), Log::WARNING, 'jerror');
        throw new \UnexpectedValueException(sprintf('Invalid email sender: %s', $from));
    }
    if ($result === false) {
        return false;
    }
    return $this;
}