Email body is blank when receiving it in GMAIL - Swiftmailer

3.2k views Asked by At

I have been having this issue, since I started using Swiftmailer. I know I am new at this so might be doing something wrong. Any help would be appreciated.

Issue is: I receive blank body just in GMAIL but receive everything as expected in OUTLOOK/YAHOO.

I am not sure what the issue is. Thanks in advance for answering.

Here I call the function to send an email with all attributes

    $subject = "CROCT Admin - Evaluate performance of participant for project  - '".$result2['project_name']."'";
    $from = array('[email protected]' => 'Tasty');
    $to = $result4[$i]['email'];
    $body = 'Hello, You have got an invitiation to participate your work in project '.$result2['project_name'].' Accept the invitation and start participating in it.' .anchor('https://example.org/'.$project_id_url.'/'.$result4[$i]['user_id'], 'Accept Invitation');
    $addpart = 'Best,Team CROCT';
    $this->**send_email($subject, $from, $to, $body, $addpart)**;

Here is the function to send_mail

    function send_email($subject, $from, $to, $body, $addpart)
        {
            require_once APPPATH.'libraries/swift_mailer/swift_required.php';
            //Create the Transport
            $transport = Swift_MailTransport::newInstance();
            /*
            You could alternatively use a different transport such as Sendmail or Mail:
            //Sendmail
            $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
            //Mail
            $transport = Swift_MailTransport::newInstance();
            */
            //Create the message
            $message = Swift_Message::newInstance();
            //Give the message a subject
            $message->setSubject($subject)
                    ->setFrom($from)
                    ->setTo($to)
                    ->setBody($body, 'text/plain')
                    ->addPart($addpart, 'text/html')
            ;
            //Create the Mailer using your created Transport
            $mailer = Swift_Mailer::newInstance($transport);
            //Send the message
            $result = $mailer->send($message);
            if ($result) {
                $this->session->set_flashdata('error', 'Invitation sent.');
            } else {
                $this->session->set_flashdata('error', 'Error occured.');
            }
        }

That's how I receive in Gmail Result I am getting in email - Just for GMAIL

That's how I receive in Outlook This is what I receive in OUTLOOK

This is I received after change enter image description here

1

There are 1 answers

8
Jamie Harding On BEST ANSWER

As to the comments, this should be accepted by all major email providers.

Tested:

  • gmail
  • outlook
  • yahoo
  • kerio

MailFunction:

function send_email($subject, $from, $to, $body, $addpart)
        {
            require_once APPPATH.'libraries/swift_mailer/swift_required.php';
            //Create the Transport
            $transport = Swift_MailTransport::newInstance();
            /*
            You could alternatively use a different transport such as Sendmail or Mail:
            //Sendmail
            $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
            //Mail
            $transport = Swift_MailTransport::newInstance();
            */
            //Create the message
            $message = Swift_Message::newInstance();
            //Give the message a subject
            $message->setSubject($subject)
                    ->setFrom($from)
                    ->setTo($to)
                    ->setBody($body, 'text/html')
            ;
            //Create the Mailer using your created Transport
            $mailer = Swift_Mailer::newInstance($transport);
            //Send the message
            $result = $mailer->send($message);
            if ($result) {
                $this->session->set_flashdata('error', 'Invitation sent.');
            } else {
                $this->session->set_flashdata('error', 'Error occured.');
            }
        }

Attributes:

$subject = "CROCT Admin - Evaluate performance of participant for project  - '".$result2['project_name']."'";
$from = array('[email protected]' => 'Tasty');
$to = $result4[$i]['email'];
$body = 'Hello, You have got an invitiation to participate your work in project '.$result2['project_name'].' Accept the invitation and start participating in it.' .anchor('https://example.org/'.$project_id_url.'/'.$result4[$i]['user_id'], 'Accept Invitation').'<br>Best,Team CROCT';
$this->**send_email($subject, $from, $to, $body)**;