1
votes

I am trying to send pdf attachments in mail using amazon SES sendmail() function in php . I have written a function which takes MIME type as content and send a mail. But I am not able to send attachment in the mail. the file paths and all the other values are seems perfect.

The functions code is as follows :

/*
* Function sendRawMail() is used to send mails to user with attachments
*/
public function sendRawMail($subject, $body='', $to, $cc = '',$bcc = '', $filetype,$filename,$filepath) 
{

    $domain = explode('@', $to);
    if (count($domain) > 1 && $domain[1] == 'guest.com') {
        $to = '[email protected]';
    }

    $destination = array();
    $destination['ToAddresses'] = array($to);
    if($cc != '')
    {
        $cc = explode(',', $cc);
        $destination['CcAddresses'] = $cc;
    }
    if($bcc != '')
    {
        $bcc = explode(',', $bcc);
        $destination['BccAddresses'] = $bcc;
    }


    $replyTo = '[email protected]';

    $client = SesClient::factory(array(
        'key' => Yii::$app->params['aws.id'],
        'secret' => Yii::$app->params['aws.secret'],
        'region' => 'us-east-1',
    ));

    $message= "To: ".$to."\n";
    $message.= "From: ".$replyTo."\n";
    $message.= "Subject: ".$subject."\n";
    $message.= "MIME-Version: 1.0\n";
    $message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= 'Content-Type: text/plain; charset="utf-8"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: 7bit\n";
    $message.= "Content-Disposition: inline\n";
    $message.= "\n";
    $message.= $body;
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= "Content-ID: \<[email protected]_IS_ADDED\>\n";
    $message.= 'Content-Type: application/'.$filetype.'; name="'.$filename.'"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: base64\n";
    $message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
    $message.= "\n";
    $message.= base64_encode(file_get_contents($filepath));
    $message.= "\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number--\n";

    $result = $client->SendRawEmail(array(
        // Source is required
        'Source' => '​​​​Knowlens Solutions Pvt. Ltd. <[email protected]>',
        // Destination is required
        'Destination' => $destination,
        // Message is required
        'RawMessage' => array(
            // Data is required
            'Data' => base64_encode($message),
        ),

    ));

}

Mail is successfully sent to the user, but without attachment. Please help.

2

2 Answers

0
votes

The total size of the message cannot exceed 10 MB. This includes any attachments that are part of the message. Have you checked the size of your pdf file?

0
votes

Thanks. It worked for me. Updated code as follows :

Function sendRawMail() is used to send mails to user (AWS mail with attachment)

public function sendRawMail($subject, $body='', $to, $cc = '',$bcc = '', $filetype,$filename,$filepath) 
{
    $precc = $cc;
    $prebcc = $bcc;

    $domain = explode('@', $to);
    if (count($domain) > 1 && $domain[1] == 'ABC.com') {
        $to = '[email protected]';
    }

    $destination = array();
    $destination['ToAddresses'] = array($to);
    if($cc != '')
    {
        $cc = explode(',', $cc);
        $destination['CcAddresses'] = $cc;
    }
    if($bcc != '')
    {
        $bcc = explode(',', $bcc);
        $destination['BccAddresses'] = $bcc;
    }


    $replyTo = '[email protected]';

    $client = SesClient::factory(array(
        'key' => Yii::$app->params['aws.id'],
        'secret' => Yii::$app->params['aws.secret'],
        'region' => 'us-east-1',
    ));

    $message= "To: ".$to."\n";
    $message.= "From: ".$replyTo."\n";
    if($precc != '')
    {
        $message.= "Cc: ".$precc."\n";
    }
    if($prebcc != '')
    {
        $message.= "Bcc: ".$prebcc."\n";
    }
    $message.= "Subject: ".$subject."\n";
    $message.= "MIME-Version: 1.0\n";
    $message.= 'Content-Type: multipart/mixed; boundary="aRandomString_with_signs_or_9879497q8w7r8number"';
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= 'Content-Type: text/html; charset="utf-8"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: 7bit\n";
    $message.= "Content-Disposition: inline\n";
    $message.= "\n";
    $message.= $body;
    $message.= "\n\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number\n";
    $message.= "Content-ID: \<[email protected]_IS_ADDED\>\n";
    $message.= 'Content-Type: application/'.$filetype.'; name="'.$filename.'"';
    $message.= "\n";
    $message.= "Content-Transfer-Encoding: base64\n";
    $message.= 'Content-Disposition: attachment; filename="'.$filename.'"';
    $message.= "\n";
    $message.= base64_encode(file_get_contents($filepath));
    $message.= "\n";
    $message.= "--aRandomString_with_signs_or_9879497q8w7r8number--\n";
    $result = $client->SendRawEmail(array(
        // Source is required
        'Source' => 'ABC Solutions Pvt. Ltd. <[email protected]>',
        // Destination is required
        'Destination' => $destination,
        // Message is required
        'RawMessage' => array(
            // Data is required
            'Data' => base64_encode($message),
        ),

    ));
    return $result;
}