[SOLVED] Can't get to send HTML emails with this?

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

[SOLVED] Can't get to send HTML emails with this?

Post by seodevhead »

I am trying to use SwiftMailer to send out an email with an attachment (from a contact form), and everything works great except it is only sending plain text emails. I would like it to send it as an HTML email. As you can see I tried setting the content-type to text/html, but no luck. Tried a few other things from the docs with no luck as well. Any guidance would be greatly appreciated. Thanks.

Code: Select all

//Create the sender from the details we've been given
			$sender =& new Swift_Address($email, $name);
			 
			//Create the message to send
			$message =& new Swift_Message("Message From: " . $name);
			$message->setContentType("text/html");
			$message->attach(new Swift_Message_Part($body));
			
			# message sent time:
			$clean['email_sent'] = date('Y:m:d h:i:s');
			
			//If an attachment was uploaded, attach it
			if ($upload_valid && isset($file_path) && isset($file_name) && isset($file_type))
			{
				$message->attach(new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
				$clean['email_sent'] = date('Y:m:d h:i:s');
			}
			
			//Try sending the email
			$sent = $swift->send($message, "info@example.com", $sender);
			//Disconnect from SMTP, we're done
			$swift->disconnect();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

//Create the sender from the details we've been given
                        $sender =& new Swift_Address($email, $name);
                        
                        //Create the message to send
                        $message =& new Swift_Message("Message From: " . $name);
                        $message->attach(new Swift_Message_Part($body, "text/html"));
                       
                        # message sent time:
                        $clean['email_sent'] = date('Y:m:d h:i:s');
                       
                        //If an attachment was uploaded, attach it
                        if ($upload_valid && isset($file_path) && isset($file_name) && isset($file_type))
                        {
                                $message->attach(new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
                                $clean['email_sent'] = date('Y:m:d h:i:s');
                        }
                       
                        //Try sending the email
                        $sent = $swift->send($message, "info@example.com", $sender);
                        //Disconnect from SMTP, we're done
                        $swift->disconnect();
As soon as you attach a "part" that becomes a mutlipart message, even if there's only one part for the client to use. The other "part" is actually the attachment in this case. You need to set the content-type on the part, not the message.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Wow... thanks so much d11wtq! I really appreciate it.

And guess what, it works!!! :)
Post Reply