PHP Mail function to SMTP

Hello Everyone,

I’ve a form that uses the mail function, but my host for some reason doesn’t allow that, they told me to use smtp instead. I’m kinda of a newbie coding, can someone please help me out with this one?
My website is live for some time, and the form isn’t working at all.

[php]<?php
?>

<?php $nameError = ''; $emailError = ''; $commentError = ''; if(isset($_POST['submitted'])) { if(trim($_POST['contactName']) === '') { $nameError = 'Please enter your name.'; $hasError = true; } else { $name = trim($_POST['contactName']); } if(trim($_POST['email']) === '') { $emailError = 'Please enter your email address.'; $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { $emailError = 'You entered an invalid email address.'; $hasError = true; } else { $email = trim($_POST['email']); } if(trim($_POST['comments']) === '') { $commentError = 'Please enter a message.'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } if(!isset($hasError)) { $emailTo = get_option('of_email'); if (!isset($emailTo) || ($emailTo == '') ){ $emailTo = get_option('admin_email'); } $subject = 'Message from '.$name; $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?> <?php get_header(); ?>
<div id="content">
	<div class="user-content">
		<?php if (get_option('of_titles_off') == "false") { ?>
			<h1 class="page-titles"><span><?php the_title(); ?></span></h1>
		<?php } ?>
		
		<div class="two_third">
			<?php if (have_posts()) : ?>
				<?php while (have_posts()) : the_post(); ?>
				<?php endwhile; ?>
			<?php endif; ?>
			
			<?php if(isset($emailSent) && $emailSent == true) { ?>
					<div class="message success nospace">
						<h2>Thanks</h2>
						<p>Your email was sent successfully, we'll get back to you as soon as possible.</p>
					</div>
					<?php } else { ?>
						<?php the_content(); ?>
						<?php if(isset($hasError) || isset($captchaError)) { ?>
							<p class="error">Sorry, an error occured.<p>
					<?php } ?>
			
				<div id="contact-form">
					<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
						<ul>
							<li>
								<?php if($nameError != '') { ?>
									<span class="error"><?php echo $nameError;?></span>
								<?php } ?>
								<label for="contactName">Name<div class="contact-arrow"></div></label>
								<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
							</li>
							<li>
								<?php if($emailError != '') { ?>
									<span class="error"><?php echo $emailError; ?></span>
								<?php } ?>
								<label for="email">Email<div class="contact-arrow"></div></label>
								<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
							</li>
							<li>
								<?php if($commentError != '') { ?>
									<span class="error"><?php echo $commentError;?></span>
								<?php } ?>
								<label class="message-area" for="commentsText">Message<div class="contact-message-arrow"></div></label>
								<textarea name="comments" id="commentsText" rows="7" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
							</li><br />
							<li>
								<button type="submit" class="button">Send</button>
							</li>
						</ul>
						<input type="hidden" name="submitted" id="submitted" value="true" />
					</form>
					<?php } ?>
				</div><!--end #contact-form-->
			</div><!-- end .two-thirds-->
			
			<div class="one_third last">
				<?php if(get_option('of_contact_address') != "") { ?><p class="contact-icon address"><?php echo get_option('of_contact_address'); ?></p><?php } ?>
				<?php if(get_option('of_contact_phone') != "") { ?><p class="contact-icon phone"><?php echo get_option('of_contact_phone'); ?></p><?php } ?>
				<?php if(get_option('of_contact_phone_alt') != "") { ?><p class="contact-icon phone-alt"><?php echo get_option('of_contact_phone_alt'); ?></p><?php } ?>
				<?php if(get_option('of_email') != "") { ?><p class="contact-icon email"><?php echo get_option('of_email'); ?></p><?php } ?>
			
				<?php if(get_option('of_maps_location') != "") { ?>
					<div class="divider"></div>
					<p class="contact-icon location">Our location</p>
					<img class="border" src="http://maps.googleapis.com/maps/api/staticmap?center=<?php echo get_option('of_maps_location') ?>&size=268x<?php echo get_option('of_maps_height') ?>&zoom=<?php echo get_option('of_maps_zoom') ?>&sensor=false&maptype=<?php echo get_option('of_maps_type') ?>&markers=<?php echo get_option('of_maps_location') ?>" />
				<?php } ?>
			</div>

	</div><!--end .user-content-->
</div><!-- end #content -->		
<?php get_footer(); ?>[/php]

I would apretiate if someone could fix this code.
If any aditicional info is needed, please say.

Thanks in advance,
Diogo

Can someone please help me?

I’ve tried everything, mixing codes, reading, but the code i’ve its more complex than i can understand.
So i can’t do anything :confused:

Once again, thanks in advance.

Standard PHP does not work with SMTP. But, it can be done with “PEAR”. Here is a site that indicates the PEAR MAIL package that is free and you can use that to send SMTP emails. Hope it helps.

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

Sponsor our Newsletter | Privacy Policy | Terms of Service