Page 1 of 1

How to send email from a form with swift mailer

Posted: Sun Feb 14, 2010 2:35 pm
by ezeuba
I have been reading some really cool discussions on swift mailer on this forum and I decided to ask my question here since I couldn't get any guidance anywhere else on the web.
I want to implement swift mailer in a basic newsletter related website where I can be able to send individual emails to the subscribers of the website (elbonfx.com). I already have a form that sends the subscriber's name and email to my email box and I then add the subscribers to my contacts. What I need is a form which I can use to implement swift mailer and how to use swift mailer to send individual newsletter emails to the subscribers. I know there are applications like phplist to do what I want but I don't always like such packaged software; I like to know what is happening with the code I use and how it works. Lately I have been trying to familiarize myself with Object Oriented PHP in order for me to better understand complex code and due to this zeal to learn more I just need to know how these things work for my own advancement. What I am asking is for someone to show me how to implement a form to email script using swift mailer. I have tried to look into phpmailer but it confuses me more than I want to be confused, so I now decided to focus on swift mailer, but sadly I cannot download the complete guide to help me understand its workings.
So if anyone can guide me I would be very grateful and would remain thankful to him/her forever.

Here is the code for the form which I want to use in implementing this:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Swift Mailer Form</title>
</head>
 
<body>
<script language='javascript'>
function verifyMe(){
var msg='';
 
if(document.getElementById('realname').value==''){
    msg+='- Name:\n\n';}
 
var email=document.getElementById('email').value;
if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
msg+='- Invalid Email Address: '+email+'\n\n';}
 
if(document.getElementById('email').value==''){
    msg+='- Email: \n\n';}
 
if(document.getElementById('subject').value==''){
    msg+='- Subject: \n\n';}
 
if(document.getElementById('message').value==''){
    msg+='- Message: \n\n';}
 
if(document.getElementById('recipient').value==''){
    msg+='- Recipient(s): \n\n';}
 
if(msg!=''){
    alert('The following fields are empty or invalid:\n\n'+msg);
    return false
}else{
    return true }
 
}
</script>
<center>
<p>Swift Mailer Form To Email</p>
<form name='Swift Mailer Form' action='./lib/classes/Swift/Mailer.php' method='POST' enctype='multipart/form-data' onsubmit='return verifyMe();'>
<table class='table_form_1' id='table_form_1' cellspacing='0'>
    <tr>
        <td class='ftbl_row_1' ><LABEL for='myname' ACCESSKEY='N' ><b style='color:red'>*</b>My Name:
        </td>
        <td class='ftbl_row_1a' ><input type='text' name='myname' id='myname' size='45' maxlength='45'  value=''>
        </td>
    </tr>
    <tr>
        <td class='ftbl_row_2' ><LABEL for='my_email' ACCESSKEY='E' ><b style='color:red'>*</b>My Email: 
        </td>
        <td class='ftbl_row_2a' ><input type='text' name='my_email' id='my_email' size='45' maxlength='45'  value=''>
        </td>
    </tr>
    <tr>
        <td class='ftbl_row_1' ><LABEL for='subject' ACCESSKEY='J' ><b style='color:red'>*</b>Subject: 
        </td>
        <td class='ftbl_row_1a' ><input type='text' name='subject' id='subject' size='45' maxlength='45'  value=''>
        </td>
    </tr>
    <tr>
        <td valign='top' class='ftbl_row_2' ><LABEL for='body' ACCESSKEY='B' ><b style='color:red'>*</b>Message Body: 
        </td>
        <td class='ftbl_row_2a' ><textarea name='body' id='body' cols='45' rows='8' value=''></textarea>
        </td>
    </tr>
    <tr>
        <td class='ftbl_row_1' ><LABEL for='recipient_name' ACCESSKEY='R' ><b style='color:red'>*</b>Recipient(s) Name<br/>(comma separated):  
        </td>
        <td class='ftbl_row_1a' ><input type='text' name='recipient_name' id='recipient_name' size='45' value=''>
        </td>
    </tr>
    <tr>
        <td class='ftbl_row_1' ><LABEL for='recipient_email' ACCESSKEY='M' ><b style='color:red'>*</b>Recipient(s) Email<br/>(comma separated): 
        </td>
        <td class='ftbl_row_1a' ><input type='text' name='recipient_email' id='recipient_email' size='45' value=''>
        </td>
    </tr>
    <tr>
        <td colspan='2' align='right'><input type='submit' name='submit' value='Send Email...'>&nbsp;<input type='reset' name='reset' value='Reset'><br />
        </td>
    </tr>
</table>
</form>
</center>
 
</body>
</html>

Re: How to send email from a form with swift mailer

Posted: Wed Mar 31, 2010 6:41 am
by siji86
Hi,

You can use Swift Mailer by first including the library and writing a few lines of code.

Code: Select all

require_once("SwiftMailer/lib/swift_required.php");
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
$mailer = Swift_Mailer::newInstance($transport);
$message=Swift_Message::newInstance();
$message->setSubject('test mail ...');
$message->setFrom(array('xyz@gmail.com'=>'sender'));
$headers = $message->getHeaders();
$message->setTo(array('beths86@gmail.com'=> 'beths'));
$message->setBody('Thus is a test message ');
$result=$mailer->send($message, $failures);
I am sure this will work.

Bye :)