What am i doing wrong? Please help.

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
apbarratt
Forum Newbie
Posts: 2
Joined: Fri Jul 17, 2009 7:40 pm

What am i doing wrong? Please help.

Post by apbarratt »

ok, i've been working on this since the morning and it is now coming up to 2AM the following morning.

What am i doing wrong here? It says it's sent but i receive nothing, please help.
<html>
<head>
<title>Katya Embroidery Email Sender</title>
</head>
<body>

<?php
//check that required data has been provided
if(!$_POST['email'])
{
die("You did not provide an email address, please click the back button and enter one. Thank you.");
}
if(!$_POST['name'])
{
die("You did not provide a name, please click the back button and enter one. Thank you.");
}

//get data from form.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$country = $_POST['country'];
$comments = $_POST['comments'];
$attachment = $_POST['attachment'];

//generate the email body
$body = "Name: $name\nCompany / Organisation: $company\nEmail: $email\nTelephone: $telephone\nCountry: $country\n\n$comments";

require_once 'swift/swift_required.php';

//Create the message
$message = Swift_Message::newInstance()

//Give the message a subject
->setSubject('KATYA EMBROIDERY - WEB SITE ENQUIRY FORM')

//Set the From address with an associative array
->setFrom(array($email => $name))

//Set the To addresses with an associative array
->setTo(array('email@email.com' => 'Katya Embroidery'))

//Give it a body
->setBody($body)

//Optionally add any attachments
->attach(Swift_Attachment::fromPath($attachment))
;


print 'Your message has been sent to Katya Embroidery, we try to reply to emails within 24 hours.<br /><a href="contact.html">Click here to return to the website.</a>';


?>

</body>
</html>
joeynovak
Forum Commoner
Posts: 26
Joined: Sun May 10, 2009 11:09 am

Re: What am i doing wrong? Please help.

Post by joeynovak »

I don't see a send call... My guess is you need to call a method after attach to actually "send" the email.

Joey
apbarratt
Forum Newbie
Posts: 2
Joined: Fri Jul 17, 2009 7:40 pm

Re: What am i doing wrong? Please help.

Post by apbarratt »

thanks Joey, have added the code for sending but still having trouble.

Here is updated code. Anyone have any ideas?

Code: Select all

<html>
<head>
<title>Katya Embroidery Email Sender</title>
</head>
<body>
 
<?php 
   //check that required data has been provided
   if(!$_POST['name'])
              {
                die("You did not provide a name, please click the back button and enter one.  Thank you.");
              }
    if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $_POST['email']))
              {
                die("You did not provide a valid email address, please click the back button and enter one.  Thank you.");
              }
              
   //get data from form.
   $name = $_POST['name'];
   $company = $_POST['company'];
   $email = $_POST['email'];
   $telephone = $_POST['telephone'];
   $country = $_POST['country'];
   $comments = $_POST['comments'];
   
   //check for file upload
    $file_path = false;
    $file_name = false;
    $file_type = false;
    if (!empty($_FILES["attachment"]["tmp_name"]))
    {
        if ($_FILES["attachment"]["error"])
        {
           //Redirect if the upload has failed
            die("Your attachment had an error while uploading, please click the back button and try again.  Thank you.");
        }
        $file_path = $_FILES["attachment"]["tmp_name"];
        $file_name = $_FILES["attachment"]["name"];
        $file_type = $_FILES["attachment"]["type"];
    }
   
   //generate the email body
   $body = "Name: $name\nCompany / Organisation: $company\nEmail: $email\nTelephone: $telephone\nCountry: $country\n\n$comments";
   
   
   //start Swift
   require_once 'swift/swift_required.php';
   
   //Create the Transport
   $transport = Swift_SmtpTransport::newInstance('localhost', 25);
   
   //Create mailer using created transport
   $mailer = Swift_Mailer::newInstance($transport);
 
       //Create the message
       $message = Swift_Message::newInstance()
 
      //Give the message a subject
      ->setSubject('KATYA EMBROIDERY - WEB SITE ENQUIRY FORM')
    
      //Set the From address with an associative array
      ->setFrom(array($email => $name))
    
      //Set the To addresses with an associative array
      ->setTo(array('apbarratt@me.com' => 'Katya Embroidery'))
    
      //Give it a body
      ->setBody($body)
    
      //Optionally add any attachments
      //->attach(Swift_Attachment::fromPath($file_path))
      ;
      
    $result = $mailer->send($message);
 
    if($result)
    {
         print 'Your message has been sent to Katya Embroidery, we try to reply to emails within 24 hours.<br /><a href="contact.html">Click here to return to the website.</a>';
    }
    else
    {
         print 'Your message FAILED to send to Katya Embroidery, please click the back button to try again.';
    }
   
?>
 
</body>
</html>
As you can see, i've tried commenting out the add attachment line but a still having no luck.
Last edited by Benjamin on Sun Jul 19, 2009 12:01 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply