【问题标题】:How to pass multiple headers for swift mailer php如何为swift mailer php传递多个标题
【发布时间】:2019-09-06 10:10:51
【问题描述】:

我正在使用 php 在 Outlook 中创建会议,我发现了一些示例并尝试实现它并且它的工作但它正在使用 php mail() method,在 php 邮件方法中我知道如何传递标题但我想要的项目实现正在使用 swift mailer,我不知道如何在 swift mailer 中定义这些标头,以下是使用 php mail() 的工作示例

$from_name = "Some One";        
$from_address = "def@abc.com";        
$to_name = "Soem Two";        
$to_address = "xyz@abc.com";        

$date               = '20190905';
$startTime          = '13:20:00';
$endTime            = '19:00:00';   

$subject = "Standup Meeting";        
$description = "The purpose of the meeting is to discuss works done and inprogress";        
$location = "ABCD EFGH";
$domain = 'mydomain.com';

//Create Email Headers
$mime_boundary = "----Meeting Booking----".MD5(TIME());

$headers = "From: ".$from_name." <".$from_address.">\n";
$headers .= "Reply-To: ".$from_name." <".$from_address.">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-class: urn:content-classes:calendarmessage\n";

//Create Email Body (HTML)
$message = "--$mime_boundary\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= "<html>\n";
$message .= "<body>\n";
$message .= '<p>Dear '.$to_name.',</p>';
$message .= '<p>'.$description.'</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$message .= "--$mime_boundary\r\n";

$ical = 'BEGIN:VCALENDAR' . "\r\n" .
'PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN' . "\r\n" .
'VERSION:2.0' . "\r\n" .
'METHOD:REQUEST' . "\r\n" .
'BEGIN:VTIMEZONE' . "\r\n" .
'TZID:Eastern Time' . "\r\n" .
'BEGIN:STANDARD' . "\r\n" .
'DTSTART:20091101T020000' . "\r\n" .
'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11' . "\r\n" .
'TZOFFSETFROM:-0400' . "\r\n" .
'TZOFFSETTO:-0500' . "\r\n" .
'TZNAME:EST' . "\r\n" .
'END:STANDARD' . "\r\n" .
'BEGIN:DAYLIGHT' . "\r\n" .
'DTSTART:20090301T020000' . "\r\n" .
'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3' . "\r\n" .
'TZOFFSETFROM:-0500' . "\r\n" .
'TZOFFSETTO:-0400' . "\r\n" .
'TZNAME:EDST' . "\r\n" .
'END:DAYLIGHT' . "\r\n" .
'END:VTIMEZONE' . "\r\n" .  
'BEGIN:VEVENT' . "\r\n" .
'ORGANIZER;CN="'.$from_name.'":MAILTO:'.$from_address. "\r\n" .
'ATTENDEE;CN="'.$to_name.'";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:'.$to_address. "\r\n" .
'LAST-MODIFIED:' . date("Ymd\TGis") . "\r\n" .
'UID:'.date("Ymd\TGis", strtotime($startTime)).rand()."@".$domain."\r\n" .
'DTSTAMP:'.date("Ymd\TGis"). "\r\n" .
'DTSTART:'.$date."T".$startTime."00Z\r\n" .
'DTEND:'.$date."T".$endTime."00Z\r\n" .
'TRANSP:OPAQUE'. "\r\n" .
'SEQUENCE:1'. "\r\n" .
'SUMMARY:' . $subject . "\r\n" .
'LOCATION:' . $location . "\r\n" .
'CLASS:PUBLIC'. "\r\n" .
'PRIORITY:5'. "\r\n" .
'BEGIN:VALARM' . "\r\n" .
'TRIGGER:-PT15M' . "\r\n" .
'ACTION:DISPLAY' . "\r\n" .
'DESCRIPTION:Reminder' . "\r\n" .
'END:VALARM' . "\r\n" .
'END:VEVENT'. "\r\n" .
'END:VCALENDAR'. "\r\n";
$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST'."\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= $ical;

$mailsent = mail($to_address, $subject, $message, $headers);

if($mailsent)
{
    echo 'Meeting email sent';
}
else
{
    echo 'Problem in sending email';
}

但是我想在 swift mailer 中使用它 我想要这个。

 $from_name = "Some One";        
        $from_address = "def@abc.com";        
        $to_name = "Soem Two";        
        $to_address = "xyz@abc.com";       


        $date               = '20190905';
        $startTime          = '13:20:00';
        $endTime            = '19:00:00';   

        $subject = "Standup Meeting";        
        $description = "The purpose of the meeting is to discuss works done and inprogress";        
        $location = "abcfer ";
        $domain = 'mydomain.com';

        //Create Email Headers
        $mime_boundary = "----Meeting Booking----".MD5(TIME());

        $headers = "From: ".$from_name." <".$from_address.">\n";
        $headers .= "Reply-To: ".$from_name." <".$from_address.">\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
        $headers .= "Content-class: urn:content-classes:calendarmessage\n";

        //Create Email Body (HTML)
        $message = "--$mime_boundary\r\n";
        $message .= "Content-Type: text/html; charset=UTF-8\n";
        $message .= "Content-Transfer-Encoding: 8bit\n\n";
        $message .= "<html>\n";
        $message .= "<body>\n";
        $message .= '<p>Dear '.$to_name.',</p>';
        $message .= '<p>'.$description.'</p>';
        $message .= "</body>\n";
        $message .= "</html>\n";
        $message .= "--$mime_boundary\r\n";

        $ical = 'BEGIN:VCALENDAR' . "\r\n" .
        'PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN' . "\r\n" .
        'VERSION:2.0' . "\r\n" .
        'METHOD:REQUEST' . "\r\n" .
        'BEGIN:VTIMEZONE' . "\r\n" .
        'TZID:Eastern Time' . "\r\n" .
        'BEGIN:STANDARD' . "\r\n" .
        'DTSTART:20091101T020000' . "\r\n" .
        'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11' . "\r\n" .
        'TZOFFSETFROM:-0400' . "\r\n" .
        'TZOFFSETTO:-0500' . "\r\n" .
        'TZNAME:EST' . "\r\n" .
        'END:STANDARD' . "\r\n" .
        'BEGIN:DAYLIGHT' . "\r\n" .
        'DTSTART:20090301T020000' . "\r\n" .
        'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3' . "\r\n" .
        'TZOFFSETFROM:-0500' . "\r\n" .
        'TZOFFSETTO:-0400' . "\r\n" .
        'TZNAME:EDST' . "\r\n" .
        'END:DAYLIGHT' . "\r\n" .
        'END:VTIMEZONE' . "\r\n" .  
        'BEGIN:VEVENT' . "\r\n" .
        'ORGANIZER;CN="'.$from_name.'":MAILTO:'.$from_address. "\r\n" .
        'ATTENDEE;CN="'.$to_name.'";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:'.$to_address. "\r\n" .
        'LAST-MODIFIED:' . date("Ymd\TGis") . "\r\n" .
        'UID:'.date("Ymd\TGis", strtotime($startTime)).rand()."@".$domain."\r\n" .
        'DTSTAMP:'.date("Ymd\TGis"). "\r\n" .
        'DTSTART:'.$date."T".$startTime."00Z\r\n" .
        'DTEND:'.$date."T".$endTime."00Z\r\n" .
        'TRANSP:OPAQUE'. "\r\n" .
        'SEQUENCE:1'. "\r\n" .
        'SUMMARY:' . $subject . "\r\n" .
        'LOCATION:' . $location . "\r\n" .
        'CLASS:PUBLIC'. "\r\n" .
        'PRIORITY:5'. "\r\n" .
        'BEGIN:VALARM' . "\r\n" .
        'TRIGGER:-PT15M' . "\r\n" .
        'ACTION:DISPLAY' . "\r\n" .
        'DESCRIPTION:Reminder' . "\r\n" .
        'END:VALARM' . "\r\n" .
        'END:VEVENT'. "\r\n" .
        'END:VCALENDAR'. "\r\n";
        $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST'."\n";
        $message .= "Content-Transfer-Encoding: 8bit\n\n";
        $message .= $ical;

         $message=Yii::$app->mailer->compose()
        ->setFrom($from_address)
        ->setTo($to_address)
        ->setSubject('This is subject') // ???? ??????
        ->addTextHeader($headers)
        ->setHtmlBody($message)
        ->send();
        var_dump($message);

【问题讨论】:

    标签: php email yii2 swiftmailer


    【解决方案1】:

    您正在使用 swiftmailer 以避免需要自己设置标题。您可以发送带有所需标题的邮件,如下所示:

    $from_name = "Some One";
    $from_address = "def@abc.com";
    $to_name = "Soem Two";
    $to_address = "xyz@abc.com";
    
    
    $date               = '20190905';
    $startTime          = '13:20:00';
    $endTime            = '19:00:00';
    
    $subject = "Standup Meeting";
    $description = "The purpose of the meeting is to discuss works done and inprogress";
    $location = "acfr ffs";
    $domain = 'my.com';
    
    /**
     * @var \yii\swiftmailer\Message $mail
     */
    $mail=Yii::$app->mailer->compose()
        ->setFrom([$from_address => $from_name])
        ->setReplyTo([$from_address => $from_name])
        ->setSubject($subject)
        ->setTo([$to_address => $to_name]);
    
    $mail->addHeader('Content-class', 'urn:content-classes:calendarmessage');
    
    //Create Email Body (HTML)
    $message = "<html>\n";
    $message .= "<body>\n";
    $message .= '<p>Dear '.$to_name.',</p>';
    $message .= '<p>'.$description.'</p>';
    $message .= "</body>\n";
    $message .= "</html>\n";
    
    $ical = 'BEGIN:VCALENDAR' . "\r\n" .
        ... skipped the body for better readability ...
        'END:VCALENDAR'. "\r\n";
    
    $swiftMail = $mail->getSwiftMessage();
    $swiftMail->setContentType('multipart/alternative');
    $swiftMail->addPart($message, 'text/html');
    
    $swiftMail->addPart(
        $ical,
        'text/calendar;name="meeting.ics";method=REQUEST',
        null
    );
    
    
    $mail->send();
    

    如果您不需要这个特定的标题并且您的目标只是发送日历事件,您可以让它变得更简单

    $from_name = "Some One";
    $from_address = "def@abc.com";
    $to_name = "Soem Two";
    $to_address = "xyz@abc.com";
    
    
    $date               = '20190905';
    $startTime          = '13:20:00';
    $endTime            = '19:00:00';
    
    $subject = "Standup Meeting";
    $description = "The purpose of the meeting is to discuss works done and inprogress";
    $location = "cfrt hjd";
    $domain = 'my.com';
    
    $mail=Yii::$app->mailer->compose()
        ->setFrom([$from_address => $from_name])
        ->setReplyTo([$from_address => $from_name])
        ->setSubject($subject)
        ->setTo([$to_address => $to_name]);
    
    //Create Email Body (HTML)
    $message = "<html>\n";
    $message .= "<body>\n";
    $message .= '<p>Dear '.$to_name.',</p>';
    $message .= '<p>'.$description.'</p>';
    $message .= "</body>\n";
    $message .= "</html>\n";
    
    $ical = 'BEGIN:VCALENDAR' . "\r\n" .
        ... skipped the body for better readability ...
        'END:VCALENDAR'. "\r\n";
    
    $mail->setHtmlBody($message);
    $mail->attachContent(
        $ical,
        [
            'contentType' => 'text/calendar;name="meeting.ics";method=REQUEST'
        ]
    );
    
    $mail->send();
    

    【讨论】:

    • 但是它作为附件发送 ics 存在问题,我与邮件共享的代码是目录创建事件
    • 有没有办法直接创建事件而不是发送 ical 作为附件?
    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2014-08-30
    • 2019-03-30
    相关资源
    最近更新 更多